2024-10-17 11:46:19 +00:00
|
|
|
|
using Demo.Domain.Models;
|
|
|
|
|
using Demo.Data.Repository;
|
|
|
|
|
|
|
|
|
|
namespace Demo.Domain.UseCase
|
|
|
|
|
{
|
|
|
|
|
public class UserUseCase
|
|
|
|
|
{
|
2024-10-21 09:38:07 +00:00
|
|
|
|
private readonly IUserRepository _repositoryUserImpl;
|
|
|
|
|
private readonly IGroupRepository _repositoryGroupImpl;
|
2024-10-17 11:46:19 +00:00
|
|
|
|
|
2024-10-21 09:38:07 +00:00
|
|
|
|
public UserUseCase(IGroupRepository repositoryGroupImpl, IUserRepository repositoryUserImpl)
|
2024-10-17 11:46:19 +00:00
|
|
|
|
{
|
|
|
|
|
_repositoryGroupImpl = repositoryGroupImpl;
|
|
|
|
|
_repositoryUserImpl = repositoryUserImpl;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 09:38:07 +00:00
|
|
|
|
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
|
2024-10-17 11:46:19 +00:00
|
|
|
|
.Select(it => new Group{ID = it.ID, Name = it.Name}).ToList();
|
|
|
|
|
|
2024-10-21 09:38:07 +00:00
|
|
|
|
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUser()
|
|
|
|
|
.Join(_repositoryGroupImpl.GetAllGroup(),
|
2024-10-17 11:46:19 +00:00
|
|
|
|
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 User GetUserByGuid(Guid userGuid){
|
|
|
|
|
UserLocalEntity? userLocalEntity = _repositoryUserImpl.GetUserById(userGuid);
|
|
|
|
|
if (userLocalEntity == null) throw new Exception("bello");
|
|
|
|
|
Group? group = GetAllGroups().FirstOrDefault(it => userLocalEntity.GroupID == it.ID);
|
|
|
|
|
if (group == null) throw new Exception("bello");
|
|
|
|
|
return new User{
|
|
|
|
|
FIO = userLocalEntity.FIO,
|
|
|
|
|
Guid = userLocalEntity.Guid,
|
|
|
|
|
Group = new Group{
|
|
|
|
|
ID = group.ID,
|
|
|
|
|
Name = group.Name}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RemoveUserByGuid(Guid userGuid) {
|
|
|
|
|
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User UpdateUser(User user)
|
|
|
|
|
{
|
|
|
|
|
UserLocalEntity userLocalEntity = new UserLocalEntity
|
|
|
|
|
{
|
|
|
|
|
FIO = user.FIO,
|
|
|
|
|
Guid = user.Guid,
|
|
|
|
|
GroupID = user.Group.ID
|
|
|
|
|
};
|
|
|
|
|
UserLocalEntity? result = _repositoryUserImpl.UpdateUserById(userLocalEntity);
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не удалось обновить пользователя: пользователь не найден.");
|
|
|
|
|
}
|
|
|
|
|
Group? group = GetAllGroups().FirstOrDefault(it => result.GroupID == it.ID);
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не удалось обновить пользователя: группа не найдена.");
|
|
|
|
|
}
|
|
|
|
|
return new User
|
|
|
|
|
{
|
|
|
|
|
FIO = result.FIO,
|
|
|
|
|
Guid = result.Guid,
|
|
|
|
|
Group = group
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|