add group interface

This commit is contained in:
adm 2024-10-16 10:01:04 +03:00
parent e1ec08575e
commit 74e3d94f9a
10 changed files with 110 additions and 26 deletions

View File

@ -6,11 +6,19 @@ using System.Threading.Tasks;
namespace Demo.domain.Models
{
public class UserLocalEnity
public class UserLocalEnity : IEquatable<UserLocalEnity>
{
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);
}
}
}

View File

@ -18,14 +18,14 @@ namespace Demo.Data.LocalData
new GroupLocalEntity{ Id = 3, Name = "ИП1-23" },
};
public static List<UserLocalEnity> users => new List<UserLocalEnity>
public static List<UserLocalEnity> users => new List<UserLocalEnity>
{
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 },
};
}
}

View File

@ -8,8 +8,33 @@ using System.Threading.Tasks;
namespace Demo.Data.Repository
{
public class GroupRepositoryImpl
public class GroupRepositoryImpl:IGroupRepository
{
public List<GroupLocalEntity> GetAllGroups() => LocalStaticData.groups;
public bool AddGroup(GroupLocalEntity newGroup)
{
throw new NotImplementedException();
}
public List<GroupLocalEntity> GetAllGroup()
{
throw new NotImplementedException();
}
public List<GroupLocalEntity> 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();
}
}
}

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.Data.Repository
{
public interface IGroupRepository
{
List<GroupLocalEntity> GetAllGroup();
bool RemoveGroupById(int groupID);
bool UpdateGroupById(int groupID, GroupLocalEntity updatedGroup);
GroupLocalEntity GetGroupById(int groupID);
bool AddGroup(GroupLocalEntity newGroup);
}
}

View File

@ -10,21 +10,24 @@ namespace Demo.Data.Repository
{
public class UserRepositoryImpl
{
public List<UserLocalEnity> GetAllUsers() => LocalStaticData.users;
public UserRepositoryImpl() {
GetAllUsers = LocalStaticData.users;
}
public List<UserLocalEnity> 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;

View File

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

View File

@ -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<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
.Select(it => new Group { Id = it.Id, Name = it.Name}).ToList();
public List<User> GetAllUsers => _repositoryUserImpl.GetAllUsers()
.Join(_repositoryGroupImpl.GetAllGroups(),
public List<User> 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);

View File

@ -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);

View File

@ -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;
}
}
}
}

View File

@ -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}");
}