From 74e3d94f9a0fa56427898d733c5c0d45009eee40 Mon Sep 17 00:00:00 2001 From: adm Date: Wed, 16 Oct 2024 10:01:04 +0300 Subject: [PATCH] add group interface --- Demo/Data/LocalData/Entity/User.cs | 10 ++++++- Demo/Data/LocalData/LocalStaticData.cs | 14 +++++----- Demo/Data/Repository/GroupRepositoryImpl.cs | 29 +++++++++++++++++++-- Demo/Data/Repository/IGroupRepository.cs | 18 +++++++++++++ Demo/Data/Repository/UserRepositoryImpl.cs | 18 +++++++------ Demo/Demo.csproj | 4 +++ Demo/Domain/UseCase/UserUseCase.cs | 15 ++++++----- Demo/Program.cs | 2 ++ Demo/UI/MainMenu.cs | 18 ++++++++++++- Demo/UI/UserConsole.cs | 8 +++++- 10 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 Demo/Data/Repository/IGroupRepository.cs diff --git a/Demo/Data/LocalData/Entity/User.cs b/Demo/Data/LocalData/Entity/User.cs index 08479f2..5af4881 100644 --- a/Demo/Data/LocalData/Entity/User.cs +++ b/Demo/Data/LocalData/Entity/User.cs @@ -6,11 +6,19 @@ using System.Threading.Tasks; namespace Demo.domain.Models { - public class UserLocalEnity + public class UserLocalEnity : IEquatable { public required string FIO { get; set; } public Guid Guid { get; set; } public required int GroupID { get; set; } + + + + public bool Equals(UserLocalEnity? other) + { + if (other == null) return false; + return this.Guid.Equals(other.Guid); + } } } diff --git a/Demo/Data/LocalData/LocalStaticData.cs b/Demo/Data/LocalData/LocalStaticData.cs index 92c7f91..50e54f7 100644 --- a/Demo/Data/LocalData/LocalStaticData.cs +++ b/Demo/Data/LocalData/LocalStaticData.cs @@ -18,14 +18,14 @@ namespace Demo.Data.LocalData new GroupLocalEntity{ Id = 3, Name = "ИП1-23" }, }; - public static List users => new List + public static List users => new List { - new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio", GroupID = 1 }, - new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio1", GroupID = 2 }, - new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio2", GroupID = 3 }, - new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio3", GroupID = 1 }, - new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio4", GroupID = 2 }, - new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio5", GroupID = 3 }, + new UserLocalEnity{Guid=Guid.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), FIO = "RandomFio", GroupID = 1 }, + new UserLocalEnity{Guid=Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), FIO = "RandomFio1", GroupID = 2 }, + new UserLocalEnity{Guid=Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), FIO = "RandomFio2", GroupID = 3 }, + new UserLocalEnity{Guid=Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), FIO = "RandomFio3", GroupID = 1 }, + new UserLocalEnity{Guid=Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), FIO = "RandomFio4", GroupID = 2 }, + new UserLocalEnity{Guid=Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), FIO = "RandomFio5", GroupID = 3 }, }; } } diff --git a/Demo/Data/Repository/GroupRepositoryImpl.cs b/Demo/Data/Repository/GroupRepositoryImpl.cs index e5b9a29..a6653a3 100644 --- a/Demo/Data/Repository/GroupRepositoryImpl.cs +++ b/Demo/Data/Repository/GroupRepositoryImpl.cs @@ -8,8 +8,33 @@ using System.Threading.Tasks; namespace Demo.Data.Repository { - public class GroupRepositoryImpl + public class GroupRepositoryImpl:IGroupRepository { - public List GetAllGroups() => LocalStaticData.groups; + public bool AddGroup(GroupLocalEntity newGroup) + { + throw new NotImplementedException(); + } + + public List GetAllGroup() + { + throw new NotImplementedException(); + } + + public List GetAllGroups() => LocalStaticData.groups; + + public GroupLocalEntity GetGroupById(int groupID) + { + throw new NotImplementedException(); + } + + public bool RemoveGroupById(int groupID) + { + throw new NotImplementedException(); + } + + public bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup) + { + throw new NotImplementedException(); + } } } diff --git a/Demo/Data/Repository/IGroupRepository.cs b/Demo/Data/Repository/IGroupRepository.cs new file mode 100644 index 0000000..610efde --- /dev/null +++ b/Demo/Data/Repository/IGroupRepository.cs @@ -0,0 +1,18 @@ +using Demo.domain.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Demo.Data.Repository +{ + public interface IGroupRepository + { + List GetAllGroup(); + bool RemoveGroupById(int groupID); + bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup); + GroupLocalEntity GetGroupById(int groupID); + bool AddGroup(GroupLocalEntity newGroup); + } +} diff --git a/Demo/Data/Repository/UserRepositoryImpl.cs b/Demo/Data/Repository/UserRepositoryImpl.cs index 93fab95..bf9ae8c 100644 --- a/Demo/Data/Repository/UserRepositoryImpl.cs +++ b/Demo/Data/Repository/UserRepositoryImpl.cs @@ -10,21 +10,24 @@ namespace Demo.Data.Repository { public class UserRepositoryImpl { - public List GetAllUsers() => LocalStaticData.users; + public UserRepositoryImpl() { + + GetAllUsers = LocalStaticData.users; + } + public List GetAllUsers + { get; set; } public bool RemoveUserByGuid(Guid userGuid) { - UserLocalEnity? userLocal = LocalStaticData - .users + UserLocalEnity? userLocal = GetAllUsers .Where(x => x.Guid == userGuid).FirstOrDefault(); if (userLocal == null) return false; - return LocalStaticData.users.Remove(userLocal); + return GetAllUsers.Remove(userLocal); } public UserLocalEnity? GetUserByGuid(Guid userGuid) { - UserLocalEnity? userLocal = LocalStaticData - .users + UserLocalEnity? userLocal = GetAllUsers .Where(x => x.Guid == userGuid).FirstOrDefault(); if (userLocal == null) return null; @@ -32,8 +35,7 @@ namespace Demo.Data.Repository } public UserLocalEnity? UpdateUser(UserLocalEnity userUpdateLocalEnity) { - UserLocalEnity? userLocal = LocalStaticData - .users + UserLocalEnity? userLocal = GetAllUsers .Where(x => x.Guid == userUpdateLocalEnity.Guid).FirstOrDefault(); if (userLocal == null) return null; userLocal.FIO = userUpdateLocalEnity.FIO; diff --git a/Demo/Demo.csproj b/Demo/Demo.csproj index 1ea2759..213b500 100644 --- a/Demo/Demo.csproj +++ b/Demo/Demo.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/Demo/Domain/UseCase/UserUseCase.cs b/Demo/Domain/UseCase/UserUseCase.cs index f0a507d..f176969 100644 --- a/Demo/Domain/UseCase/UserUseCase.cs +++ b/Demo/Domain/UseCase/UserUseCase.cs @@ -10,19 +10,19 @@ namespace Demo.Domain.UseCase { public class UserUseCase { - private UserRepositoryImpl _repositoryUserImpl; - private GroupRepositoryImpl _repositoryGroupImpl; + private readonly UserRepositoryImpl _repositoryUserImpl; + private readonly IGroupRepository _repositoryGroupImpl; - public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) + public UserUseCase(UserRepositoryImpl repositoryImpl, IGroupRepository repositoryGroupImpl) { _repositoryUserImpl = repositoryImpl; _repositoryGroupImpl = repositoryGroupImpl; } - public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups() + private List GetAllGroups() => _repositoryGroupImpl.GetAllGroup() .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); - public List GetAllUsers => _repositoryUserImpl.GetAllUsers() - .Join(_repositoryGroupImpl.GetAllGroups(), + public List GetAllUsers() => _repositoryUserImpl.GetAllUsers + .Join(_repositoryGroupImpl.GetAllGroup(), user => user.GroupID, group => group.Id, (user, group) => @@ -31,6 +31,9 @@ namespace Demo.Domain.UseCase 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); diff --git a/Demo/Program.cs b/Demo/Program.cs index 9ebb621..303435b 100644 --- a/Demo/Program.cs +++ b/Demo/Program.cs @@ -3,6 +3,8 @@ using Demo.Data.Repository; using Demo.Domain.UseCase; using Demo.UI; + + GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); diff --git a/Demo/UI/MainMenu.cs b/Demo/UI/MainMenu.cs index f93611a..ce13884 100644 --- a/Demo/UI/MainMenu.cs +++ b/Demo/UI/MainMenu.cs @@ -14,7 +14,23 @@ namespace Demo.UI public MainMenuUI(UserUseCase userUseCase) { _userConsoleUI = new UserConsoleUI(userUseCase); - _userConsoleUI.DisplayAllUsers(); + DisplayMenu(); + + } + + private void DisplayMenu() { + while (true) + { + switch (Console.ReadLine()) + { + case "1": _userConsoleUI.DisplayAllUsers(); break; + case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; + + default: DisplayMenu(); + break; + } + + } } } diff --git a/Demo/UI/UserConsole.cs b/Demo/UI/UserConsole.cs index 81aadda..15ec296 100644 --- a/Demo/UI/UserConsole.cs +++ b/Demo/UI/UserConsole.cs @@ -14,10 +14,16 @@ namespace Demo.UI _userUseCase = userUseCase; } + public void RemoveUserByGuid(Guid guidUser) { + + string output = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален"; + Console.WriteLine(output); + } + public void DisplayAllUsers() { StringBuilder userOutput = new StringBuilder(); - foreach (var user in _userUseCase.GetAllUsers) + foreach (var user in _userUseCase.GetAllUsers()) { userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.Group.Name}"); }