172 lines
5.6 KiB
C#
172 lines
5.6 KiB
C#
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;
|
|
|
|
public UsersController(IUserService userService)
|
|
{
|
|
_userService = userService;
|
|
}
|
|
|
|
[HttpGet("all")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<List<UserInformationDto>>>> GetAllUsers()
|
|
{
|
|
var users = await _userService.GetAllUsers();
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpGet("user-info")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<UserInformationDto>>> GetUserInformation(string id)
|
|
{
|
|
var user = await _userService.GetUserInformationById(id);
|
|
return Ok(user);
|
|
}
|
|
|
|
[HttpGet("user-authorized")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<User>>> UserFromAuthorizeHead()
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if(identity != null)
|
|
{
|
|
IEnumerable<Claim> 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<ActionResult<ServiceResponse<List<UserInformationDto>>>> GetAllOnlineUsers()
|
|
{
|
|
var users = await _userService.GetAllOnlineUsers();
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpPut("update")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<UserInformationDto>>> UpdateUserInformation(UserUpdateInformationDto user)
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if(identity != null)
|
|
{
|
|
IEnumerable<Claim> 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<ActionResult<ServiceResponse<string>>> UploadOrUpdateProfilePic(string userId, IFormFile file)
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if(identity != null)
|
|
{
|
|
IEnumerable<Claim> 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<ActionResult> 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<ActionResult<ServiceResponse<User>>> DeleteUserById(string id)
|
|
{
|
|
var result = await _userService.DeleteUser(id);
|
|
return Ok(result);
|
|
}
|
|
|
|
[HttpPost("update-user-currency")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<int>>> UpdateUserCurrency(int amount, bool isSpinClaim)
|
|
{
|
|
var identity = HttpContext.User.Identity as ClaimsIdentity;
|
|
|
|
if (identity != null)
|
|
{
|
|
IEnumerable<Claim> claims = identity.Claims;
|
|
var id = claims.First().Value;
|
|
|
|
if (id != null) return Ok(await _userService.AddCurrencyToUser(id, amount, isSpinClaim));
|
|
else return Ok(new ServiceResponse<int> { Success = false, Message = "Identity Has No User ID" });
|
|
}
|
|
else return Ok(new ServiceResponse<int> { Success = false, Message = "No Identity" });
|
|
}
|
|
}
|
|
}
|