prezzens/Demo/Domain/UseCase/UserUseCase.cs
2024-10-19 22:38:28 +03:00

85 lines
2.4 KiB
C#
Raw 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 Demo.Data.Repository;
using Demo.Data.Exceptions;
using Demo.Domain.Models;
using System;
using System.Collections.Generic;
using Demo.domain.Models;
namespace Demo.Domain.UseCase
{
public class UserUseCase
{
private readonly UserRepositoryImpl _userRepositoryImpl;
public UserUseCase(UserRepositoryImpl userRepositoryImpl)
{
_userRepositoryImpl = userRepositoryImpl;
}
public List<UserLocalEntity> GetAllUsers()
{
try
{
return new List<UserLocalEntity>(_userRepositoryImpl.GetAllUsers);
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка при получении пользователей: {ex.Message}");
return new List<UserLocalEntity>();
}
}
public void DeleteUserByGuid(Guid guid)
{
try
{
_userRepositoryImpl.DeleteUserByGuid(guid);
}
catch (DataNotFoundException ex)
{
Console.WriteLine($"Ошибка при удалении пользователя: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Неизвестная ошибка: {ex.Message}");
}
}
public UserLocalEntity? FindUserByGuid(Guid guid)
{
try
{
return _userRepositoryImpl.FindUserByGuid(guid);
}
catch (DataNotFoundException ex)
{
Console.WriteLine($"Ошибка при поиске пользователя: {ex.Message}");
return null;
}
catch (Exception ex)
{
Console.WriteLine($"Неизвестная ошибка: {ex.Message}");
return null;
}
}
public void UpdateUser(Guid guid, string newFIO, int newGroupID)
{
var users = _userRepositoryImpl.GetAllUsers;
var user = users.FirstOrDefault(u => u.Guid == guid);
if (user != null)
{
user.FIO = newFIO;
user.GroupID = newGroupID;
}
else
{
throw new GuidNotFoundException($"Пользователь с GUID {guid} не найден.");
}
}
}
}