2024-09-06 10:59:27 +02:00

74 lines
2.3 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 RoleService : IRoleService
{
// CTOR
private readonly IRoleRepository _roleRepository;
private readonly IMapper _mapper;
public RoleService(IRoleRepository roleRepository, IMapper mapper)
{
_roleRepository = roleRepository;
_mapper = mapper;
}
// CREATE
public async Task<Role?> AddRoleAsync(CreatingRoleDto creatingRoleDto)
{
var role = _mapper.Map<Role>(creatingRoleDto);
var created = await _roleRepository.AddAsync(role);
return created;
}
// READ ALL
public async Task<IEnumerable<ReadingRoleDto>> GetAllAsync()
{
var roles = await _roleRepository.GetAllAsync();
var readDto = _mapper.Map<IEnumerable<ReadingRoleDto>>(roles);
return readDto;
}
// READ BY ID
public async Task<ReadingRoleDto> GetByIdAsync(int id)
{
var role = await _roleRepository.GetByIdAsync(id);
var readDto = _mapper.Map<ReadingRoleDto>(role);
return readDto;
}
// READ BY NAME
public async Task<ReadingRoleDto> GetByNameAsync(string name)
{
var role = await _roleRepository.GetByNameAsync(name);
var readDto = _mapper.Map<ReadingRoleDto>(role);
return readDto;
}
// UPDATE
public async Task<bool> UpdateRoleAsync(UpdatingRoleDto updatingRoleDto)
{
var role = _mapper.Map<Role>(updatingRoleDto);
bool isUpdated = await _roleRepository.UpdateAsync(role);
return isUpdated;
}
// DELETE
public async Task<bool> DeleteRoleAsync(int id)
{
Role? role = await _roleRepository.GetByIdAsync(id);
if (role is null)
return false;
bool isDeleted = await _roleRepository.DeleteAsync(role);
return isDeleted;
}
}
}