2024-09-17 16:54:38 +02:00

133 lines
4.5 KiB
C#

using AutoMapper;
using UserManagement.Application.Dtos.Incomming;
using UserManagement.Application.Dtos.Outgoing;
using UserManagement.Application.Interfaces;
using UserManagement.Domain.Entities;
using UserManagement.Infrastructure.Interfaces;
namespace UserManagement.Application.Services
{
public class UserService : IUserService
{
// CTOR
private readonly IUserRepository _userRepository;
private readonly IRoleRepository _roleRepository;
private readonly IMapper _mapper;
public UserService(IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper)
{
_userRepository = userRepository;
_roleRepository = roleRepository;
_mapper = mapper;
}
// CREATE
public async Task<User?> AddUserAsync(CreatingUserDto creatingUserDto)
{
var user = _mapper.Map<User>(creatingUserDto);
if (!string.IsNullOrEmpty(creatingUserDto.Password))
{
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(creatingUserDto.Password);
}
user.UserRoles = new List<UserRole>();
foreach (var roleId in creatingUserDto.RoleIds)
{
var role = await _roleRepository.GetByIdAsync(roleId);
if (role is not null)
{
user.UserRoles.Add(new UserRole { UserId = user.Id, RoleId = role.Id });
}
else
{
throw new ArgumentException($"Role with Id {roleId} not found");
}
}
var created = await _userRepository.AddAsync(user);
return created;
}
// READ ALL
public async Task<IEnumerable<ReadingUserDto>> GetUsersAsync(bool includeRoles = true)
{
var users = await _userRepository.GetAllAsync(includeRoles);
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
return readDto;
}
// READ BY ID
public async Task<ReadingUserDto> GetByIdAsync(int id, bool includeRoles = true)
{
var user = await _userRepository.GetByIdAsync(id, includeRoles);
var readDto = _mapper.Map<ReadingUserDto>(user);
return readDto;
}
// READ BY USERNAME
public async Task<ReadingUserDto> GetByUsernameAsync(string username, bool includeRoles = true)
{
var user = await _userRepository.GetByUsernameAsync(username, includeRoles);
var readDto = _mapper.Map<ReadingUserDto>(user);
return readDto;
}
// READ BY ROLE
public async Task<IEnumerable<ReadingUserDto>> GetByRoleAsync(string role)
{
var users = await _userRepository.GetByRoleAsync(role);
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
return readDto;
}
// UPDATE
public async Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto)
{
var user = await _userRepository.GetByIdAsync(updatingUserDto.Id);
if (user is null)
{
return false;
}
_mapper.Map(updatingUserDto, user);
if (!string.IsNullOrEmpty(updatingUserDto.Password))
{
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(updatingUserDto.Password);
}
user.UserRoles!.Clear();
foreach(var roleId in updatingUserDto.RoleIds)
{
var role = await _roleRepository.GetByIdAsync(roleId);
if (role is not null)
{
user.UserRoles.Add(new UserRole { UserId = user.Id, RoleId = role.Id });
}
else
{
throw new ArgumentException($"Role with Id {roleId} not found");
}
}
bool isUpdated = await _userRepository.UpdateAsync(user);
return isUpdated;
}
// DELETE
public async Task<bool> DeleteUserAsync(int id)
{
User? user = await _userRepository.GetByIdAsync(id);
if (user is null)
return false;
bool isDeleted = await _userRepository.DeleteAsync(user);
return isDeleted;
}
}
}