2024-10-16 20:15:49 +00:00
|
|
|
using Demo.Data.Repository;
|
|
|
|
using Demo.domain.Models;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Demo.Domain.UseCase
|
|
|
|
{
|
|
|
|
public class UserUseCase
|
|
|
|
{
|
2024-10-21 09:07:49 +00:00
|
|
|
private readonly UserRepositoryImpl _repositoryUserImpl;
|
|
|
|
private readonly GroupRepositoryImpl _repositoryGroupImpl;
|
2024-10-16 20:15:49 +00:00
|
|
|
|
|
|
|
public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
|
|
|
|
{
|
|
|
|
_repositoryUserImpl = repositoryImpl;
|
|
|
|
_repositoryGroupImpl = repositoryGroupImpl;
|
|
|
|
}
|
|
|
|
|
2024-10-21 09:07:49 +00:00
|
|
|
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups
|
2024-10-16 20:15:49 +00:00
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
|
|
|
|
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
|
|
|
|
.Join(_repositoryGroupImpl.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 User GetUserByGuid(Guid userGuid) {
|
|
|
|
UserLocalEntity? userLocalEntity = _repositoryUserImpl.GetUserByGuid(userGuid);
|
|
|
|
if (userLocalEntity == null) throw new Exception("");
|
|
|
|
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == userLocalEntity!.GroupID);
|
|
|
|
if (group == null) throw new Exception("");
|
|
|
|
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 userLocalEnity = new UserLocalEntity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
|
|
|
|
UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);
|
|
|
|
if (result == null) throw new Exception("");
|
|
|
|
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
|
|
|
|
if (group == null) throw new Exception("");
|
|
|
|
return new User { FIO = user.FIO, Guid = user.Guid, Group = group};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|