aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers/RoleController.cs
blob: 1664f4ffb051b4e18feea3bd79195ffd56308644 (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
using System.Threading.Tasks;
using DevHive.Data.Repositories;
using DevHive.Services.Services;
using Microsoft.AspNetCore.Mvc;
using DevHive.Web.Models.Identity.Role;
using AutoMapper;
using DevHive.Services.Models.Identity.Role;
using System;
using Microsoft.AspNetCore.Authorization;

namespace DevHive.Web.Controllers
{
    [ApiController]
	[Route("/api/[controller]")]
	//[Authorize(Roles = "Admin")]
	public class RoleController
	{
		private readonly RoleService _roleService;
		private readonly IMapper _roleMapper;

		public RoleController(DevHiveContext context, IMapper mapper)
		{
			this._roleService = new RoleService(context, mapper);
			this._roleMapper = mapper;
		}

		[HttpPost]
		public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel)
		{
			RoleServiceModel roleServiceModel = 
				this._roleMapper.Map<RoleServiceModel>(createRoleWebModel); 
			
			bool result = await this._roleService.CreateRole(roleServiceModel);

			if (!result)
				return new BadRequestObjectResult("Could not create role!");

			return new OkResult();
		}

		[HttpGet]
		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]
		public async Task<IActionResult> Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel)
		{
			RoleServiceModel roleServiceModel = 
				this._roleMapper.Map<RoleServiceModel>(updateRoleWebModel);
			roleServiceModel.Id = id;

			bool result = await this._roleService.UpdateRole(roleServiceModel);

			if (!result)
				return new BadRequestObjectResult("Could not update role!");

			return new OkResult();
		}

		[HttpDelete]
		public async Task<IActionResult> Delete(Guid id)
		{
			bool result = await this._roleService.DeleteRole(id);

			if (!result)
				return new BadRequestObjectResult("Could nor delete role!");

			return new OkResult();
		}
	}
}