Interfaces UseCases, services

This commit is contained in:
1billy17 2024-10-21 13:38:39 +03:00
parent be284427eb
commit 6f930408b5
10 changed files with 102 additions and 24 deletions

View File

@ -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<UserLocalEntity> GetAllUser();
bool RemoveUserByGuid(Guid userGuid);
UserLocalEntity? UpdateUser(UserLocalEntity userUpdateLocalEntity);
UserLocalEntity? GetUserByGuid(Guid userGuid);
}
}

View File

@ -11,4 +11,8 @@
<Folder Include="Data\RemoteData\" /> <Folder Include="Data\RemoteData\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
</ItemGroup>
</Project> </Project>

View File

@ -8,17 +8,17 @@ using System.Threading.Tasks;
namespace Demo.Domain.UseCase 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<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
public Group UpdateGroupName(Group group) { 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) { public bool CreateGroup(Group group) {
GroupLocalEntity? groupLocalEntity = new GroupLocalEntity{Id = group.Id, Name = group.Name}; GroupLocalEntity? groupLocalEntity = new GroupLocalEntity{Id = group.Id, Name = group.Name};
@ -48,5 +53,12 @@ namespace Demo.Domain.UseCase
if (groupLocalEntity != null) return false; if (groupLocalEntity != null) return false;
return true; 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 };
}
} }
} }

View File

@ -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<Group> GetAllGroups();
bool RemoveGroupById(int groupID);
Group UpdateGroupName(Group group);
Group GetGroupById(int groupID);
bool CreateGroup(Group group);
}
}

View File

@ -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<Group> GetAllGroups();
List<User> GetAllUsers();
User GetUserByGuid(Guid userGuid);
bool RemoveUserByGuid(Guid userGuid);
User UpdateUser(User user);
}
}

View File

@ -8,21 +8,21 @@ using System.Threading.Tasks;
namespace Demo.Domain.UseCase namespace Demo.Domain.UseCase
{ {
public class UserUseCase public class UserUseCase:IUserUseCase
{ {
private readonly UserRepositoryImpl _repositoryUserImpl; private readonly IUserRepository _repositoryUserImpl;
private readonly GroupRepositoryImpl _repositoryGroupImpl; private readonly IGroupRepository _repositoryGroupImpl;
public UserUseCase(UserRepositoryImpl repositoryImpl, GroupRepositoryImpl repositoryGroupImpl) public UserUseCase(IUserRepository userRepository, IGroupRepository groupRepository)
{ {
_repositoryUserImpl = repositoryImpl; _repositoryUserImpl = userRepository;
_repositoryGroupImpl = repositoryGroupImpl; _repositoryGroupImpl = groupRepository;
} }
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList(); .Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUsers public List<User> GetAllUsers() => _repositoryUserImpl.GetAllUser()
.Join(_repositoryGroupImpl.GetAllGroups, .Join(_repositoryGroupImpl.GetAllGroup(),
user => user.GroupID, user => user.GroupID,
group => group.Id, group => group.Id,
(user, group) => (user, group) =>

View File

@ -2,10 +2,19 @@
using Demo.Data.Repository; using Demo.Data.Repository;
using Demo.Domain.UseCase; using Demo.Domain.UseCase;
using Demo.UI; using Demo.UI;
using Microsoft.Extensions.DependencyInjection;
GroupRepositoryImpl groupRepositoryImpl = new GroupRepositoryImpl(); IServiceCollection services = new ServiceCollection();
UserRepositoryImpl userRepositoryImpl = new UserRepositoryImpl();
UserUseCase userUseCase = new UserUseCase(userRepositoryImpl, groupRepositoryImpl);
GroupUseCase groupUseCase = new GroupUseCase(groupRepositoryImpl);
MainMenuUI mainMenuUI = new MainMenuUI(userUseCase, groupUseCase); services
.AddSingleton<IGroupRepository, GroupRepositoryImpl>()
.AddSingleton<IUserRepository, UserRepositoryImpl>()
.AddSingleton<IGroupUseCase, GroupUseCase>()
.AddSingleton<IUserUseCase, UserUseCase>()
.AddSingleton<UserConsoleUI>()
.AddSingleton<GroupConsoleUI>()
.AddSingleton<MainMenuUI>();
var serviceProvider = services.BuildServiceProvider();
MainMenuUI mainMenuUI = serviceProvider.GetService<MainMenuUI>();

View File

@ -7,9 +7,9 @@ namespace Demo.UI
{ {
public class GroupConsoleUI public class GroupConsoleUI
{ {
GroupUseCase _groupUseCase; IGroupUseCase _groupUseCase;
public GroupConsoleUI(GroupUseCase groupUseCase) { public GroupConsoleUI(IGroupUseCase groupUseCase) {
_groupUseCase = groupUseCase; _groupUseCase = groupUseCase;
} }

View File

@ -14,7 +14,7 @@ namespace Demo.UI
UserConsoleUI _userConsoleUI; UserConsoleUI _userConsoleUI;
GroupConsoleUI _groupConsoleUI; GroupConsoleUI _groupConsoleUI;
public MainMenuUI(UserUseCase userUseCase, GroupUseCase groupUseCase) { public MainMenuUI(IUserUseCase userUseCase, IGroupUseCase groupUseCase) {
_userConsoleUI = new UserConsoleUI(userUseCase); _userConsoleUI = new UserConsoleUI(userUseCase);
_groupConsoleUI = new GroupConsoleUI(groupUseCase); _groupConsoleUI = new GroupConsoleUI(groupUseCase);
DisplayMenu(); DisplayMenu();

View File

@ -10,8 +10,8 @@ namespace Demo.UI
{ {
public class UserConsoleUI public class UserConsoleUI
{ {
UserUseCase _userUseCase; IUserUseCase _userUseCase;
public UserConsoleUI(UserUseCase userUseCase) { public UserConsoleUI(IUserUseCase userUseCase) {
_userUseCase = userUseCase; _userUseCase = userUseCase;
} }