From fadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 17 Dec 2020 11:59:52 +0200 Subject: Reworked RoleController and RoleService to be consistent with the app structure --- .../Configurations/Mapping/RoleMapings.cs | 1 + src/DevHive.Services/Services/RoleService.cs | 39 +++++++++------------- 2 files changed, 16 insertions(+), 24 deletions(-) (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 0e06523..25b314a 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,6 +9,7 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c04bf31..d5c1848 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -4,7 +4,6 @@ using AutoMapper; using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.Role; -using Microsoft.AspNetCore.Mvc; namespace DevHive.Services.Services { @@ -19,51 +18,43 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) - return new BadRequestObjectResult("Role already exists!"); + throw new ArgumentException("Role already exists!"); Role role = this._roleMapper.Map(roleServiceModel); - await this._roleRepository.AddAsync(role); - - return new CreatedResult("CreateRole", role); + return await this._roleRepository.AddAsync(role); } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { - Role role = await this._roleRepository.GetByIdAsync(id); - - if (role == null) - return new NotFoundObjectResult("Role does not exist!"); + Role role = await this._roleRepository.GetByIdAsync(id) + ?? throw new ArgumentException("Role does not exist!"); - return new ObjectResult(role); + return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) - return new NotFoundObjectResult("Role does not exist!"); + throw new ArgumentException("Role does not exist!"); - if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) - return new BadRequestObjectResult("Role name already exists!"); + if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + throw new ArgumentException("Role name already exists!"); Role role = this._roleMapper.Map(roleServiceModel); - await this._roleRepository.EditAsync(role); - - return new AcceptedResult("UpdateRole", role); + return await this._roleRepository.EditAsync(role); } - public async Task DeleteRole(Guid id) + public async Task DeleteRole(Guid id) { if (!await this._roleRepository.DoesRoleExist(id)) - return new NotFoundObjectResult("Role does not exist!"); + throw new ArgumentException("Role does not exist!"); Role role = await this._roleRepository.GetByIdAsync(id); - await this._roleRepository.DeleteAsync(role); - - return new OkResult(); + return await this._roleRepository.DeleteAsync(role); } } } -- cgit v1.2.3