presence/Demo/Domain/UseCase/UserUseCase.cs
Class_Student 2971dbdac4 update
2024-11-01 10:14:01 +03:00

78 lines
2.4 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 Demo.Data.Repository;
using Demo.Domain.Models;
using Demo.Data.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using Demo.domain.Models;
namespace Demo.Domain.UseCase
{
public class UserUseCase
{
private readonly UserRepositoryImpl _userRepository;
private readonly IGroupRepository _groupRepository;
public UserUseCase(UserRepositoryImpl userRepository, IGroupRepository groupRepository)
{
_userRepository = userRepository;
_groupRepository = groupRepository;
}
private List<Group> GetAllGroups() =>
_groupRepository.GetAllGroups()
.Select(g => new Group { Id = g.Id, Name = g.Name })
.ToList();
public List<User> GetAllUsers() =>
_userRepository.GetAllUsers
.Join(_groupRepository.GetAllGroups(),
user => user.GroupID,
group => group.Id,
(user, group) => new User
{
FIO = user.FIO,
Guid = user.Guid,
Group = new Group { Id = group.Id, Name = group.Name }
})
.ToList();
public bool RemoveUserByGuid(Guid userGuid)
{
try
{
_userRepository.DeleteUserByGuid(userGuid);
return true;
}
catch (GuidNotFoundException)
{
return false;
}
}
public User UpdateUser(User user)
{
try
{
_userRepository.UpdateUser(user.Guid.ToString(), user.FIO, user.Group.Id);
var group = GetAllGroups().FirstOrDefault(g => g.Id == user.Group.Id);
if (group == null)
{
throw new Exception($"Группа с ID {user.Group.Id} не найдена.");
}
return new User { FIO = user.FIO, Guid = user.Guid, Group = group };
}
catch (GuidNotFoundException ex)
{
throw new Exception("Пользователь не найден.", ex);
}
catch (ArgumentException ex)
{
throw new Exception("Некорректный GUID.", ex);
}
}
}
}