112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
|
using Dataceptions;
|
|||
|
using data.Repository;
|
|||
|
using domain.Models;
|
|||
|
|
|||
|
namespace domain.UseCase
|
|||
|
{
|
|||
|
public class UserUseCase
|
|||
|
{
|
|||
|
private readonly IUserRepository _repositoryUserImpl;
|
|||
|
private readonly IGroupRepository _repositoryGroupImpl;
|
|||
|
|
|||
|
public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
|
|||
|
{
|
|||
|
_repositoryUserImpl = repositoryImpl;
|
|||
|
_repositoryGroupImpl = repositoryGroupImpl;
|
|||
|
}
|
|||
|
|
|||
|
// Вывести всех пользователей
|
|||
|
public List<User> 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();
|
|||
|
|
|||
|
// Удалить пользователя по id
|
|||
|
public bool RemoveUserById(Guid userGuid)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
return _repositoryUserImpl.RemoveUserById(userGuid);
|
|||
|
}
|
|||
|
catch (UserNotFoundException)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
catch (RepositoryException)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Обновить пользователя по 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("Ошибка при обновлении пользователя.");
|
|||
|
}
|
|||
|
|
|||
|
var groupEntity = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == result.GroupID);
|
|||
|
|
|||
|
if (groupEntity == null)
|
|||
|
{
|
|||
|
throw new Exception("Группа не найдена.");
|
|||
|
}
|
|||
|
|
|||
|
return new User
|
|||
|
{
|
|||
|
FIO = result.FIO,
|
|||
|
Guid = result.Guid,
|
|||
|
Group = new Group
|
|||
|
{
|
|||
|
Id = groupEntity.Id,
|
|||
|
Name = groupEntity.Name
|
|||
|
}
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
// Найти пользователя по id
|
|||
|
public User FindUserById(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);
|
|||
|
|
|||
|
if (group == null)
|
|||
|
{
|
|||
|
throw new Exception("Группа не найдена.");
|
|||
|
}
|
|||
|
|
|||
|
return new User
|
|||
|
{
|
|||
|
FIO = user.FIO,
|
|||
|
Guid = user.Guid,
|
|||
|
Group = new Group { Id = group.Id, Name = group.Name }
|
|||
|
};
|
|||
|
}
|
|||
|
}
|
|||
|
}
|