2024-10-18 06:12:15 +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
|
|
|
|
|
{
|
|
|
|
|
private UserRepositoryImpl _repositoryUserImpl;
|
|
|
|
|
private GroupRepositoryImpl _repositoryGroupImpl;
|
|
|
|
|
|
|
|
|
|
public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
|
|
|
|
|
{
|
|
|
|
|
_repositoryUserImpl = repositoryImpl;
|
|
|
|
|
_repositoryGroupImpl = repositoryGroupImpl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
|
|
|
|
|
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
|
|
|
|
|
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers
|
|
|
|
|
.Join(_repositoryGroupImpl.GetAllGroups(),
|
|
|
|
|
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.RemoveUserByGuid(userGuid);
|
|
|
|
|
}
|
|
|
|
|
public User UpdateUser(User user) {
|
2024-10-18 06:49:19 +00:00
|
|
|
|
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
|
|
|
|
|
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);
|
2024-10-18 06:12:15 +00:00
|
|
|
|
if (result == null) throw new Exception("");
|
|
|
|
|
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
|
2024-10-18 06:49:19 +00:00
|
|
|
|
if (group == null) throw new Exception("Группа не найдена");
|
|
|
|
|
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
|
|
|
|
|
}
|
2024-10-18 06:12:15 +00:00
|
|
|
|
|
|
|
|
|
public User FindUserByGuid(Guid userGuid)
|
|
|
|
|
{
|
2024-10-18 07:04:09 +00:00
|
|
|
|
var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid);
|
2024-10-18 06:12:15 +00:00
|
|
|
|
if (user == null) throw new Exception("Нет такого пользователя");
|
|
|
|
|
var group = _repositoryGroupImpl.GetAllGroups().FirstOrDefault(g => g.Id == user.GroupID);
|
|
|
|
|
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-18 06:49:19 +00:00
|
|
|
|
}
|