diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-12-15 20:13:24 +0200 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2020-12-15 20:13:37 +0200 |
| commit | 0f35d898c2d0bbc84196ad254dd94a26852e3e4e (patch) | |
| tree | b6a731bb8c32f0159da454a51dcc6354178a3e7c /src/DevHive.Services/Services/RoleService.cs | |
| parent | 4adc2aecc81961430c7d73ff658b535a75e7685b (diff) | |
| download | DevHive-0f35d898c2d0bbc84196ad254dd94a26852e3e4e.tar DevHive-0f35d898c2d0bbc84196ad254dd94a26852e3e4e.tar.gz DevHive-0f35d898c2d0bbc84196ad254dd94a26852e3e4e.zip | |
Implemented RoleService and updated RoleRepository structure
Diffstat (limited to 'src/DevHive.Services/Services/RoleService.cs')
| -rw-r--r-- | src/DevHive.Services/Services/RoleService.cs | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 9077a47..b3e734d 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,5 +1,7 @@ using System; using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.Role; using Microsoft.AspNetCore.Mvc; @@ -8,31 +10,60 @@ namespace DevHive.Services.Services { public class RoleService { - private readonly DevHiveContext _context; + private readonly RoleRepository _roleRepository; + private readonly IMapper _roleMapper; - public RoleService(DevHiveContext context) + public RoleService(DevHiveContext context, IMapper mapper) { - this._context = context; + this._roleRepository = new RoleRepository(context); + this._roleMapper = mapper; } - public Task<IActionResult> CreateRole(RoleServiceModel roleServiceModel) + public async Task<IActionResult> CreateRole(RoleServiceModel roleServiceModel) { - throw new NotImplementedException(); + if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + return new BadRequestObjectResult("Invalid role name!"); + + Role role = this._roleMapper.Map<Role>(roleServiceModel); + + await this._roleRepository.AddAsync(role); + + return new CreatedResult("CreateRole", role); } - public Task<IActionResult> GetRoleById(Guid id) + public async Task<IActionResult> GetRoleById(Guid id) { - throw new NotImplementedException(); + Role role = await this._roleRepository.GetByIdAsync(id); + + if (role == null) + return new NotFoundObjectResult("Role does not exist!"); + + return new ObjectResult(role); } - public Task<IActionResult> UpdateRole(RoleServiceModel roleServiceModel) + public async Task<IActionResult> UpdateRole(RoleServiceModel roleServiceModel) { - throw new NotImplementedException(); + if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) + return new NotFoundObjectResult("Role does not exist!"); + + if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + return new BadRequestObjectResult("Role name already exists!"); + + Role role = this._roleMapper.Map<Role>(roleServiceModel); + await this._roleRepository.EditAsync(role); + + return new AcceptedResult("UpdateRole", role); } - public Task<IActionResult> DeleteRole(Guid id) + public async Task<IActionResult> DeleteRole(Guid id) { - throw new NotImplementedException(); + if (!await this._roleRepository.DoesRoleExist(id)) + return new NotFoundObjectResult("Role does not exist!"); + + Role role = await this._roleRepository.GetByIdAsync(id); + await this._roleRepository.DeleteAsync(role); + + return new OkResult(); } } } |
