55 lines
1.6 KiB
C#
55 lines
1.6 KiB
C#
using MediatR;
|
|||
|
|
using MemeVault.Api.Auth;
|
||
|
|
using MemeVault.Application.Commands.DeleteMedia;
|
||
|
|
using MemeVault.Application.Commands.UpdateMedia;
|
||
|
|
using MemeVault.Application.Queries.GetUserMedia;
|
||
|
|
using MemeVault.Application.Queries.SearchMedia;
|
||
|
|
using Microsoft.AspNetCore.Authorization;
|
||
|
|
using Microsoft.AspNetCore.Mvc;
|
||
|
|
|
||
|
|
namespace MemeVault.Api.Controllers;
|
||
|
|
|
||
|
|
[ApiController]
|
||
|
|
[Authorize]
|
||
|
|
[Route("api/[controller]")]
|
||
|
|
public sealed class MediaController(IMediator mediator, CurrentUser currentUser) : ControllerBase
|
||
|
|
{
|
||
|
|
// GET /api/media?q=...
|
||
|
|
[HttpGet]
|
||
|
|
public async Task<IActionResult> GetAll([FromQuery] string? q, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var items = string.IsNullOrWhiteSpace(q)
|
||
|
|
? await mediator.Send(new GetUserMediaQuery(currentUser.TelegramId), ct)
|
||
|
|
: await mediator.Send(new SearchMediaQuery(currentUser.TelegramId, q), ct);
|
||
|
|
|
||
|
|
return Ok(items);
|
||
|
|
}
|
||
|
|
|
||
|
|
// PATCH /api/media/{id}
|
||
|
|
[HttpPatch("{id:guid}")]
|
||
|
|
public async Task<IActionResult> Update(
|
||
|
|
Guid id,
|
||
|
|
[FromBody] UpdateMediaRequest request,
|
||
|
|
CancellationToken ct)
|
||
|
|
{
|
||
|
|
var ok = await mediator.Send(
|
||
|
|
new UpdateMediaCommand(id, currentUser.TelegramId, request.Caption, request.Tags),
|
||
|
|
ct);
|
||
|
|
|
||
|
|
return ok ? NoContent() : NotFound();
|
||
|
|
}
|
||
|
|
|
||
|
|
// DELETE /api/media/{id}
|
||
|
|
[HttpDelete("{id:guid}")]
|
||
|
|
public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
|
||
|
|
{
|
||
|
|
await mediator.Send(new DeleteMediaCommand(id, currentUser.TelegramId), ct);
|
||
|
|
return NoContent();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed record UpdateMediaRequest(
|
||
|
|
string? Caption,
|
||
|
|
IReadOnlyList<string> Tags
|
||
|
|
);
|