presenceNikita/Demo/Domain/UseCase/UserUseCase.cs

85 lines
2.9 KiB
C#
Raw Normal View History

2024-10-16 08:22:40 +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 08:46:20 +00:00
private readonly UserRepositoryImpl _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl;
2024-10-16 08:22:40 +00:00
2024-10-21 08:46:20 +00:00
public UserUseCase(UserRepositoryImpl repositoryImpl, IGroupRepository repositoryGroupImpl)
2024-10-16 08:22:40 +00:00
{
_repositoryUserImpl = repositoryImpl;
_repositoryGroupImpl = repositoryGroupImpl;
}
2024-10-21 08:46:20 +00:00
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
2024-10-19 09:39:40 +00:00
.Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
2024-10-21 08:46:20 +00:00
2024-10-19 09:39:40 +00:00
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
2024-10-21 08:46:20 +00:00
.Join(_repositoryGroupImpl.GetAllGroup(),
2024-10-19 09:39:40 +00:00
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();
// удалить пользователя по Giud
public bool RemoveUserByGuid(Guid userGuid)
{
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
2024-10-16 08:22:40 +00:00
}
2024-10-19 09:39:40 +00:00
// Обновить пользователя по Guid
public User UpdateUser(User user)
{
2024-10-16 08:22:40 +00:00
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);
2024-10-19 09:39:40 +00:00
if (result == null)
{
throw new Exception("");
}
2024-10-16 08:22:40 +00:00
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
2024-10-19 09:39:40 +00:00
if (group == null)
{
throw new Exception("");
}
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
2024-10-16 08:22:40 +00:00
}
2024-10-19 09:39:40 +00:00
// Найти пользователя по Guid
public User FindUserByGuid(Guid userGuid)
{
var user = _repositoryUserImpl.GetAllUsers
.FirstOrDefault(u => u.Guid == userGuid);
if (user == null) throw new Exception("Пользователь не найден");
2024-10-21 08:46:20 +00:00
var group = _repositoryGroupImpl.GetAllGroup()
2024-10-19 09:39:40 +00:00
.FirstOrDefault(g => g.Id == user.GroupID);
return new User
{
FIO = user.FIO,
Guid = user.Guid,
Group = new Group { Id = group.Id, Name = group.Name }
};
}
//public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups().Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
2024-10-16 08:22:40 +00:00
}
}