77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using MediatR;
|
|
using MemeVault.Api.Auth;
|
|
using MemeVault.Application.Commands.CopyMediaToCollection;
|
|
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}
|
|
// Обновляет caption, теги и/или перемещает в другую коллекцию
|
|
[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, request.CollectionId),
|
|
ct);
|
|
|
|
return ok ? NoContent() : NotFound();
|
|
}
|
|
|
|
// POST /api/media/{id}/copy
|
|
// Копирует элемент в другую коллекцию; оригинал остаётся на месте
|
|
[HttpPost("{id:guid}/copy")]
|
|
public async Task<IActionResult> Copy(
|
|
Guid id,
|
|
[FromBody] CopyMediaRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
var newId = await mediator.Send(
|
|
new CopyMediaToCollectionCommand(id, request.TargetCollectionId, currentUser.TelegramId),
|
|
ct);
|
|
|
|
if (newId is null) return NotFound();
|
|
|
|
return CreatedAtAction(nameof(GetAll), new { id = newId }, new { id = newId });
|
|
}
|
|
|
|
// 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,
|
|
Guid? CollectionId = null
|
|
);
|
|
|
|
public sealed record CopyMediaRequest(Guid TargetCollectionId);
|