2024-10-30 09:11:19 +00:00
|
|
|
|
using Demo.Data.Entity;
|
|
|
|
|
using Demo.Data.Repository;
|
2024-10-14 11:51:48 +00:00
|
|
|
|
using Demo.domain.Models;
|
2024-10-14 11:19:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Demo.Domain.UseCase
|
|
|
|
|
{
|
|
|
|
|
public class UserUseCase
|
|
|
|
|
{
|
2024-10-30 09:11:19 +00:00
|
|
|
|
private readonly IUserRepository _repositoryUserImpl;
|
2024-10-21 12:42:07 +00:00
|
|
|
|
private readonly IGroupRepository _repositoryGroupImpl;
|
2024-10-14 11:19:48 +00:00
|
|
|
|
|
2024-10-21 12:42:07 +00:00
|
|
|
|
|
2024-10-30 09:11:19 +00:00
|
|
|
|
public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
|
2024-10-14 11:19:48 +00:00
|
|
|
|
{
|
2024-10-14 11:51:48 +00:00
|
|
|
|
_repositoryUserImpl = repositoryImpl;
|
|
|
|
|
_repositoryGroupImpl = repositoryGroupImpl;
|
2024-10-14 11:19:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-21 12:42:07 +00:00
|
|
|
|
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
|
2024-10-18 09:51:43 +00:00
|
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
|
|
|
|
|
|
2024-10-30 09:11:19 +00:00
|
|
|
|
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers()
|
2024-10-21 12:42:07 +00:00
|
|
|
|
.Join(_repositoryGroupImpl.GetAllGroup(),
|
2024-10-14 11:51:48 +00:00
|
|
|
|
user => user.GroupID,
|
|
|
|
|
group => group.Id,
|
2024-10-18 09:51:43 +00:00
|
|
|
|
(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)
|
|
|
|
|
{
|
2024-10-30 09:11:19 +00:00
|
|
|
|
return _repositoryUserImpl.RemoveUserById(userGuid);
|
2024-10-16 07:01:04 +00:00
|
|
|
|
}
|
2024-10-18 09:51:43 +00:00
|
|
|
|
|
2024-10-30 09:11:19 +00:00
|
|
|
|
public User UpdateUser(Guid userGuid,User user)
|
2024-10-18 09:51:43 +00:00
|
|
|
|
{
|
2024-10-14 11:51:48 +00:00
|
|
|
|
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
|
2024-10-30 09:11:19 +00:00
|
|
|
|
bool result = _repositoryUserImpl.UpdateUserById(userGuid, userLocalEnity);
|
|
|
|
|
if (!result) throw new Exception("");
|
|
|
|
|
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == user.Group.Id);
|
2024-10-14 11:51:48 +00:00
|
|
|
|
if (group == null) throw new Exception("");
|
2024-10-18 09:51:43 +00:00
|
|
|
|
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User? FindUserByGuid(Guid userGuid)
|
|
|
|
|
{
|
2024-10-30 09:11:19 +00:00
|
|
|
|
var userLocal = _repositoryUserImpl.GetUserById(userGuid);
|
2024-10-18 09:51:43 +00:00
|
|
|
|
if (userLocal == null) return null;
|
|
|
|
|
|
2024-10-21 12:42:07 +00:00
|
|
|
|
var group = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == userLocal.GroupID);
|
2024-10-18 09:51:43 +00:00
|
|
|
|
return new User
|
|
|
|
|
{
|
|
|
|
|
FIO = userLocal.FIO,
|
|
|
|
|
Guid = userLocal.Guid,
|
|
|
|
|
Group = group != null ? new Group { Id = group.Id, Name = group.Name } : null
|
|
|
|
|
};
|
2024-10-14 11:51:48 +00:00
|
|
|
|
}
|
2024-10-14 11:19:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|