using Zurnal.Data.Repository; using Zurnal.domain.Models; using Zurnal.RemaDateBase; namespace Zurnal.Domain.UseCase { public class UserUseCase { private readonly UserRepositoryImpl _repositoryUserImpl; internal IGroupRepository RepositoryGroupImpl { get; } public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) { _repositoryUserImpl = repositoryImpl; RepositoryGroupImpl = (IGroupRepository?)repositoryGroupImpl; } public List GetAllGroups() { return RepositoryGroupImpl.GetAllGroup() .Select(it => new Group { Id = it.Id, Name = it.Name }).ToList(); public List GetAllUsers() => _repositoryUserImpl.GetAllUsers() .Join(RepositoryGroupImpl.GetAllGroup(), user => user.GroupID, group => group.Id, (user, group) => new User { FIO = user.FIO, Guid = user.Guid, Group = new Group { Id = group.Id, Name = group.Name } } ).ToList(); public bool RemoveUserByGuid(Guid userGuid) { return _repositoryUserImpl.RemoveUserByGuid(userGuid); } public User UpdateUser(User user) { UserLocalEnity userLocalEntity = new UserLocalEntity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEntity); if (result == null) throw new Exception("User update failed."); Group? group = GetAllGroups().FirstOrDefault(it => it.Id = result.GroupID); if (group == null) throw new Exception("Group not found."); return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; } public User FindUserByGuid(Guid userGuid) { var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid); if (user == null) throw new Exception("User not found."); var group = RepositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == user.GroupID); return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; } } internal class UserLocalEntity { public string FIO { get; set; } public Guid GroupID { get; set; } public Guid Guid { get; set; } } }