xxxproject/Demo/Domain/UseCase/UserUseCase.cs
Class_Student 6b9b5a424f init
2024-10-30 12:11:19 +03:00

63 lines
2.3 KiB
C#

using Demo.Data.Entity;
using Demo.Data.Repository;
using Demo.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Demo.Domain.UseCase
{
public class UserUseCase
{
private readonly IUserRepository _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl;
public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
{
_repositoryUserImpl = repositoryImpl;
_repositoryGroupImpl = repositoryGroupImpl;
}
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
.Select(it => new Group { Id = it.Id, Name = it.Name }).ToList();
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();
public bool RemoveUserByGuid(Guid userGuid)
{
return _repositoryUserImpl.RemoveUserById(userGuid);
}
public User UpdateUser(Guid userGuid,User user)
{
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
bool result = _repositoryUserImpl.UpdateUserById(userGuid, userLocalEnity);
if (!result) throw new Exception("");
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == user.Group.Id);
if (group == null) throw new Exception("");
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
public User? FindUserByGuid(Guid userGuid)
{
var userLocal = _repositoryUserImpl.GetUserById(userGuid);
if (userLocal == null) return null;
var group = _repositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == userLocal.GroupID);
return new User
{
FIO = userLocal.FIO,
Guid = userLocal.Guid,
Group = group != null ? new Group { Id = group.Id, Name = group.Name } : null
};
}
}
}