LANCommander/LANCommander.Server/Controllers/Api/ProfileController.cs

156 lines
4.8 KiB
C#
Raw Normal View History

2024-08-04 18:44:33 -05:00
using LANCommander.Server.Data.Models;
using LANCommander.Server.Models;
using LANCommander.Server.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
2024-08-04 18:44:33 -05:00
namespace LANCommander.Server.Controllers.Api
{
[Route("api/[controller]")]
[Authorize(AuthenticationSchemes = "Bearer")]
[ApiController]
public class ProfileController : BaseApiController
{
private readonly UserService UserService;
2024-12-20 11:34:59 -06:00
private readonly MediaService MediaService;
private readonly UserCustomFieldService UserCustomFieldService;
public ProfileController(
ILogger<ProfileController> logger,
UserService userService,
2024-12-20 11:34:59 -06:00
MediaService mediaService,
UserCustomFieldService userCustomFieldService) : base(logger)
{
UserService = userService;
2024-12-20 11:34:59 -06:00
MediaService = mediaService;
UserCustomFieldService = userCustomFieldService;
}
[HttpGet]
2024-11-12 23:22:01 -06:00
public async Task<ActionResult<User>> GetAsync()
{
if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
{
2024-11-16 12:12:25 -06:00
var user = await UserService.GetAsync<SDK.Models.User>(User?.Identity?.Name);
if (user != null)
return Ok(user);
else
return NotFound();
}
else
return Unauthorized();
}
2024-11-12 23:28:05 -06:00
[HttpPut("ChangeAlias")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> ChangeAliasAsync(ChangeAliasRequest request)
{
if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
{
var user = await UserService.GetAsync(User?.Identity?.Name);
2024-01-15 20:38:45 -06:00
user.Alias = request.Alias;
await UserService.UpdateAsync(user);
2024-01-15 20:38:45 -06:00
return Ok(request.Alias);
}
else
return Unauthorized();
}
[HttpGet("Avatar")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> AvatarAsync()
{
if (User != null && User.Identity != null && User.Identity.IsAuthenticated)
{
var user = await UserService.GetAsync(User?.Identity?.Name);
2024-12-20 11:34:59 -06:00
if (user == null)
return NotFound();
var media = await MediaService.FirstOrDefaultAsync(m => m.Type == SDK.Enums.MediaType.Avatar && m.UserId == user.Id);
if (media == null)
return NotFound();
2024-11-05 19:38:59 -06:00
var fs = System.IO.File.OpenRead(MediaService.GetMediaPath(media));
return File(fs, media.MimeType);
}
return NotFound();
}
[AllowAnonymous]
[HttpGet("{userName}/Avatar")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> AvatarAsync(string userName)
{
try
{
var user = await UserService.GetAsync(userName);
if (user == null)
return NotFound();
2024-12-20 11:34:59 -06:00
var media = await MediaService.FirstOrDefaultAsync(m => m.Type == SDK.Enums.MediaType.Avatar && m.UserId == user.Id);
if (media == null)
return NotFound();
2024-11-05 19:38:59 -06:00
var fs = System.IO.File.OpenRead(MediaService.GetMediaPath(media));
return File(fs, media.MimeType);
}
catch (Exception ex)
{
return NotFound();
}
}
[HttpGet("CustomField/{name}")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> GetCustomFieldAsync(string name)
{
try
{
var user = await UserService.GetAsync(User?.Identity?.Name);
var field = await UserCustomFieldService.GetAsync(user.Id, name);
return Ok(field.Value);
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not get the custom field with the name {CustomFieldName}", name);
return NotFound();
}
}
2024-11-12 23:28:05 -06:00
[HttpPut("CustomField/{name}")]
2024-11-12 23:22:01 -06:00
public async Task<IActionResult> UpdateCustomFieldAsync(string name, string value)
{
try
{
var user = await UserService.GetAsync(User?.Identity?.Name);
await UserCustomFieldService.UpdateAsync(user.Id, name, value);
return Ok(value);
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not update the custom field with the name {CustomFieldName}", name);
return BadRequest();
}
}
}
}