From 63b8e26c16d6e6dfe59b6773eeabd899e4e6e4b5 Mon Sep 17 00:00:00 2001 From: Nastya Date: Sun, 20 Oct 2024 00:39:57 +0300 Subject: [PATCH] final --- Demo/Domain/UseCase/GroupUseCase.cs | 51 ++++++++++++++++++++++++++ Demo/Domain/UseCase/UserUseCase.cs | 51 +++++++++++++++++--------- Demo/UI/GroupConsole.cs | 44 +++++++++++++++++++++++ Demo/UI/MainMenu.cs | 56 ++++++++++++++++++++++------- Demo/UI/UserConsole.cs | 32 +++++++++++++---- 5 files changed, 199 insertions(+), 35 deletions(-) create mode 100644 Demo/Domain/UseCase/GroupUseCase.cs create mode 100644 Demo/UI/GroupConsole.cs diff --git a/Demo/Domain/UseCase/GroupUseCase.cs b/Demo/Domain/UseCase/GroupUseCase.cs new file mode 100644 index 0000000..4fd867a --- /dev/null +++ b/Demo/Domain/UseCase/GroupUseCase.cs @@ -0,0 +1,51 @@ +using Demo.Data.LocalData; +using Demo.Data.Repository; +using Demo.domain.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Demo.Domain.UseCase +{ + public class GroupUseCase + { + private List _groups = LocalStaticData.groups; + + public List GetAllGroups() => _groups; + + public void AddGroup(GroupLocalEntity group) + { + group.Id = _groups.Any() ? _groups.Max(g => g.Id) + 1 : 1; + _groups.Add(group); + } + + public void UpdateGroup(GroupLocalEntity group) + { + var existingGroup = _groups.FirstOrDefault(g => g.Id == group.Id); + if (existingGroup != null) + { + existingGroup.Name = group.Name; + } + } + public void RenameGroup(int groupId, string newName) + { + if (string.IsNullOrWhiteSpace(newName)) + { + throw new ArgumentException("Новое имя группы не может быть пустым", nameof(newName)); + } + + var existingGroup = _groups.FirstOrDefault(g => g.Id == groupId); + if (existingGroup == null) + { + throw new KeyNotFoundException($"Группа с ID {groupId} не найдена."); + } + existingGroup.Name = newName; + } + + } +} + + diff --git a/Demo/Domain/UseCase/UserUseCase.cs b/Demo/Domain/UseCase/UserUseCase.cs index f1f44e1..9ddc215 100644 --- a/Demo/Domain/UseCase/UserUseCase.cs +++ b/Demo/Domain/UseCase/UserUseCase.cs @@ -19,28 +19,47 @@ 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); + public bool RemoveUserByGuid(Guid userGuid) + { + return _repositoryUserImpl.RemoveUserByGuid(userGuid); } - public User UpdateUser(User user) { + 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}; + return new User { FIO = user.FIO, Guid = user.Guid, Group = group }; + } + public User FindUserByGuid(Guid userGuid) + { + Dictionary users = new Dictionary(); + + if (users.ContainsKey(userGuid)) + { + return users[userGuid]; + } + else + { + return null; + } + } } -} +} \ No newline at end of file diff --git a/Demo/UI/GroupConsole.cs b/Demo/UI/GroupConsole.cs new file mode 100644 index 0000000..772f2c6 --- /dev/null +++ b/Demo/UI/GroupConsole.cs @@ -0,0 +1,44 @@ +using Demo.domain.Models; +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 GroupConsole + { + + + internal class GroupConsoleUI + { + GroupUseCase _groupUseCase; + + public GroupConsoleUI(GroupUseCase groupUseCase) + { + _groupUseCase = groupUseCase; + } + + + public void AllGroups() + { + foreach (var Group in _groupUseCase.GetAllGroups()) + { + Console.WriteLine($"{Group.Id}\t{Group.Name}"); + } + } + + public void UpdateGroup(int groupId, GroupLocalEntity updatedGroup) + { + if (updatedGroup == null) + { + throw new ArgumentNullException(nameof(updatedGroup), "Обновленная группа не может быть null"); + } + + + } + } + } +} diff --git a/Demo/UI/MainMenu.cs b/Demo/UI/MainMenu.cs index ce13884..4f867ef 100644 --- a/Demo/UI/MainMenu.cs +++ b/Demo/UI/MainMenu.cs @@ -9,29 +9,59 @@ namespace Demo.UI { public class MainMenuUI { - - UserConsoleUI _userConsoleUI; + private UserConsoleUI _userConsoleUI; - public MainMenuUI(UserUseCase userUseCase) { - _userConsoleUI = new UserConsoleUI(userUseCase); + public MainMenuUI(UserUseCase userUseCase) + { + _userConsoleUI = new UserConsoleUI(userUseCase); DisplayMenu(); - } - private void DisplayMenu() { + private void DisplayMenu() + { while (true) { - switch (Console.ReadLine()) - { - case "1": _userConsoleUI.DisplayAllUsers(); break; - case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; + Console.Clear(); + Console.WriteLine("Главное меню:"); + Console.WriteLine("1. Показать всех пользователей"); + Console.WriteLine("2. Удалить пользователя по GUID"); + Console.WriteLine("0. Выход"); - default: DisplayMenu(); + string choice = Console.ReadLine(); + + switch (choice) + { + case "1": + _userConsoleUI.DisplayAllUsers(); + break; + case "2": + RemoveUser(); + break; + case "0": + return; + default: + Console.WriteLine("Неверный выбор, попробуйте снова."); break; } + Console.WriteLine("Нажмите любую клавишу для продолжения..."); + Console.ReadKey(); + } + } + + private void RemoveUser() + { + Console.Write("Введите GUID пользователя для удаления: "); + string input = Console.ReadLine(); + + if (Guid.TryParse(input, out Guid userGuid)) + { + _userConsoleUI.RemoveUserByGuid(userGuid); + } + else + { + Console.WriteLine("Неверный формат GUID. Пожалуйста, попробуйте еще раз."); } } - } -} +} \ No newline at end of file diff --git a/Demo/UI/UserConsole.cs b/Demo/UI/UserConsole.cs index 15ec296..ac1e979 100644 --- a/Demo/UI/UserConsole.cs +++ b/Demo/UI/UserConsole.cs @@ -1,4 +1,5 @@ -using Demo.Domain.UseCase; +using Demo.domain.Models; +using Demo.Domain.UseCase; using System; using System.Collections.Generic; using System.Linq; @@ -7,17 +8,20 @@ using System.Threading.Tasks; namespace Demo.UI { + public class UserConsoleUI { UserUseCase _userUseCase; - public UserConsoleUI(UserUseCase userUseCase) { + public UserConsoleUI(UserUseCase userUseCase) + { _userUseCase = userUseCase; } - public void RemoveUserByGuid(Guid guidUser) { + public void RemoveUserByGuid(Guid guidUser) + { - string output = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален"; - Console.WriteLine(output); + string del = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален"; + Console.WriteLine(del); } public void DisplayAllUsers() @@ -29,5 +33,21 @@ namespace Demo.UI } Console.WriteLine(userOutput); } + + public void UpdateUserGuid(Guid guidUser) + { + var user = _userUseCase.FindUserByGuid(guidUser); + Console.WriteLine($"Текущие данные: {user.FIO}, {user.Group.Name}"); + Console.Write("Введите новое ФИО: "); + string newFIO = Console.ReadLine(); + user.FIO = newFIO; + _userUseCase.UpdateUser(user); + } + + public void GetUserByGuid(Guid guidUser) + { + var user = _userUseCase.FindUserByGuid(guidUser); + Console.WriteLine($"Пользователь найден: {user.Guid}, {user.FIO}, {user.Group.Name}"); + } } -} +} \ No newline at end of file