From 7603d4fbad2a24c8f119cdb70870e3f5ee3846a3 Mon Sep 17 00:00:00 2001 From: 1billy17 Date: Thu, 17 Oct 2024 15:35:14 +0300 Subject: [PATCH] UI --- .DS_Store | Bin 0 -> 6148 bytes Data/Repository/GroupRepositoryImpl.cs | 9 +++-- Data/Repository/UserRepositoryImpl.cs | 12 +++---- Domain/Models/Group.cs | 5 +++ Domain/Models/User.cs | 6 ++++ Domain/UseCase/GroupUseCase.cs | 32 ++++++++++------- Program.cs | 2 +- UI/GroupConsole.cs | 46 +++++++++++++++++++++++++ UI/MainMenu.cs | 14 ++++++-- UI/UserConsole.cs | 23 +++++++++++++ 10 files changed, 121 insertions(+), 28 deletions(-) create mode 100644 .DS_Store create mode 100644 UI/GroupConsole.cs diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9ac86c500094aff17ad2f64321dab014cf810d5f GIT binary patch literal 6148 zcmeHLy-EW?5T5l84gO#Z78VL8mH|m>3(s&Cf`t%l)Z!8YF_QCA#3mwK>kEjL&)`ew z3-~7b0?zDClHFWlVId+ju={QH+u3jT3wN6h0I1q+s|rvAfC3gme-W!GK|GaQ%yG{K zqM|vv?P~opO?tVJ(jg9r1Aoy0dUk!-g*J4c?>@he_coWLQPk|FP3$WySKe#o`0c>u zTi^1%E9>3h#h3~nG*R`&RV8A`*qVC}AMG`x$$3dSR?M@Go}Lu<89@X3&~O#k_mLiP zw&va0#p}*+pPXx}PUBtOn3U&HH-!YXbGt2lPV%i%oTYLL_aJ)72Tj$xTdDhi?A-y>`g(FvOfzBA+Z)>f>^Aqorx$!qU$+v6yGRYhfH7pu3?hS} zOe&&DReZ%zCLQ|&9v3oZ22DCBzI-TFR(wNIS{>&P^l(t2L8-+7alm!JGarj|{vXc1 z|GP=@NgNOd{*?pDkLpnkx5Q^_ZgX_j23TS&6f`a~s8g`w<5)N7D5n1vbRZW{12JUG S3}O$4{1DJKNF@&Zssmp&7NN2L literal 0 HcmV?d00001 diff --git a/Data/Repository/GroupRepositoryImpl.cs b/Data/Repository/GroupRepositoryImpl.cs index cb681ec..596d33d 100644 --- a/Data/Repository/GroupRepositoryImpl.cs +++ b/Data/Repository/GroupRepositoryImpl.cs @@ -19,11 +19,10 @@ namespace Demo.Data.Repository public GroupLocalEntity? UpdateGroupName(GroupLocalEntity groupUpdateLocalEntity) { - GroupLocalEntity? groupLocal = GetAllGroups - .Where(x => x.Id == groupUpdateLocalEntity.Id).FirstOrDefault(); - if (groupLocal == null) return null; - groupLocal.Name = groupUpdateLocalEntity.Name; - return groupLocal; + int index = GetAllGroups.FindIndex(x => x.Id == groupUpdateLocalEntity.Id); + if (index == -1) return null; + GetAllGroups[index].Name = groupUpdateLocalEntity.Name; + return GetAllGroups[index]; } public GroupLocalEntity? CreateGroup(GroupLocalEntity groupCreateLocalEntity) { diff --git a/Data/Repository/UserRepositoryImpl.cs b/Data/Repository/UserRepositoryImpl.cs index cf1f060..5a5b9db 100644 --- a/Data/Repository/UserRepositoryImpl.cs +++ b/Data/Repository/UserRepositoryImpl.cs @@ -35,13 +35,11 @@ namespace Demo.Data.Repository } public UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEntity) { - UserLocalEntity? userLocal = GetAllUsers - .Where(x => x.Guid == userUpdateLocalEntity.Guid).FirstOrDefault(); - if (userLocal == null) return null; - userLocal.FIO = userUpdateLocalEntity.FIO; - userLocal.GroupID = userUpdateLocalEntity.GroupID; - return userLocal; - + int index = GetAllUsers.FindIndex(x => x.Guid == userUpdateLocalEntity.Guid); + if (index == -1) return null; + GetAllUsers[index].FIO = userUpdateLocalEntity.FIO; + GetAllUsers[index].GroupID = userUpdateLocalEntity.GroupID; + return GetAllUsers[index]; } } } \ No newline at end of file diff --git a/Domain/Models/Group.cs b/Domain/Models/Group.cs index 8cb9fa6..29c2f56 100644 --- a/Domain/Models/Group.cs +++ b/Domain/Models/Group.cs @@ -10,5 +10,10 @@ namespace Demo.domain.Models { public required int Id { get; set; } public required string Name { get; set; } + + public static Group Parse(string input){ + string[] words = input.Split(" "); + return new Group{Id = Convert.ToInt32(words[0]), Name = words[1]}; + } } } \ No newline at end of file diff --git a/Domain/Models/User.cs b/Domain/Models/User.cs index c71a74c..aaaa75c 100644 --- a/Domain/Models/User.cs +++ b/Domain/Models/User.cs @@ -12,5 +12,11 @@ namespace Demo.domain.Models public Guid Guid { get; set; } public required Group Group { get; set; } + + + public static User Parse(string input){ + string[] words = input.Split(" "); + return new User{FIO = words[0], Guid = Guid.Parse(words[1]), Group = new Group{Id = Convert.ToInt32(words[2]), Name = words[3]}}; + } } } \ No newline at end of file diff --git a/Domain/UseCase/GroupUseCase.cs b/Domain/UseCase/GroupUseCase.cs index a70d84d..bc99c1f 100644 --- a/Domain/UseCase/GroupUseCase.cs +++ b/Domain/UseCase/GroupUseCase.cs @@ -23,22 +23,30 @@ namespace Demo.Domain.UseCase public Group UpdateGroupName(Group group) { - GroupLocalEntity? groupLocalEntity = new GroupLocalEntity { Id = group.Id, Name = group.Name }; - GroupLocalEntity? updatedGroupEntity = _repositoryGroupImpl.UpdateGroupName(groupLocalEntity); - if (updatedGroupEntity == null) throw new Exception(""); - - return new Group { Id = updatedGroupEntity.Id, Name = updatedGroupEntity.Name}; + GroupLocalEntity groupLocalEntity = new GroupLocalEntity + { + Id = group.Id, + Name = group.Name + }; + GroupLocalEntity? result = _repositoryGroupImpl.UpdateGroupName(groupLocalEntity); + if (result == null) + { + throw new Exception(""); + } + return new Group + { + Id = result.Id, + Name = result.Name + }; } - public Group CreateGroup(Group group) { + public bool CreateGroup(Group group) { - GroupLocalEntity? groupLocalEntity = new GroupLocalEntity { Id = group.Id, Name = group.Name}; - GroupLocalEntity? createdGroupEntity = _repositoryGroupImpl.CreateGroup(groupLocalEntity); - - if (createdGroupEntity == null) throw new Exception(""); - - return new Group { Id = createdGroupEntity.Id, Name = createdGroupEntity.Name}; + GroupLocalEntity? groupLocalEntity = new GroupLocalEntity{Id = group.Id, Name = group.Name}; + _repositoryGroupImpl.CreateGroup(groupLocalEntity); + if (groupLocalEntity != null) return false; + return true; } } } diff --git a/Program.cs b/Program.cs index d1f245a..4bcac7c 100644 --- a/Program.cs +++ b/Program.cs @@ -8,4 +8,4 @@ UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl); -MainMenuUI mainMenuUI = new MainMenuUI(userUseCase); \ No newline at end of file +MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase); diff --git a/UI/GroupConsole.cs b/UI/GroupConsole.cs new file mode 100644 index 0000000..bb2cf12 --- /dev/null +++ b/UI/GroupConsole.cs @@ -0,0 +1,46 @@ +using Demo.Domain.UseCase; +using Demo.domain.Models; +using System.Text; + + +namespace Demo.UI +{ + public class GroupConsoleUI + { + GroupUseCase _groupUseCase; + + public GroupConsoleUI(GroupUseCase groupUseCase) { + _groupUseCase = groupUseCase; + } + + public void UpdateGroupName(Group group) + { + try + { + Group output = _groupUseCase.UpdateGroupName(group); + StringBuilder groupOutput = new StringBuilder(); + groupOutput.AppendLine($"Обновленная группа: {output.Id}\t{output.Name}"); + Console.WriteLine(groupOutput); + } + catch (Exception ex) + { + Console.WriteLine($"Ошибка при обновлении группы: {ex.Message}"); + } + } + + public void CreateNewGroup(Group group){ + string output = _groupUseCase.CreateGroup(group) ? "Пользователь создан" : "Пользователь не создан"; + } + + public void DisplayAllGroups() + { + StringBuilder groupOutput = new StringBuilder(); + foreach (var group in _groupUseCase.GetAllGroups()) + { + groupOutput.AppendLine($"{group.Id}\t {group.Name}\t"); + } + Console.WriteLine(groupOutput); + } + } + +} diff --git a/UI/MainMenu.cs b/UI/MainMenu.cs index 2bbf02f..16e9982 100644 --- a/UI/MainMenu.cs +++ b/UI/MainMenu.cs @@ -1,4 +1,5 @@ using Demo.Domain.UseCase; +using Demo.domain.Models; using System; using System.Collections.Generic; using System.Linq; @@ -9,11 +10,13 @@ namespace Demo.UI { public class MainMenuUI { - - UserConsoleUI _userConsoleUI; - public MainMenuUI(UserUseCase userUseCase) { + UserConsoleUI _userConsoleUI; + GroupConsoleUI _groupConsoleUI; + + public MainMenuUI(UserUseCase userUseCase, GroupUseCase groupUseCase) { _userConsoleUI = new UserConsoleUI(userUseCase); + _groupConsoleUI = new GroupConsoleUI(groupUseCase); DisplayMenu(); } @@ -25,6 +28,11 @@ namespace Demo.UI { case "1": _userConsoleUI.DisplayAllUsers(); break; case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; + case "3": _userConsoleUI.UpdateUser(User.Parse(Console.ReadLine())); break; + case "4": _userConsoleUI.GetUserByGuid(Guid.Parse(Console.ReadLine())); break; + case "5": _groupConsoleUI.DisplayAllGroups(); break; + case "6": _groupConsoleUI.CreateNewGroup(Group.Parse(Console.ReadLine())); break; + case "7": _groupConsoleUI.UpdateGroupName(Group.Parse(Console.ReadLine())); break; default: DisplayMenu(); break; diff --git a/UI/UserConsole.cs b/UI/UserConsole.cs index f032863..43dc3df 100644 --- a/UI/UserConsole.cs +++ b/UI/UserConsole.cs @@ -1,4 +1,5 @@ using Demo.Domain.UseCase; +using Demo.domain.Models; using System; using System.Collections.Generic; using System.Linq; @@ -29,5 +30,27 @@ namespace Demo.UI } Console.WriteLine(userOutput); } + + public void UpdateUser(User user) + { + try + { + User output = _userUseCase.UpdateUser(user); + StringBuilder userOutput = new StringBuilder(); + userOutput.AppendLine($"Обновленный пользователь: {output.Guid}\t{output.FIO}\t{output.Group.Name}"); + Console.WriteLine(userOutput); + } + catch (Exception ex) + { + Console.WriteLine($"Ошибка при обновлении пользователя: {ex.Message}"); + } + } + + public void GetUserByGuid(Guid guid){ + User output = _userUseCase.GetUserByGuid(guid); + StringBuilder userOutput = new StringBuilder(); + userOutput.AppendLine($"{output.Guid}\t{output.FIO}\t{output.Group.Name}"); + Console.WriteLine(userOutput); + } } } \ No newline at end of file