From 7b7b49d3e1d1bfb4c1f925824dd95953123cde7f Mon Sep 17 00:00:00 2001 From: Diana Date: Fri, 18 Oct 2024 20:54:42 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=82=D0=B0=D0=B2=D1=8C?= =?UTF-8?q?=D1=82=D0=B5=204=20=D0=BF=D0=BE=D0=B6=D0=B0=D0=BB=D1=83=D0=B9?= =?UTF-8?q?=D1=81=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Demo/Data/Repository/GroupRepositoryImpl.cs | 33 ++++++++++++ Demo/Domain/UseCase/GroupUseCase.cs | 58 +++++++++++++++++++++ Demo/Domain/UseCase/UserUseCase.cs | 1 + Demo/Program.cs | 4 +- Demo/UI/GroupConsole.cs | 46 ++++++++++++++++ Demo/UI/MainMenu.cs | 10 ++-- Demo/UI/UserConsole.cs | 23 +++++++- 7 files changed, 169 insertions(+), 6 deletions(-) create mode 100644 Demo/Domain/UseCase/GroupUseCase.cs create mode 100644 Demo/UI/GroupConsole.cs diff --git a/Demo/Data/Repository/GroupRepositoryImpl.cs b/Demo/Data/Repository/GroupRepositoryImpl.cs index e5b9a29..7137ec4 100644 --- a/Demo/Data/Repository/GroupRepositoryImpl.cs +++ b/Demo/Data/Repository/GroupRepositoryImpl.cs @@ -11,5 +11,38 @@ namespace Demo.Data.Repository public class GroupRepositoryImpl { public List GetAllGroups() => LocalStaticData.groups; + + public List GetAllGroups + { get; set; } + + public bool RemoveGroupById(int id) + { + GroupLocalEntity? groupLocal = GetAllGroups + .Where(x => x.int == id).FirstOrDefault(); + if (groupLocal == null) return false; + + return GetAllGroup.Remove(groupLocal); + } + + public GroupLocalEntity? GetGroupByGuid(int id) + { + GroupLocalEntity? groupLocal = GetAllGroups + .Where(x => x.int == id).FirstOrDefault(); + if (groupLocal == null) return null; + + return groupLocal; + } + + public GroupLocalEntity? UpdateGroup(GroupLocalEntity groupUpdateLocalEnity) + { + GroupLocalEntity? groupLocal = GetAllGroups + .Where(x => x.int == groupUpdateLocalEnity.id).FirstOrDefault(); + if (groupLocal == null) return null; + groupLocal.Name = groupUpdateLocalEnity.Name; + return groupLocal; + + } } + + } diff --git a/Demo/Domain/UseCase/GroupUseCase.cs b/Demo/Domain/UseCase/GroupUseCase.cs new file mode 100644 index 0000000..66981c5 --- /dev/null +++ b/Demo/Domain/UseCase/GroupUseCase.cs @@ -0,0 +1,58 @@ +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 GroupUseCase + { + private UserRepositoryImpl _repositoryUserImpl; + private GroupRepositoryImpl _repositoryGroupImpl; + + public GroupUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) + { + _repositoryUserImpl = repositoryImpl; + _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 bool RemoveGroupId(int id) + { + return _repositoryGroupImpl.RemoveGroupById(id); + } + + public List GetGroupById1(int id) + { + return _repositoryGroupImpl.GetGroupByid(id); + } + + public Group UpdateGroup(Group group) + { + GroupLocalEnity groupLocalEnity = new GroupLocalEnity { Id = group.Id, Name = group.name }; + GroupLocalEnity? result = _repositoryGroupImpl.UpdateGroup(groupLocalEnity); + if (result == null) throw new Exception(""); + Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID); + if (group == null) throw new Exception(""); + return new Group { Id = group.Id, Name = group.name }; + } + } +} diff --git a/Demo/Domain/UseCase/UserUseCase.cs b/Demo/Domain/UseCase/UserUseCase.cs index f1f44e1..ed01c9a 100644 --- a/Demo/Domain/UseCase/UserUseCase.cs +++ b/Demo/Domain/UseCase/UserUseCase.cs @@ -34,6 +34,7 @@ namespace Demo.Domain.UseCase 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); diff --git a/Demo/Program.cs b/Demo/Program.cs index 9ebb621..7bd342b 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -5,6 +5,6 @@ using Demo.UI; GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); -UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); +UserUseCase UserUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); -MainMenuUI mainMenuUI = new MainMenuUI(userUseCase); \ No newline at end of file +MainMenuUI mainMenuUI = new MainMenuUI(UserUseCase); \ No newline at end of file diff --git a/Demo/UI/GroupConsole.cs b/Demo/UI/GroupConsole.cs new file mode 100644 index 0000000..d8252d0 --- /dev/null +++ b/Demo/UI/GroupConsole.cs @@ -0,0 +1,46 @@ +using Demo.Domain.UseCase; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Demo.UI +{ + public class GroupConsoleUI + { + GroupUseCase _groupUseCase; + public UserConsoleUI(GroupUseCase groupUseCase) + { + _groupUseCase = groupUseCase; + } + + public UpdateGroups() + { + string output = _groupUseCase.UpdateGroup("бла бла бла") ? "Группа обновлена" : "Группа не обновлена"; + Console.WriteLine(output); + } + + public void RemoveGroupByGuid(int id) + { + + string output = _groupUseCase.RemoveGroupByGuid(id) ? "Группа удалена" : "Группа не удалена"; + Console.WriteLine(output); + } + + public void GetGroupById(int id) + { + Console.WriteLine(GetGroupById1(id)); + } + + public void DisplayAllGroup() + { + StringBuilder userOutput = new StringBuilder(); + foreach (var user in _groupUseCase.GetAllGroup()) + { + userOutput.AppendLine($"{group.Id}\t{group.Name}"); + } + Console.WriteLine(userOutput); + } + } +} diff --git a/Demo/UI/MainMenu.cs b/Demo/UI/MainMenu.cs index ce13884..6f39a39 100644 --- a/Demo/UI/MainMenu.cs +++ b/Demo/UI/MainMenu.cs @@ -1,6 +1,8 @@ -using Demo.Domain.UseCase; +using Demo.domain.Models; +using Demo.Domain.UseCase; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -9,7 +11,7 @@ namespace Demo.UI { public class MainMenuUI { - + UserConsoleUI _userConsoleUI; public MainMenuUI(UserUseCase userUseCase) { @@ -18,6 +20,7 @@ namespace Demo.UI } + private void DisplayMenu() { while (true) { @@ -25,7 +28,8 @@ namespace Demo.UI { case "1": _userConsoleUI.DisplayAllUsers(); break; case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; - + case "3": _userConsoleUI.UpdateUser(Split(Console.ReadLine())); break; + case "4": _userConsoleUI.GetUserByGuid("614c0a23-5bd5-43ae-b48e-d5750afbc282"); break; default: DisplayMenu(); break; } diff --git a/Demo/UI/UserConsole.cs b/Demo/UI/UserConsole.cs index 15ec296..17abd8b 100644 --- a/Demo/UI/UserConsole.cs +++ b/Demo/UI/UserConsole.cs @@ -1,7 +1,10 @@ -using Demo.Domain.UseCase; +using Demo.domain.Models; +using Demo.Domain.UseCase; using System; using System.Collections.Generic; using System.Linq; +using System.Net.Http.Headers; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -29,5 +32,23 @@ namespace Demo.UI } Console.WriteLine(userOutput); } + + public void UpdateUser(User user) + { + try + { + User updatedUser = _userUseCase.UpdateUser(user); + Console.WriteLine($"Пользователь обновлен: {updatedUser.FIO}, Группа: {updatedUser.Group.Name}"); + } + catch (Exception ex) + { + Console.WriteLine($"Произошла ошибка: {ex.Message}"); + } + } + + public void GetUserByGuid(Guid userGuid) { + var output = _userUseCase.GetUserByGuid(userGuid); + Console.WriteLine(output); + } } }