2024-11-11 09:07:11 +00:00
|
|
|
|
using domain.Models;
|
|
|
|
|
using domain.UseCase;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace ui
|
|
|
|
|
{
|
|
|
|
|
public class UserConsoleUI
|
|
|
|
|
{
|
|
|
|
|
private readonly UserUseCase _userUseCase;
|
|
|
|
|
|
|
|
|
|
public UserConsoleUI(UserUseCase userUseCase)
|
|
|
|
|
{
|
|
|
|
|
_userUseCase = userUseCase;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:00:26 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
public void DisplayAllUsers()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("\n=== Список всех пользователей ===");
|
|
|
|
|
StringBuilder userOutput = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
foreach (var user in _userUseCase.GetAllUsers())
|
|
|
|
|
{
|
|
|
|
|
userOutput.AppendLine($"{user.Guid}\t{user.FIO}\t{user.Group.Name}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(userOutput);
|
|
|
|
|
Console.WriteLine("===============================\n");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:00:26 +00:00
|
|
|
|
|
|
|
|
|
public void RemoveUserByGuid(Guid userGuid)
|
2024-11-11 09:07:11 +00:00
|
|
|
|
{
|
2024-11-13 09:00:26 +00:00
|
|
|
|
string output = _userUseCase.RemoveUserByGuid(userGuid) ? "Пользователь удален" : "Пользователь не найден";
|
2024-11-11 09:07:11 +00:00
|
|
|
|
Console.WriteLine($"\n{output}\n");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:00:26 +00:00
|
|
|
|
public void UpdateUserByGuid(Guid userGuid)
|
2024-11-11 09:07:11 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-11-13 09:00:26 +00:00
|
|
|
|
var user = _userUseCase.FindUserByGuid(userGuid);
|
2024-11-11 09:07:11 +00:00
|
|
|
|
Console.WriteLine($"Текущие данные: {user.FIO}, {user.Group.Name}");
|
|
|
|
|
Console.Write("\nВведите новое ФИО: ");
|
|
|
|
|
string newFIO = Console.ReadLine();
|
|
|
|
|
user.FIO = newFIO;
|
|
|
|
|
_userUseCase.UpdateUser(user);
|
|
|
|
|
Console.WriteLine("\nПользователь обновлен.\n");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка: {ex.Message}\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:00:26 +00:00
|
|
|
|
|
|
|
|
|
public void FindUserByGuid(Guid userGuid)
|
2024-11-11 09:07:11 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-11-13 09:00:26 +00:00
|
|
|
|
var user = _userUseCase.FindUserByGuid(userGuid);
|
2024-11-11 09:07:11 +00:00
|
|
|
|
Console.WriteLine($"\nПользователь найден: {user.Guid}, {user.FIO}, {user.Group.Name}\n");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Ошибка: {ex.Message}\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ValidateUserFIO(string fio)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(fio))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("ФИО не может быть пустым.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:00:26 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
private GroupLocalEntity ValidateGroupExistence(int groupId)
|
|
|
|
|
{
|
|
|
|
|
var group = _userUseCase.GetAllUsers().FirstOrDefault(u => u.Group.Id == groupId)?.Group;
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Группа не найдена.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-13 09:00:26 +00:00
|
|
|
|
|
2024-11-11 09:07:11 +00:00
|
|
|
|
return new GroupLocalEntity
|
|
|
|
|
{
|
|
|
|
|
Id = group.Id,
|
|
|
|
|
Name = group.Name
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|