pr1/presence/domain/UseCase/UserUseCase.cs

120 lines
3.3 KiB
C#
Raw Normal View History

2024-11-17 16:24:01 +00:00
using Demo.Data.Exceptions;
2024-12-05 09:30:01 +00:00
using Demo.Data.RemoteData.RemoteDataBase.DAO;
2024-11-17 16:24:01 +00:00
using Demo.Data.Repository;
2024-10-19 21:12:06 +00:00
using Demo.domain.Models;
namespace Demo.Domain.UseCase
{
public class UserUseCase
{
2024-11-17 16:24:01 +00:00
private readonly IUserRepository _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl;
2024-10-19 21:12:06 +00:00
2024-11-17 16:24:01 +00:00
public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
2024-10-19 21:12:06 +00:00
{
_repositoryUserImpl = repositoryImpl;
_repositoryGroupImpl = repositoryGroupImpl;
}
2024-11-17 16:24:01 +00:00
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();
2024-12-05 09:30:01 +00:00
public bool RemoveUserByGuid(Guid userGuid)
2024-11-17 16:24:01 +00:00
{
try
2024-10-19 21:39:57 +00:00
{
2024-12-05 09:30:01 +00:00
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
2024-10-19 21:39:57 +00:00
}
2024-12-05 09:30:01 +00:00
catch (UserNotFoundException)
2024-10-19 21:39:57 +00:00
{
2024-11-17 16:24:01 +00:00
return false;
2024-10-19 21:39:57 +00:00
}
2024-12-05 09:30:01 +00:00
catch (RepositoryException)
2024-11-17 16:24:01 +00:00
{
return false;
}
}
2024-12-05 09:30:01 +00:00
2024-11-17 16:24:01 +00:00
public User UpdateUser(User user)
{
UserLocalEnity userLocalEnity = new UserLocalEnity
{
FIO = user.FIO,
GroupID = user.Group.Id,
Guid = user.Guid
};
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);
2024-10-19 21:39:57 +00:00
2024-11-17 16:24:01 +00:00
if (result == null)
{
throw new Exception("Ошибка при обновлении пользователя.");
}
2024-12-05 09:30:01 +00:00
var groupEntity = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == result.GroupID);
if (groupEntity == null)
{
throw new Exception("Группа не найдена.");
}
2024-11-17 16:24:01 +00:00
return new User
{
FIO = result.FIO,
Guid = result.Guid,
Group = new Group
{
Id = groupEntity.Id,
Name = groupEntity.Name
}
};
}
2024-12-05 09:30:01 +00:00
public User FindUserByGuid(Guid userGuid)
2024-11-17 16:24:01 +00:00
{
2024-12-05 09:30:01 +00:00
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("Группа не найдена.");
}
2024-11-17 16:24:01 +00:00
return new User
{
FIO = user.FIO,
Guid = user.Guid,
Group = new Group { Id = group.Id, Name = group.Name }
};
2024-10-19 21:12:06 +00:00
}
2024-12-05 09:30:01 +00:00
public UserDao GetUserInfo(Guid userGuid)
{
return _repositoryUserImpl.GetUserInfo(userGuid);
}
2024-10-19 21:12:06 +00:00
}
2024-12-05 09:30:01 +00:00
2024-11-17 16:24:01 +00:00
}
2024-12-05 09:30:01 +00:00