using API.BusinessLogic; using API.Models.UserModels; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; namespace API.Controllers { [ApiController] [Route("api/[controller]")] public class UserController : Controller { private readonly UserLogic _userLogic; public UserController(UserLogic userLogic) { _userLogic = userLogic; } /// /// Gets the users email and username /// /// returns the users email, username and Id [Authorize] [HttpGet("get")] public async Task ReadUser() { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _userLogic.GetUser(userId); } /// /// Logins a user /// /// The users login credentials /// Returns a jwttoken their username, id and a refreshtoken [HttpPost("login")] public async Task Login([FromBody] LoginDTO loginDTO) { return await _userLogic.Login(loginDTO); } /// /// Create a new user /// /// contains the username email and password /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [HttpPost("create")] public async Task CreateUser([FromBody] CreateUserDTO userDTO) { return await _userLogic.RegisterUser(userDTO); } /// /// Changes the password of the user /// /// Contains the old password and the new one /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpPut("change-password")] public async Task ChangePassword([FromBody] ChangePasswordDTO passwordDTO) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _userLogic.ChangePassword(passwordDTO, userId); } /// /// Edits the email and username of the user /// /// The updated username and email /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpPut("update")] public async Task UpdateUser([FromBody] UpdateUserDTO userDTO) { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _userLogic.EditProfile(userDTO, userId); } /// /// Deletes the user /// /// returns a okobjectresult with a boolean that is true if it fails it returns a confliftobjectresult with a message of why it failed [Authorize] [HttpDelete("delete")] public async Task DeleteUser() { var claims = HttpContext.User.Claims; string userIdString = claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value; int userId = Convert.ToInt32(userIdString); return await _userLogic.DeleteUser(userId); } /// /// For when the jwt token is outdated /// /// contains a string with the refreshtoken /// returns a new refreshtoken and new jwt token [HttpPost("refreshtoken")] public async Task RefreashToken([FromBody] RefreshTokenDTO refreshToken) { var token = refreshToken.RefreshToken; return await _userLogic.RefreshToken(token); } } }