presence/Demo/Domain/UseCase/UserUseCase.cs

53 lines
2.0 KiB
C#
Raw Permalink Normal View History

2024-10-14 11:19:48 +00:00
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;
using System.Text;
using System.Threading.Tasks;
namespace Demo.Domain.UseCase
{
public class UserUseCase
{
2024-10-16 07:01:04 +00:00
private readonly UserRepositoryImpl _repositoryUserImpl;
private readonly IGroupRepository _repositoryGroupImpl;
2024-10-14 11:19:48 +00:00
2024-10-23 07:47:07 +00:00
public UserUseCase(IGroupRepository repository) {
_repositoryGroupImpl = repository;
_repositoryUserImpl = new UserRepositoryImpl();
}
2024-10-16 07:01:04 +00:00
public UserUseCase(UserRepositoryImpl 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-16 07:01:04 +00:00
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
2024-10-14 11:51:48 +00:00
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
2024-10-16 07:01:04 +00:00
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
.Join(_repositoryGroupImpl.GetAllGroup(),
2024-10-14 11:51:48 +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();
2024-10-16 07:01:04 +00:00
public bool RemoveUserByGuid(Guid userGuid) {
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
}
2024-10-14 11:51:48 +00:00
public User UpdateUser(User user) {
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
UserLocalEnity? 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};
}
2024-10-14 11:19:48 +00:00
}
}