diff options
| author | Victor S <57849063+transtrike@users.noreply.github.com> | 2021-02-05 10:54:49 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-05 10:54:49 -0800 |
| commit | f4a70c6430db923af9fa9958a11c2d6612cb52cc (patch) | |
| tree | ca0ea403ba5500df20bc8854ec50529a25c64245 /src/DevHive.Web/Controllers/RoleController.cs | |
| parent | 1ccdefdac025b1b986ad2bd0bc3eda7505d6e7c3 (diff) | |
| parent | 2269b5aa6c8d3dcb407c34fa256200bdc573585a (diff) | |
| download | DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.tar DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.tar.gz DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.zip | |
Merge pull request #18 from Team-Kaleidoscope/devv0.1
First stage: Complete. Awaiting further progress...
Diffstat (limited to 'src/DevHive.Web/Controllers/RoleController.cs')
| -rw-r--r-- | src/DevHive.Web/Controllers/RoleController.cs | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs new file mode 100644 index 0000000..0d2a2eb --- /dev/null +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -0,0 +1,77 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using DevHive.Web.Models.Identity.Role; +using AutoMapper; +using System; +using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; +using Microsoft.AspNetCore.Authorization; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class RoleController + { + private readonly IRoleService _roleService; + private readonly IMapper _roleMapper; + + public RoleController(IRoleService roleService, IMapper mapper) + { + this._roleService = roleService; + this._roleMapper = mapper; + } + + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel) + { + CreateRoleServiceModel roleServiceModel = + this._roleMapper.Map<CreateRoleServiceModel>(createRoleWebModel); + + Guid id = await this._roleService.CreateRole(roleServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [Authorize(Roles = "User,Admin")] + public async Task<IActionResult> GetById(Guid id) + { + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map<RoleWebModel>(roleServiceModel); + + return new OkObjectResult(roleWebModel); + } + + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + { + UpdateRoleServiceModel updateRoleServiceModel = + this._roleMapper.Map<UpdateRoleServiceModel>(updateRoleWebModel); + updateRoleServiceModel.Id = id; + + bool result = await this._roleService.UpdateRole(updateRoleServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update role!"); + + return new OkResult(); + } + + [HttpDelete] + [Authorize(Roles = "Admin")] + public async Task<IActionResult> Delete(Guid id) + { + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could not delete role!"); + + return new OkResult(); + } + } +} |
