From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- src/Web/DevHive.Web/Controllers/RoleController.cs | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/Web/DevHive.Web/Controllers/RoleController.cs (limited to 'src/Web/DevHive.Web/Controllers/RoleController.cs') diff --git a/src/Web/DevHive.Web/Controllers/RoleController.cs b/src/Web/DevHive.Web/Controllers/RoleController.cs new file mode 100644 index 0000000..0d2a2eb --- /dev/null +++ b/src/Web/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 Create([FromBody] CreateRoleWebModel createRoleWebModel) + { + CreateRoleServiceModel roleServiceModel = + this._roleMapper.Map(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 GetById(Guid id) + { + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); + + return new OkObjectResult(roleWebModel); + } + + [HttpPut] + [Authorize(Roles = "Admin")] + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + { + UpdateRoleServiceModel updateRoleServiceModel = + this._roleMapper.Map(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 Delete(Guid id) + { + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could not delete role!"); + + return new OkResult(); + } + } +} -- cgit v1.2.3