FinalPresenceLexa/UI/UserConsoleUI.cs
2025-04-28 11:51:01 +03:00

123 lines
4.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Linq;
using System.Text;
using domain.UseCase;
using data.RemoteData.RemoteDataBase.DAO;
using domain.UseCase;
namespace ui
{
public class UserConsoleUI
{
private readonly UserUseCase _userUseCase;
private readonly GroupUseCase _groupUseCase;
public UserConsoleUI(UserUseCase userUseCase, GroupUseCase groupService)
{
_userUseCase = userUseCase;
_groupUseCase = groupService;
}
public void ShowAllUsers()
{
Console.WriteLine("\n=== Полный список пользователей ===");
var users = _userUseCase.GetAllUsers();
if (users == null || !users.Any())
{
Console.WriteLine("В системе нет зарегистрированных пользователей.");
return;
}
var groups = _groupUseCase.GetAllGroups();
var report = new StringBuilder();
foreach (var user in users)
{
var groupInfo = groups?.FirstOrDefault(g => g.Id == user.GroupId)?.Name
?? $"Группа ID:{user.GroupId}";
report.AppendLine($"ID: {user.UserId,-5} | ФИО: {user.FIO,-30} | Группа: {groupInfo}");
}
Console.WriteLine(report);
Console.WriteLine(new string('=', 50));
}
public void DeleteUser()
{
Console.Write("Введите ID пользователя для удаления: ");
if (int.TryParse(Console.ReadLine(), out int userId))
{
bool result = _userUseCase.RemoveUser(userId);
Console.WriteLine(result ? "Пользователь успешно удален."
: "Пользователь с указанным ID не найден.");
}
else
{
Console.WriteLine("Некорректный формат ID.");
}
}
public void EditUser()
{
Console.Write("Введите ID пользователя для редактирования: ");
if (!int.TryParse(Console.ReadLine(), out int userId))
{
Console.WriteLine("Некорректный формат ID.");
return;
}
try
{
var user = _userUseCase.FindUserById(userId);
if (user == null)
{
Console.WriteLine("Пользователь не найден.");
return;
}
Console.WriteLine($"\nТекущие данные:\nФИО: {user.FIO}\nГруппа: {user.GroupId}");
Console.Write("\nНовое ФИО (Enter - оставить без изменений): ");
string newName = Console.ReadLine();
Console.Write("Новый ID группы (Enter - оставить текущий): ");
string groupInput = Console.ReadLine();
string updatedName = string.IsNullOrEmpty(newName) ? user.FIO : newName;
int updatedGroupId = string.IsNullOrEmpty(groupInput) ? user.GroupId : int.Parse(groupInput);
_userUseCase.UpdateUser(userId, updatedName, updatedGroupId);
Console.WriteLine("\nДанные пользователя обновлены.");
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка: {ex.Message}");
}
}
public void FindUser()
{
Console.Write("Введите ID пользователя для поиска: ");
if (!int.TryParse(Console.ReadLine(), out int userId))
{
Console.WriteLine("Некорректный формат ID.");
return;
}
var user = _userUseCase.FindUserById(userId);
if (user != null)
{
Console.WriteLine("\nНайден пользователь:");
Console.WriteLine($"ID: {user.UserId}");
Console.WriteLine($"ФИО: {user.FIO}");
Console.WriteLine($"Группа: {user.Group?.Name ?? "Не указана"}");
}
else
{
Console.WriteLine("Пользователь не найден.");
}
}
}
}