Demo/Domain/UseCase/UserUseCase.cs

80 lines
2.7 KiB
C#
Raw Normal View History

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
{
2024-10-24 20:41:31 +00:00
public class UserUseCase : IUserUseCase
{
2024-10-21 10:38:39 +00:00
private readonly IUserRepository _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl;
2024-10-21 10:38:39 +00:00
public UserUseCase(IUserRepository userRepository, IGroupRepository groupRepository)
{
2024-10-21 10:38:39 +00:00
_repositoryUserImpl = userRepository;
_repositoryGroupImpl = groupRepository;
}
2024-10-21 10:38:39 +00:00
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
2024-10-24 20:41:31 +00:00
.Select(it => new Group{Id = it.Id, Name = it.Name}).ToList();
2024-10-21 10:38:39 +00:00
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUser()
2024-10-24 20:41:31 +00:00
.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 User GetUserByGuid(Guid userGuid) {
UserLocalEntity? userLocalEntity = _repositoryUserImpl.GetUserByGuid(userGuid);
2024-10-24 20:41:31 +00:00
if (userLocalEntity == null) throw new Exception("bello");
Group? group = GetAllGroups().FirstOrDefault(it => userLocalEntity.GroupID == it.Id);
if (group == null) throw new Exception("bello");
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) {
2024-10-24 20:41:31 +00:00
UserLocalEntity userLocalEntity = new UserLocalEntity
{
FIO = user.FIO,
Guid = user.Guid,
GroupID = user.Group.Id
};
UserLocalEntity? result = _repositoryUserImpl.UpdateUser(userLocalEntity);
if (result == null)
{
throw new Exception("");
}
Group? group = GetAllGroups().FirstOrDefault(it => result.GroupID == it.Id);
if (group == null)
{
throw new Exception("");
}
return new User
{
FIO = result.FIO,
Guid = result.Guid,
Group = group
};
}
}
}