Raspisanie/Zurnal/Domain/UseCase/UserUseCase.cs

45 lines
2.1 KiB
C#
Raw Normal View History

2024-10-23 08:14:10 +00:00
using Zurnal.Data.Repository;
using Zurnal.domain.Models;
2024-10-21 11:56:16 +00:00
using Zurnal.Date.Repository;
2024-10-23 08:14:10 +00:00
namespace Zurnal.Domain.UseCase
2024-10-21 11:56:16 +00:00
{
2024-10-23 08:27:57 +00:00
public class UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl)
2024-10-21 11:56:16 +00:00
{
2024-10-23 08:27:57 +00:00
private UserRepositoryImpl _repositoryUserImpl = repositoryImpl;
2024-10-21 11:56:16 +00:00
2024-10-23 08:27:57 +00:00
internal IGroupRepository RepositoryGroupImpl { get; } = (IGroupRepository?)repositoryGroupImpl;
2024-10-21 11:56:16 +00:00
2024-10-23 08:27:57 +00:00
public List<Group> GetAllGroups() => RepositoryGroupImpl.GetAllGroup()
2024-10-21 11:56:16 +00:00
.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.RemoveUserByGuid(userGuid);
}
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 };
}
public User FindUserByGuid(Guid userGuid)
{
var user = _repositoryUserImpl.GetAllUsersList().FirstOrDefault(u => u.Guid == userGuid);
if (user == null) throw new Exception("Нет такого пользователя");
var group = RepositoryGroupImpl.GetAllGroup().FirstOrDefault(g => g.Id == user.GroupID);
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
}
}