Demo/Domain/UseCase/UserUseCase.cs

77 lines
2.7 KiB
C#
Raw Normal View History

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