78 lines
2.4 KiB
C#
78 lines
2.4 KiB
C#
using Demo.Data.Repository;
|
||
using Demo.Domain.Models;
|
||
using Demo.Data.Exceptions;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Demo.domain.Models;
|
||
|
||
namespace Demo.Domain.UseCase
|
||
{
|
||
public class UserUseCase
|
||
{
|
||
private readonly UserRepositoryImpl _userRepository;
|
||
private readonly IGroupRepository _groupRepository;
|
||
|
||
public UserUseCase(UserRepositoryImpl userRepository, IGroupRepository groupRepository)
|
||
{
|
||
_userRepository = userRepository;
|
||
_groupRepository = groupRepository;
|
||
}
|
||
|
||
private List<Group> GetAllGroups() =>
|
||
_groupRepository.GetAllGroups()
|
||
.Select(g => new Group { Id = g.Id, Name = g.Name })
|
||
.ToList();
|
||
|
||
public List<User> GetAllUsers() =>
|
||
_userRepository.GetAllUsers
|
||
.Join(_groupRepository.GetAllGroups(),
|
||
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 bool RemoveUserByGuid(Guid userGuid)
|
||
{
|
||
try
|
||
{
|
||
_userRepository.DeleteUserByGuid(userGuid);
|
||
return true;
|
||
}
|
||
catch (GuidNotFoundException)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public User UpdateUser(User user)
|
||
{
|
||
try
|
||
{
|
||
_userRepository.UpdateUser(user.Guid.ToString(), user.FIO, user.Group.Id);
|
||
|
||
var group = GetAllGroups().FirstOrDefault(g => g.Id == user.Group.Id);
|
||
if (group == null)
|
||
{
|
||
throw new Exception($"Группа с ID {user.Group.Id} не найдена.");
|
||
}
|
||
|
||
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
|
||
}
|
||
catch (GuidNotFoundException ex)
|
||
{
|
||
throw new Exception("Пользователь не найден.", ex);
|
||
}
|
||
catch (ArgumentException ex)
|
||
{
|
||
throw new Exception("Некорректный GUID.", ex);
|
||
}
|
||
}
|
||
}
|
||
}
|