using Demo.Data.Repository; using Demo.domain.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo.Domain.UseCase { public class UserUseCase { private readonly UserRepositoryImpl _repositoryUserImpl; private readonly IGroupRepository _repositoryGroupImpl; public UserUseCase(UserRepositoryImpl repositoryImpl, IGroupRepository repositoryGroupImpl) { _repositoryUserImpl = repositoryImpl; _repositoryGroupImpl = repositoryGroupImpl; } private List GetAllGroups() => _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(); // удалить пользователя по Giud public bool RemoveUserByGuid(Guid userGuid) { return _repositoryUserImpl.RemoveUserByGuid(userGuid); } // Обновить пользователя по Guid public User UpdateUser(User user) { UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); if (result == null) { throw new Exception(""); } Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID); if (group == null) { throw new Exception(""); } return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; } // Найти пользователя по Guid public User FindUserByGuid(Guid userGuid) { var user = _repositoryUserImpl.GetAllUsers .FirstOrDefault(u => u.Guid == userGuid); if (user == null) throw new Exception("Пользователь не найден"); var group = _repositoryGroupImpl.GetAllGroup() .FirstOrDefault(g => g.Id == user.GroupID); return new User { FIO = user.FIO, Guid = user.Guid, Group = new Group { Id = group.Id, Name = group.Name } }; } //public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups().Select(it => new Group { Id = it.Id, Name = it.Name }).ToList(); } }