blob: ebb305e188eda186776894eb40a7a1eb4b508841 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using DevHive.Web.Models.Role;
using AutoMapper;
using System;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.Role;
using Microsoft.AspNetCore.Authorization;
namespace DevHive.Web.Controllers
{
/// <summary>
/// All endpoints for interacting with the roles layer
/// </summary>
[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;
}
/// <summary>
/// Create a new role for the roles hierarchy. Admin only!
/// </summary>
/// <param name="createRoleWebModel">The new role's parametars</param>
/// <returns>The new role's Id</returns>
[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 });
}
/// <summary>
/// Get a role's full data, querying it by it's Id
/// </summary>
/// <param name="id">The role's Id</param>
/// <returns>Full info of the role</returns>
[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);
}
/// <summary>
/// Update a role's parametars. Admin only!
/// </summary>
/// <param name="id">The role's Id</param>
/// <param name="updateRoleWebModel">The new parametrats for that role</param>
/// <returns>Ok result</returns>
[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();
}
/// <summary>
/// Delete a role. Admin only!
/// </summary>
/// <param name="id">The role's Id</param>
/// <returns>Ok result</returns>
[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();
}
}
}
|