From 6f930408b5a49835560062aaf2a4bd504751602d Mon Sep 17 00:00:00 2001 From: 1billy17 Date: Mon, 21 Oct 2024 13:38:39 +0300 Subject: [PATCH] Interfaces UseCases, services --- Data/Repository/IUserRepository.cs | 17 +++++++++++++++++ Demo.csproj | 4 ++++ Domain/UseCase/GroupUseCase.cs | 22 +++++++++++++++++----- Domain/UseCase/IGroupUseCase.cs | 18 ++++++++++++++++++ Domain/UseCase/IUserUseCase.cs | 18 ++++++++++++++++++ Domain/UseCase/UserUseCase.cs | 18 +++++++++--------- Program.cs | 19 ++++++++++++++----- UI/GroupConsole.cs | 4 ++-- UI/MainMenu.cs | 2 +- UI/UserConsole.cs | 4 ++-- 10 files changed, 102 insertions(+), 24 deletions(-) create mode 100644 Domain/UseCase/IGroupUseCase.cs create mode 100644 Domain/UseCase/IUserUseCase.cs diff --git a/Data/Repository/IUserRepository.cs b/Data/Repository/IUserRepository.cs index e69de29..9560b00 100644 --- a/Data/Repository/IUserRepository.cs +++ b/Data/Repository/IUserRepository.cs @@ -0,0 +1,17 @@ +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 IUserRepository + { + List GetAllUser(); + bool RemoveUserByGuid(Guid userGuid); + UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEntity); + UserLocalEntity? GetUserByGuid(Guid userGuid); + } +} \ No newline at end of file diff --git a/Demo.csproj b/Demo.csproj index 6f10314..28a3ec0 100644 --- a/Demo.csproj +++ b/Demo.csproj @@ -11,4 +11,8 @@ + + + + \ No newline at end of file diff --git a/Domain/UseCase/GroupUseCase.cs b/Domain/UseCase/GroupUseCase.cs index bc99c1f..dedaadc 100644 --- a/Domain/UseCase/GroupUseCase.cs +++ b/Domain/UseCase/GroupUseCase.cs @@ -8,17 +8,17 @@ using System.Threading.Tasks; namespace Demo.Domain.UseCase { - public class GroupUseCase + public class GroupUseCase:IGroupUseCase { - private GroupRepositoryImpl _repositoryGroupImpl; + private IGroupRepository _repositoryGroupImpl; - public GroupUseCase(GroupRepositoryImpl repositoryGroupImpl) + public GroupUseCase(IGroupRepository groupRepository) { - _repositoryGroupImpl = repositoryGroupImpl; + _repositoryGroupImpl = groupRepository; } - public List GetAllGroups() => _repositoryGroupImpl.GetAllGroups + public List GetAllGroups() => _repositoryGroupImpl.GetAllGroup() .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); public Group UpdateGroupName(Group group) { @@ -41,6 +41,11 @@ namespace Demo.Domain.UseCase } + public bool RemoveGroupById(int groupId) { + + return _repositoryGroupImpl.RemoveGroupById(groupId); + } + public bool CreateGroup(Group group) { GroupLocalEntity? groupLocalEntity = new GroupLocalEntity{Id = group.Id, Name = group.Name}; @@ -48,5 +53,12 @@ namespace Demo.Domain.UseCase if (groupLocalEntity != null) return false; return true; } + + public Group GetGroupById(int groupId) { + + GroupLocalEntity? groupLocalEntity = _repositoryGroupImpl.GetGroupById(groupId); + if (groupLocalEntity == null) throw new Exception(""); + return new Group { Id = groupLocalEntity.Id, Name = groupLocalEntity.Name }; + } } } diff --git a/Domain/UseCase/IGroupUseCase.cs b/Domain/UseCase/IGroupUseCase.cs new file mode 100644 index 0000000..bf53584 --- /dev/null +++ b/Domain/UseCase/IGroupUseCase.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.Domain.UseCase +{ + public interface IGroupUseCase + { + List GetAllGroups(); + bool RemoveGroupById(int groupID); + Group UpdateGroupName(Group group); + Group GetGroupById(int groupID); + bool CreateGroup(Group group); + } +} \ No newline at end of file diff --git a/Domain/UseCase/IUserUseCase.cs b/Domain/UseCase/IUserUseCase.cs new file mode 100644 index 0000000..6dd57e1 --- /dev/null +++ b/Domain/UseCase/IUserUseCase.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.Domain.UseCase +{ + public interface IUserUseCase + { + List GetAllGroups(); + List GetAllUsers(); + User GetUserByGuid(Guid userGuid); + bool RemoveUserByGuid(Guid userGuid); + User UpdateUser(User user); + } +} \ No newline at end of file diff --git a/Domain/UseCase/UserUseCase.cs b/Domain/UseCase/UserUseCase.cs index 89b23b7..91591cc 100644 --- a/Domain/UseCase/UserUseCase.cs +++ b/Domain/UseCase/UserUseCase.cs @@ -8,21 +8,21 @@ using System.Threading.Tasks; namespace Demo.Domain.UseCase { - public class UserUseCase + public class UserUseCase:IUserUseCase { - private readonly UserRepositoryImpl _repositoryUserImpl; - private readonly GroupRepositoryImpl _repositoryGroupImpl; + private readonly IUserRepository _repositoryUserImpl; + private readonly IGroupRepository _repositoryGroupImpl; - public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) + public UserUseCase(IUserRepository userRepository, IGroupRepository groupRepository) { - _repositoryUserImpl = repositoryImpl; - _repositoryGroupImpl = repositoryGroupImpl; + _repositoryUserImpl = userRepository; + _repositoryGroupImpl = groupRepository; } - private List GetAllGroups() => _repositoryGroupImpl.GetAllGroups + public 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.GetAllUser() + .Join(_repositoryGroupImpl.GetAllGroup(), user => user.GroupID, group => group.Id, (user, group) => diff --git a/Program.cs b/Program.cs index 4bcac7c..a97ffea 100644 --- a/Program.cs +++ b/Program.cs @@ -2,10 +2,19 @@ using Demo.Data.Repository; using Demo.Domain.UseCase; using Demo.UI; +using Microsoft.Extensions.DependencyInjection; -GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); -UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl(); -UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl); -GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl); +IServiceCollection services = new ServiceCollection(); -MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase); +services + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton(); + +var serviceProvider = services.BuildServiceProvider(); + +MainMenuUI mainMenuUI = serviceProvider.GetService(); diff --git a/UI/GroupConsole.cs b/UI/GroupConsole.cs index bb2cf12..3d4bafd 100644 --- a/UI/GroupConsole.cs +++ b/UI/GroupConsole.cs @@ -7,9 +7,9 @@ namespace Demo.UI { public class GroupConsoleUI { - GroupUseCase _groupUseCase; + IGroupUseCase _groupUseCase; - public GroupConsoleUI(GroupUseCase groupUseCase) { + public GroupConsoleUI(IGroupUseCase groupUseCase) { _groupUseCase = groupUseCase; } diff --git a/UI/MainMenu.cs b/UI/MainMenu.cs index 16e9982..a1543d1 100644 --- a/UI/MainMenu.cs +++ b/UI/MainMenu.cs @@ -14,7 +14,7 @@ namespace Demo.UI UserConsoleUI _userConsoleUI; GroupConsoleUI _groupConsoleUI; - public MainMenuUI(UserUseCase userUseCase, GroupUseCase groupUseCase) { + public MainMenuUI(IUserUseCase userUseCase, IGroupUseCase groupUseCase) { _userConsoleUI = new UserConsoleUI(userUseCase); _groupConsoleUI = new GroupConsoleUI(groupUseCase); DisplayMenu(); diff --git a/UI/UserConsole.cs b/UI/UserConsole.cs index 43dc3df..e895507 100644 --- a/UI/UserConsole.cs +++ b/UI/UserConsole.cs @@ -10,8 +10,8 @@ namespace Demo.UI { public class UserConsoleUI { - UserUseCase _userUseCase; - public UserConsoleUI(UserUseCase userUseCase) { + IUserUseCase _userUseCase; + public UserConsoleUI(IUserUseCase userUseCase) { _userUseCase = userUseCase; }