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

namespace DevHive.Web.Controllers
{
	[ApiController]
	[Route("/api/[controller]")]
	public class UserController: ControllerBase
	{
		private readonly UserService _userService;
		private readonly IMapper _userMapper;

		public UserController(DevHiveContext context, IMapper mapper, JWTOptions jwtOptions)
		{
			this._userService = new UserService(context, mapper, jwtOptions);
			this._userMapper = mapper;
		}

		[HttpPost]
		[Route("Login")]
		public async Task<IActionResult> Login([FromBody] LoginWebModel loginModel)
		{
			LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel);

			TokenServiceModel tokenServiceModel = await this._userService.LoginUser(loginServiceModel);
			TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenServiceModel);

			return new OkObjectResult(tokenWebModel);
		}

		[HttpPost]
		[Route("Register")]
		public async Task<IActionResult> Register([FromBody] RegisterWebModel registerModel)
		{
			RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel);

			UserServiceModel userServiceModel = await this._userService.RegisterUser(registerServiceModel);
			UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel);

			return new CreatedResult("Register", userWebModel);
		}

		//Read
		[HttpGet]
		public async Task<IActionResult> GetById(Guid id)
		{
			UserServiceModel userServiceModel = await this._userService.GetUserById(id);
			UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel);

			return new OkObjectResult(userWebModel);
		}

		//Update
		[HttpPut]
		[Authorize(Roles = Role.DefaultRole)]
		public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateModel)
		{
			UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateModel);
			updateUserServiceModel.Id = id;

			UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel);
			UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel);

			return new AcceptedResult("UpdateUser", userWebModel);

		}

		//Delete
		[HttpDelete]
		[Authorize(Roles = Role.DefaultRole)]
		public async Task<IActionResult> Delete(Guid id)
		{
			await this._userService.DeleteUser(id);
			return new OkResult();
		}
	}
}