Files
lct2025-lonolith/LctMonolith/Controllers/ProfileController.cs
2025-10-01 23:59:31 +03:00

89 lines
2.6 KiB
C#

using System.Security.Claims;
using LctMonolith.Models.Database;
using LctMonolith.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace LctMonolith.Controllers;
[ApiController]
[Route("api/profile")]
[Authorize]
public class ProfileController : ControllerBase
{
private readonly IProfileService _profiles;
public ProfileController(IProfileService profiles)
{
_profiles = profiles;
}
private Guid CurrentUserId()
{
return Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
}
public class UpdateProfileDto
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public DateOnly? BirthDate { get; set; }
public string? About { get; set; }
public string? Location { get; set; }
}
[HttpGet("me")]
public async Task<IActionResult> GetMe(CancellationToken ct)
{
var profile = await _profiles.GetByUserIdAsync(CurrentUserId(), ct);
if (profile == null)
{
return NotFound();
}
return Ok(profile);
}
[HttpGet("{userId:guid}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> GetByUser(Guid userId, CancellationToken ct)
{
var profile = await _profiles.GetByUserIdAsync(userId, ct);
if (profile == null)
{
return NotFound();
}
return Ok(profile);
}
[HttpPut]
public async Task<IActionResult> Upsert(UpdateProfileDto dto, CancellationToken ct)
{
var profile = await _profiles.UpsertAsync(CurrentUserId(), dto.FirstName, dto.LastName, dto.BirthDate, dto.About, dto.Location, ct);
return Ok(profile);
}
[HttpPost("avatar")]
[RequestSizeLimit(7_000_000)]
public async Task<IActionResult> UploadAvatar(IFormFile file, CancellationToken ct)
{
if (file == null || file.Length == 0)
{
return BadRequest("File required");
}
await using var stream = file.OpenReadStream();
var profile = await _profiles.UpdateAvatarAsync(CurrentUserId(), stream, file.ContentType, file.FileName, ct);
return Ok(new { profile.AvatarUrl });
}
[HttpDelete("avatar")]
public async Task<IActionResult> DeleteAvatar(CancellationToken ct)
{
var removed = await _profiles.DeleteAvatarAsync(CurrentUserId(), ct);
if (!removed)
{
return NotFound();
}
return NoContent();
}
}