using Zurnal.Data.Repository; using Zurnal.Date.Repository; using Zurnal.domain.Models; using Zurnal.RemaDateBase.DateDao; namespace Zurnal.Domain.UseCase { public class UserUseCase : IGroupRepository { private readonly UserRepositoryImpl _repositoryUserImpl; internal IGroupRepository RepositoryGroupImpl { get; } public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) { _repositoryUserImpl = repositoryImpl; RepositoryGroupImpl = (IGroupRepository?)(repositoryGroupImpl ?? throw new ArgumentNullException(nameof(repositoryGroupImpl))); } public List GetAllUsers() => _repositoryUserImpl.GetAllUsersList() .Join(RepositoryGroupImpl.AllGroup, user => user.GroupID, group => group.Id, (user, group) => new User { FIO = user.FIO, Guid = user.Guid, Group = new Group { Id = group.Id, Name = group.GroupName } } ).ToList(); public bool RemoveUserByGuid(Guid userGuid) { return _repositoryUserImpl.RemoveUserByGuid(userGuid); } public User UpdateUser(User user, int groupId) { UserLocalEntity userLocalEntity = new UserLocalEntity { FIO = user.FIO, GroupID = groupId, 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 }; } private List GetAllGroups() { return RepositoryGroupImpl.AllGroup.Select(g => new Group { Id = g.Id, Name = g.GroupName }).ToList(); } public User FindUserByGuid(Guid userGuid) { var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid); if (user == null) throw new Exception("Пользователь не найден."); var group = RepositoryGroupImpl.AllGroup.FirstOrDefault(g => g.Id == user.GroupID); return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; } public List AllGroup => new List(); List IGroupRepository.AllGroup => throw new NotImplementedException(); IEnumerable IGroupRepository.AllGroups() { return (IEnumerable)((IGroupRepository)_repositoryUserImpl).GetAllGroups(); } public bool RemoveGroupById(int groupID) { throw new NotImplementedException(); } public bool UpdateGroupById(int groupID, GroupDao updatedGroup) { throw new NotImplementedException(); } public bool AddGroup(GroupDao newGroup) { throw new NotImplementedException(); } public void AddGroupFromRegex(System.Text.RegularExpressions.Group group) { throw new NotImplementedException(); } public GroupDao GetGroupById(int id) { throw new NotImplementedException(); } IEnumerable IGroupRepository.GetAllGroups() { throw new NotImplementedException(); } public void UpdateGroup(GroupDao group) { throw new NotImplementedException(); } public void DeleteGroup(int id) { throw new NotImplementedException(); } internal class UserLocalEntity { public string FIO { get; set; } public int GroupID { get; set; } public Guid Guid { get; set; } } } }