using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Models.Identity; using DevHive.Data.Interfaces; using DevHive.Data.Models; using DevHive.Services.Interfaces; namespace DevHive.Services.Services { public class RoleService : IRoleService { private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; public RoleService(IRoleRepository roleRepository, IMapper mapper) { this._roleRepository = roleRepository; this._roleMapper = mapper; } public async Task CreateRole(RoleModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); Role role = this._roleMapper.Map(roleServiceModel); return await this._roleRepository.AddAsync(role); } public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); return this._roleMapper.Map(role); } public async Task UpdateRole(RoleModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role name already exists!"); Role role = this._roleMapper.Map(roleServiceModel); return await this._roleRepository.EditAsync(role); } public async Task DeleteRole(Guid id) { if (!await this._roleRepository.DoesRoleExist(id)) throw new ArgumentException("Role does not exist!"); Role role = await this._roleRepository.GetByIdAsync(id); return await this._roleRepository.DeleteAsync(role); } } }