change static list

This commit is contained in:
adm 2024-10-16 10:01:04 +03:00
parent e1ec08575e
commit 6012168a59
6 changed files with 54 additions and 19 deletions

View File

@ -6,11 +6,19 @@ using System.Threading.Tasks;
namespace Demo.domain.Models namespace Demo.domain.Models
{ {
public class UserLocalEnity public class UserLocalEnity : IEquatable<UserLocalEnity>
{ {
public required string FIO { get; set; } public required string FIO { get; set; }
public Guid Guid { get; set; } public Guid Guid { get; set; }
public required int GroupID { 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

@ -20,12 +20,12 @@ namespace Demo.Data.LocalData
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.Parse("e6b9964d-ea9f-420a-84b9-af9633bbfab9"), FIO = "RandomFio", GroupID = 1 },
new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio1", GroupID = 2 }, new UserLocalEnity{Guid=Guid.Parse("8388d931-5bef-41be-a152-78f1aca980ed"), FIO = "RandomFio1", GroupID = 2 },
new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio2", GroupID = 3 }, new UserLocalEnity{Guid=Guid.Parse("ed174548-49ed-4503-a902-c970cbf27173"), FIO = "RandomFio2", GroupID = 3 },
new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio3", GroupID = 1 }, new UserLocalEnity{Guid=Guid.Parse("614c0a23-5bd5-43ae-b48e-d5750afbc282"), FIO = "RandomFio3", GroupID = 1 },
new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio4", GroupID = 2 }, new UserLocalEnity{Guid=Guid.Parse("efcc1473-c116-4244-b3f7-f2341a5c3003"), FIO = "RandomFio4", GroupID = 2 },
new UserLocalEnity{Guid=Guid.NewGuid(), FIO = "RandomFio5", GroupID = 3 }, new UserLocalEnity{Guid=Guid.Parse("60640fb3-ace2-4cad-81d5-a0a58bc2dbbd"), FIO = "RandomFio5", GroupID = 3 },
}; };
} }
} }

View File

@ -10,21 +10,24 @@ namespace Demo.Data.Repository
{ {
public class UserRepositoryImpl 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) public bool RemoveUserByGuid(Guid userGuid)
{ {
UserLocalEnity? userLocal = LocalStaticData UserLocalEnity? userLocal = GetAllUsers
.users
.Where(x => x.Guid == userGuid).FirstOrDefault(); .Where(x => x.Guid == userGuid).FirstOrDefault();
if (userLocal == null) return false; if (userLocal == null) return false;
return LocalStaticData.users.Remove(userLocal); return GetAllUsers.Remove(userLocal);
} }
public UserLocalEnity? GetUserByGuid(Guid userGuid) { public UserLocalEnity? GetUserByGuid(Guid userGuid) {
UserLocalEnity? userLocal = LocalStaticData UserLocalEnity? userLocal = GetAllUsers
.users
.Where(x => x.Guid == userGuid).FirstOrDefault(); .Where(x => x.Guid == userGuid).FirstOrDefault();
if (userLocal == null) return null; if (userLocal == null) return null;
@ -32,8 +35,7 @@ namespace Demo.Data.Repository
} }
public UserLocalEnity? UpdateUser(UserLocalEnity userUpdateLocalEnity) { public UserLocalEnity? UpdateUser(UserLocalEnity userUpdateLocalEnity) {
UserLocalEnity? userLocal = LocalStaticData UserLocalEnity? userLocal = GetAllUsers
.users
.Where(x => x.Guid == userUpdateLocalEnity.Guid).FirstOrDefault(); .Where(x => x.Guid == userUpdateLocalEnity.Guid).FirstOrDefault();
if (userLocal == null) return null; if (userLocal == null) return null;
userLocal.FIO = userUpdateLocalEnity.FIO; userLocal.FIO = userUpdateLocalEnity.FIO;

View File

@ -21,7 +21,7 @@ namespace Demo.Domain.UseCase
public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups() public List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroups()
.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.GetAllUsers
.Join(_repositoryGroupImpl.GetAllGroups(), .Join(_repositoryGroupImpl.GetAllGroups(),
user => user.GroupID, user => user.GroupID,
group => group.Id, group => group.Id,
@ -31,6 +31,9 @@ namespace Demo.Domain.UseCase
Group = new Group {Id = group.Id, Name = group.Name } } Group = new Group {Id = group.Id, Name = group.Name } }
).ToList(); ).ToList();
public bool RemoveUserByGuid(Guid userGuid) {
return _repositoryUserImpl.RemoveUserByGuid(userGuid);
}
public User UpdateUser(User user) { public User UpdateUser(User user) {
UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid }; UserLocalEnity userLocalEnity = new UserLocalEnity { FIO = user.FIO, GroupID = user.Group.Id, Guid = user.Guid };
UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity); UserLocalEnity? result = _repositoryUserImpl.UpdateUser(userLocalEnity);

View File

@ -14,7 +14,23 @@ namespace Demo.UI
public MainMenuUI(UserUseCase userUseCase) { public MainMenuUI(UserUseCase userUseCase) {
_userConsoleUI = new UserConsoleUI(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; _userUseCase = userUseCase;
} }
public void RemoveUserByGuid(Guid guidUser) {
string output = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален";
Console.WriteLine(output);
}
public void DisplayAllUsers() public void DisplayAllUsers()
{ {
StringBuilder userOutput = new StringBuilder(); 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}"); userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.Group.Name}");
} }