aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Web/Controllers/UserController.cs
diff options
context:
space:
mode:
authorVictor S <57849063+transtrike@users.noreply.github.com>2021-02-05 10:54:49 -0800
committerGitHub <noreply@github.com>2021-02-05 10:54:49 -0800
commitf4a70c6430db923af9fa9958a11c2d6612cb52cc (patch)
treeca0ea403ba5500df20bc8854ec50529a25c64245 /src/DevHive.Web/Controllers/UserController.cs
parent1ccdefdac025b1b986ad2bd0bc3eda7505d6e7c3 (diff)
parent2269b5aa6c8d3dcb407c34fa256200bdc573585a (diff)
downloadDevHive-0.1.tar
DevHive-0.1.tar.gz
DevHive-0.1.zip
Merge pull request #18 from Team-Kaleidoscope/devv0.1
First stage: Complete. Awaiting further progress...
Diffstat (limited to 'src/DevHive.Web/Controllers/UserController.cs')
-rw-r--r--src/DevHive.Web/Controllers/UserController.cs140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs
new file mode 100644
index 0000000..109bbaa
--- /dev/null
+++ b/src/DevHive.Web/Controllers/UserController.cs
@@ -0,0 +1,140 @@
+using System;
+using System.Threading.Tasks;
+using AutoMapper;
+using DevHive.Services.Models.Identity.User;
+using DevHive.Web.Models.Identity.User;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using DevHive.Common.Models.Identity;
+using DevHive.Services.Interfaces;
+using Microsoft.Extensions.Hosting;
+
+namespace DevHive.Web.Controllers
+{
+ [ApiController]
+ [Route("/api/[controller]")]
+ public class UserController : ControllerBase
+ {
+ private readonly IUserService _userService;
+ private readonly IMapper _userMapper;
+
+ public UserController(IUserService userService, IMapper mapper)
+ {
+ this._userService = userService;
+ this._userMapper = mapper;
+ }
+
+ #region Authentication
+ [HttpPost]
+ [Route("Login")]
+ [AllowAnonymous]
+ public async Task<IActionResult> Login([FromBody] LoginWebModel loginModel)
+ {
+ LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel);
+
+ TokenModel TokenModel = await this._userService.LoginUser(loginServiceModel);
+ TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(TokenModel);
+
+ return new OkObjectResult(tokenWebModel);
+ }
+
+ [HttpPost]
+ [Route("Register")]
+ [AllowAnonymous]
+ public async Task<IActionResult> Register([FromBody] RegisterWebModel registerModel)
+ {
+ RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel);
+
+ TokenModel TokenModel = await this._userService.RegisterUser(registerServiceModel);
+ TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(TokenModel);
+
+ return new CreatedResult("Register", tokenWebModel);
+ }
+ #endregion
+
+ #region Read
+ [HttpGet]
+ [Authorize(Roles = "User,Admin")]
+ public async Task<IActionResult> GetById(Guid id, [FromHeader] string authorization)
+ {
+ if (!await this._userService.ValidJWT(id, authorization))
+ return new UnauthorizedResult();
+
+ UserServiceModel userServiceModel = await this._userService.GetUserById(id);
+ UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel);
+
+ return new OkObjectResult(userWebModel);
+ }
+
+ [HttpGet]
+ [Route("GetUser")]
+ [AllowAnonymous]
+ public async Task<IActionResult> GetUser(string username)
+ {
+ UserServiceModel friendServiceModel = await this._userService.GetUserByUsername(username);
+ UserWebModel friend = this._userMapper.Map<UserWebModel>(friendServiceModel);
+
+ return new OkObjectResult(friend);
+ }
+ #endregion
+
+ #region Update
+ [HttpPut]
+ [Authorize(Roles = "User,Admin")]
+ public async Task<IActionResult> Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization)
+ {
+ if (!await this._userService.ValidJWT(id, authorization))
+ return new UnauthorizedResult();
+
+ UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map<UpdateUserServiceModel>(updateUserWebModel);
+ updateUserServiceModel.Id = id;
+
+ UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel);
+ UserWebModel userWebModel = this._userMapper.Map<UserWebModel>(userServiceModel);
+
+ return new AcceptedResult("UpdateUser", userWebModel);
+ }
+
+ [HttpPut]
+ [Route("ProfilePicture")]
+ [Authorize(Roles = "User,Admin")]
+ public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] UpdateProfilePictureWebModel updateProfilePictureWebModel, [FromHeader] string authorization)
+ {
+ if (!await this._userService.ValidJWT(userId, authorization))
+ return new UnauthorizedResult();
+
+ UpdateProfilePictureServiceModel updateProfilePictureServiceModel = this._userMapper.Map<UpdateProfilePictureServiceModel>(updateProfilePictureWebModel);
+ updateProfilePictureServiceModel.UserId = userId;
+
+ ProfilePictureServiceModel profilePictureServiceModel = await this._userService.UpdateProfilePicture(updateProfilePictureServiceModel);
+ ProfilePictureWebModel profilePictureWebModel = this._userMapper.Map<ProfilePictureWebModel>(profilePictureServiceModel);
+
+ return new AcceptedResult("UpdateProfilePicture", profilePictureWebModel);
+ }
+ #endregion
+
+ #region Delete
+ [HttpDelete]
+ [Authorize(Roles = "User,Admin")]
+ public async Task<IActionResult> Delete(Guid id, [FromHeader] string authorization)
+ {
+ if (!await this._userService.ValidJWT(id, authorization))
+ return new UnauthorizedResult();
+
+ bool result = await this._userService.DeleteUser(id);
+ if (!result)
+ return new BadRequestObjectResult("Could not delete User");
+
+ return new OkResult();
+ }
+ #endregion
+
+ [HttpPost]
+ [Authorize(Roles = "User,Admin")]
+ [Route("SuperSecretPromotionToAdmin")]
+ public async Task<IActionResult> SuperSecretPromotionToAdmin(Guid userId)
+ {
+ return new OkObjectResult(await this._userService.SuperSecretPromotionToAdmin(userId));
+ }
+ }
+}