This commit is contained in:
Nastya 2024-10-20 00:39:57 +03:00
parent abf6ba433d
commit 63b8e26c16
5 changed files with 199 additions and 35 deletions

View File

@ -0,0 +1,51 @@
using Demo.Data.LocalData;
using Demo.Data.Repository;
using Demo.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Demo.Domain.UseCase
{
public class GroupUseCase
{
private List<GroupLocalEntity> _groups = LocalStaticData.groups;
public List<GroupLocalEntity> GetAllGroups() => _groups;
public void AddGroup(GroupLocalEntity group)
{
group.Id = _groups.Any() ? _groups.Max(g => g.Id) + 1 : 1;
_groups.Add(group);
}
public void UpdateGroup(GroupLocalEntity group)
{
var existingGroup = _groups.FirstOrDefault(g => g.Id == group.Id);
if (existingGroup != null)
{
existingGroup.Name = group.Name;
}
}
public void RenameGroup(int groupId, string newName)
{
if (string.IsNullOrWhiteSpace(newName))
{
throw new ArgumentException("Новое имя группы не может быть пустым", nameof(newName));
}
var existingGroup = _groups.FirstOrDefault(g => g.Id == groupId);
if (existingGroup == null)
{
throw new KeyNotFoundException($"Группа с ID {groupId} не найдена.");
}
existingGroup.Name = newName;
}
}
}

View File

@ -19,28 +19,47 @@ namespace Demo.Domain.UseCase
_repositoryGroupImpl = repositoryGroupImpl; _repositoryGroupImpl = repositoryGroupImpl;
} }
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,
(user, group) => (user, group) =>
new User { FIO = user.FIO, new User
Guid = user.Guid, {
Group = new Group {Id = group.Id, Name = group.Name } } FIO = user.FIO,
).ToList(); Guid = user.Guid,
Group = new Group { Id = group.Id, Name = group.Name }
}
).ToList();
public bool RemoveUserByGuid(Guid userGuid) { public bool RemoveUserByGuid(Guid userGuid)
return _repositoryUserImpl.RemoveUserByGuid(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);
if (result == null) throw new Exception(""); if (result == null) throw new Exception("");
Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID); Group? group = GetAllGroups().FirstOrDefault(it => it.Id == result!.GroupID);
if (group == null) throw new Exception(""); if (group == null) throw new Exception("");
return new User { FIO = user.FIO, Guid = user.Guid, Group = group}; return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
public User FindUserByGuid(Guid userGuid)
{
Dictionary<Guid, User> users = new Dictionary<Guid, User>();
if (users.ContainsKey(userGuid))
{
return users[userGuid];
}
else
{
return null;
}
} }
} }
} }

44
Demo/UI/GroupConsole.cs Normal file
View File

@ -0,0 +1,44 @@
using Demo.domain.Models;
using Demo.Domain.UseCase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo.UI
{
public class GroupConsole
{
internal class GroupConsoleUI
{
GroupUseCase _groupUseCase;
public GroupConsoleUI(GroupUseCase groupUseCase)
{
_groupUseCase = groupUseCase;
}
public void AllGroups()
{
foreach (var Group in _groupUseCase.GetAllGroups())
{
Console.WriteLine($"{Group.Id}\t{Group.Name}");
}
}
public void UpdateGroup(int groupId, GroupLocalEntity updatedGroup)
{
if (updatedGroup == null)
{
throw new ArgumentNullException(nameof(updatedGroup), "Обновленная группа не может быть null");
}
}
}
}
}

View File

@ -9,29 +9,59 @@ namespace Demo.UI
{ {
public class MainMenuUI public class MainMenuUI
{ {
private UserConsoleUI _userConsoleUI;
UserConsoleUI _userConsoleUI; public MainMenuUI(UserUseCase userUseCase)
{
public MainMenuUI(UserUseCase userUseCase) { _userConsoleUI = new UserConsoleUI(userUseCase);
_userConsoleUI = new UserConsoleUI(userUseCase);
DisplayMenu(); DisplayMenu();
} }
private void DisplayMenu() { private void DisplayMenu()
{
while (true) while (true)
{ {
switch (Console.ReadLine()) Console.Clear();
{ Console.WriteLine("Главное меню:");
case "1": _userConsoleUI.DisplayAllUsers(); break; Console.WriteLine("1. Показать всех пользователей");
case "2": _userConsoleUI.RemoveUserByGuid(Guid.Parse(Console.ReadLine())); break; Console.WriteLine("2. Удалить пользователя по GUID");
Console.WriteLine("0. Выход");
default: DisplayMenu(); string choice = Console.ReadLine();
switch (choice)
{
case "1":
_userConsoleUI.DisplayAllUsers();
break;
case "2":
RemoveUser();
break;
case "0":
return;
default:
Console.WriteLine("Неверный выбор, попробуйте снова.");
break; break;
} }
Console.WriteLine("Нажмите любую клавишу для продолжения...");
Console.ReadKey();
} }
} }
private void RemoveUser()
{
Console.Write("Введите GUID пользователя для удаления: ");
string input = Console.ReadLine();
if (Guid.TryParse(input, out Guid userGuid))
{
_userConsoleUI.RemoveUserByGuid(userGuid);
}
else
{
Console.WriteLine("Неверный формат GUID. Пожалуйста, попробуйте еще раз.");
}
}
} }
} }

View File

@ -1,4 +1,5 @@
using Demo.Domain.UseCase; using Demo.domain.Models;
using Demo.Domain.UseCase;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -7,17 +8,20 @@ using System.Threading.Tasks;
namespace Demo.UI namespace Demo.UI
{ {
public class UserConsoleUI public class UserConsoleUI
{ {
UserUseCase _userUseCase; UserUseCase _userUseCase;
public UserConsoleUI(UserUseCase userUseCase) { public UserConsoleUI(UserUseCase userUseCase)
{
_userUseCase = userUseCase; _userUseCase = userUseCase;
} }
public void RemoveUserByGuid(Guid guidUser) { public void RemoveUserByGuid(Guid guidUser)
{
string output = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален"; string del = _userUseCase.RemoveUserByGuid(guidUser) ? "Пользователь удален" : "Пользователь не удален";
Console.WriteLine(output); Console.WriteLine(del);
} }
public void DisplayAllUsers() public void DisplayAllUsers()
@ -29,5 +33,21 @@ namespace Demo.UI
} }
Console.WriteLine(userOutput); Console.WriteLine(userOutput);
} }
public void UpdateUserGuid(Guid guidUser)
{
var user = _userUseCase.FindUserByGuid(guidUser);
Console.WriteLine($"Текущие данные: {user.FIO}, {user.Group.Name}");
Console.Write("Введите новое ФИО: ");
string newFIO = Console.ReadLine();
user.FIO = newFIO;
_userUseCase.UpdateUser(user);
}
public void GetUserByGuid(Guid guidUser)
{
var user = _userUseCase.FindUserByGuid(guidUser);
Console.WriteLine($"Пользователь найден: {user.Guid}, {user.FIO}, {user.Group.Name}");
}
} }
} }