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 AddRoleAsync(CreatingRoleDto creatingRoleDto) { var role = _mapper.Map(creatingRoleDto); var created = await _roleRepository.AddAsync(role); return created; } // READ ALL public async Task> GetAllAsync() { var roles = await _roleRepository.GetAllAsync(); var readDto = _mapper.Map>(roles); return readDto; } // READ BY ID public async Task GetByIdAsync(int id) { var role = await _roleRepository.GetByIdAsync(id); var readDto = _mapper.Map(role); return readDto; } // READ BY NAME public async Task GetByNameAsync(string name) { var role = await _roleRepository.GetByNameAsync(name); var readDto = _mapper.Map(role); return readDto; } // UPDATE public async Task UpdateRoleAsync(UpdatingRoleDto updatingRoleDto) { var role = _mapper.Map(updatingRoleDto); bool isUpdated = await _roleRepository.UpdateAsync(role); return isUpdated; } // DELETE public async Task DeleteRoleAsync(int id) { Role? role = await _roleRepository.GetByIdAsync(id); if (role is null) return false; bool isDeleted = await _roleRepository.DeleteAsync(role); return isDeleted; } } }