using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using qtc_api.Dtos.User; using System.Net.Mime; using System.Security.Claims; using System.Text.Json; namespace qtc_api.Controllers { [Route("api/users")] [ApiController] public class UsersController : ControllerBase { private readonly IUserService _userService; private readonly IConfiguration _configuration; public UsersController(IUserService userService, IConfiguration configuration) { _userService = userService; _configuration = configuration; } [HttpGet("all")] [Authorize] public async Task>>> GetAllUsers() { var users = await _userService.GetAllUsers(); return Ok(users); } [HttpGet("user-info")] [Authorize] public async Task>> GetUserInformation(string id) { var user = await _userService.GetUserInformationById(id); return Ok(user); } [HttpGet("user-authorized")] [Authorize] public async Task>> UserFromAuthorizeHead() { var identity = HttpContext.User.Identity as ClaimsIdentity; if(identity != null) { IEnumerable claims = identity.Claims; var id = claims.First().Value; if(id != null) { var user = await _userService.GetUserById(id); return Ok(user); } else { return BadRequest("Token did not contain an ID."); } } else { return BadRequest("Header not found."); } } [HttpGet("users-online")] [Authorize] public async Task>>> GetAllOnlineUsers() { var users = await _userService.GetAllOnlineUsers(); return Ok(users); } [HttpPut("update")] [Authorize] public async Task>> UpdateUserInformation(UserUpdateInformationDto user) { var identity = HttpContext.User.Identity as ClaimsIdentity; if(identity != null) { IEnumerable claims = identity.Claims; var id = claims.First().Value; if(id != null && id == user.Id) { var updatedUser = await _userService.UpdateUserInfo(user); return Ok(updatedUser); } else { return Unauthorized("You are not authorized to edit that user."); } } else { return BadRequest("Session Expired."); } } [HttpPost("upload-profile-pic")] [Authorize] public async Task>> UploadOrUpdateProfilePic(string userId, IFormFile file) { var identity = HttpContext.User.Identity as ClaimsIdentity; if(identity != null) { IEnumerable claims = identity.Claims; var id = claims.First().Value; if(id != null && id == userId) { if (file.Length > 3000000) { return BadRequest("File Is Above Limit."); } var response = await _userService.UpdateUserPic(userId, file); return Ok(response); } else { return BadRequest("You are not permitted to edit that user."); } } else { return BadRequest("No Identity."); } } [HttpGet("profile-pic/{userId}")] [Authorize] public async Task GetUserProfilePicture(string userId) { var result = await _userService.GetUserPic(userId); if (result != null && result.Success != false) { return result.Data!; } else if (result!.Message == "User Does Not Have A Profile Picture." || result!.Message == "User Content Folder Does Not Exist Yet.") { return BadRequest("User has no profile picture."); } else { return BadRequest("Failed To Get Profile Picture."); } } [HttpDelete("delete-user")] [Authorize(Roles = "Admin")] public async Task>> DeleteUserById(string id) { var result = await _userService.DeleteUser(id); return Ok(result); } } }