227 lines
7.9 KiB
C#
227 lines
7.9 KiB
C#
using Microsoft.Extensions.Caching.Distributed;
|
|
using qtc_api.Extensions;
|
|
using System.Security.Claims;
|
|
|
|
namespace qtc_api.Controllers
|
|
{
|
|
[Route("api/users")]
|
|
[ApiController]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly IDistributedCache cache;
|
|
public UsersController(IUserService userService, IDistributedCache distributedCache)
|
|
{
|
|
_userService = userService;
|
|
cache = distributedCache;
|
|
}
|
|
|
|
[HttpGet("all")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<List<UserInformationDto>>>> GetAllUsers()
|
|
{
|
|
var users = new ServiceResponse<List<UserInformationDto>>();
|
|
|
|
string recordId = $"Users_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
|
users = await cache.GetRecordAsync<ServiceResponse<List<UserInformationDto>>>(recordId);
|
|
|
|
if(users == null)
|
|
{
|
|
users = await _userService.GetAllUsers();
|
|
await cache.SetRecordAsync(recordId, users);
|
|
}
|
|
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpGet("user-info")]
|
|
[Authorize]
|
|
public async Task<ActionResult<ServiceResponse<UserInformationDto>>> GetUserInformation(string id)
|
|
{
|
|
var user = new ServiceResponse<UserInformationDto>();
|
|
|
|
string recordId = $"User_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
|
user = await cache.GetRecordAsync<ServiceResponse<UserInformationDto>>(recordId);
|
|
|
|
if (user == null)
|
|
{
|
|
user = await _userService.GetUserInformationById(id);
|
|
await cache.SetRecordAsync(recordId, user);
|
|
}
|
|
|
|
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);
|
|
|
|
// always try to overwrite cache when updating user info
|
|
string recordId = $"User_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
|
await cache.SetRecordAsync(recordId, updatedUser);
|
|
|
|
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);
|
|
|
|
// always try to overwrite cache when updating pfp
|
|
string recordId = $"UserPfp_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
|
using(var stream = file.OpenReadStream())
|
|
{
|
|
using(var ms = new MemoryStream())
|
|
{
|
|
stream.CopyTo(ms);
|
|
await cache.SetImageAsync(recordId, ms.ToArray());
|
|
ms.Dispose();
|
|
}
|
|
stream.Dispose();
|
|
}
|
|
|
|
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)
|
|
{
|
|
string recordId = $"UserPfp_{DateTime.Now.ToString("yyyyMMdd_hhmm")}";
|
|
byte[] pfpBytes = await cache.GetImageAsync(recordId);
|
|
|
|
var result = new ServiceResponse<FileContentResult>();
|
|
if (pfpBytes == null)
|
|
{
|
|
result = await _userService.GetUserPic(userId);
|
|
if (result != null && result.Success && result.Data != null)
|
|
{
|
|
pfpBytes = result.Data.FileContents;
|
|
await cache.SetImageAsync(recordId, pfpBytes);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// explicitly set from cache
|
|
result.Success = true;
|
|
result.Data = new FileContentResult(pfpBytes, "image/jpeg");
|
|
result.Message = $"{userId}.pfp";
|
|
}
|
|
|
|
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" });
|
|
}
|
|
}
|
|
}
|