From 685c86d2ec0c7fc11e7d69b1d6f6e4616173b7a3 Mon Sep 17 00:00:00 2001 From: NikitaOnianov Date: Sat, 19 Oct 2024 12:39:40 +0300 Subject: [PATCH] UseCase --- Demo/Domain/UseCase/GroupUseCase.cs | 35 +++++++++++++- Demo/Domain/UseCase/UserUseCase.cs | 71 ++++++++++++++++++++++------- 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/Demo/Domain/UseCase/GroupUseCase.cs b/Demo/Domain/UseCase/GroupUseCase.cs index 414853a..f9e42b9 100644 --- a/Demo/Domain/UseCase/GroupUseCase.cs +++ b/Demo/Domain/UseCase/GroupUseCase.cs @@ -1,4 +1,6 @@ -using System; +using Demo.Data.Repository; +using Demo.domain.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,5 +10,36 @@ namespace Demo.Domain.UseCase { internal class GroupUseCase { + private readonly GroupRepositoryImpl _repositoryGroupImpl; + + public GroupUseCase(GroupRepositoryImpl repositoryGroupImpl) + { + _repositoryGroupImpl = repositoryGroupImpl; + } + + // Вывести все группы + public List GetAllGroups() + { + return _repositoryGroupImpl.GetAllGroups().Select(it => new Group { Id = it.Id, Name = it.Name }).ToList(); + } + // Создать новую группу + public void AddGroup(string groupName) + { + var newId = _repositoryGroupImpl.GetAllGroups().Max(g => g.Id) + 1; + GroupLocalEntity newGroup = new GroupLocalEntity + { + Id = newId, + Name = groupName + }; + _repositoryGroupImpl.AddGroup(newGroup); + } + + // Изменить название группы по ID + public void UpdateGroup(int groupId, string newGroupName) + { + GroupLocalEntity existingGroup = _repositoryGroupImpl.GetAllGroups().FirstOrDefault(g => g.Id == groupId); + existingGroup.Name = newGroupName; + _repositoryGroupImpl.UpdateGroup(existingGroup); + } } } diff --git a/Demo/Domain/UseCase/UserUseCase.cs b/Demo/Domain/UseCase/UserUseCase.cs index f1f44e1..544162d 100644 --- a/Demo/Domain/UseCase/UserUseCase.cs +++ b/Demo/Domain/UseCase/UserUseCase.cs @@ -19,28 +19,65 @@ namespace Demo.Domain.UseCase _repositoryGroupImpl = repositoryGroupImpl; } - public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() - .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); - public List 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 List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() + .Select(it => new Group { Id = it.Id, Name = it.Name }).ToList(); + public List 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); + // удалить пользователя по Giud + public bool RemoveUserByGuid(Guid userGuid) + { + return _repositoryUserImpl.RemoveUserByGuid(userGuid); } - public User UpdateUser(User user) { + + + // Обновить пользователя по Guid + 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(""); + + 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}; + + if (group == null) + { + throw new Exception(""); + } + return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; } + + // Найти пользователя по Guid + public User FindUserByGuid(Guid userGuid) + { + var user = _repositoryUserImpl.GetAllUsers + .FirstOrDefault(u => u.Guid == userGuid); + 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 = new Group { Id = group.Id, Name = group.Name } + }; + } + + //public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups().Select(it => new Group { Id = it.Id, Name = it.Name }).ToList(); } }