| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using System.Security.Claims; |
| | | 5 | | |
| | | 6 | | namespace Anichron.API.Endpoints; |
| | | 7 | | |
| | | 8 | | public sealed record PatchInteractionRequest(bool? Starred, bool? Liked, bool? Hidden); |
| | | 9 | | |
| | | 10 | | public static class MediaEndpoints |
| | | 11 | | { |
| | | 12 | | internal const int HiddenPageSize = 60; |
| | | 13 | | internal const string InvalidCursorMessage = "after must be the next cursor of an earlier page."; |
| | | 14 | | |
| | | 15 | | public static IEndpointRouteBuilder MapMediaEndpoints(this IEndpointRouteBuilder app) |
| | 55 | 16 | | { |
| | 55 | 17 | | var assets = app.MapGroup(ApiPaths.Assets.Group).WithTags("Media").RequireUser(); |
| | 55 | 18 | | assets.MapPatch(ApiPaths.Assets.Interaction, PatchInteractionAsync) |
| | 55 | 19 | | .Produces<AssetInteractionDto>() |
| | 55 | 20 | | .Produces(StatusCodes.Status404NotFound); |
| | 55 | 21 | | assets.MapGet(ApiPaths.Assets.Hidden, GetHiddenAsync) |
| | 55 | 22 | | .Produces<HiddenAssetsPageDto>() |
| | 55 | 23 | | .ProducesValidationProblem(); |
| | 55 | 24 | | return app; |
| | 55 | 25 | | } |
| | | 26 | | |
| | | 27 | | internal static async Task<IResult> GetHiddenAsync( |
| | | 28 | | string? after, |
| | | 29 | | ClaimsPrincipal user, |
| | | 30 | | [FromServices] IAssetViewReader reader, |
| | | 31 | | CancellationToken ct) |
| | 10 | 32 | | { |
| | 10 | 33 | | if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) |
| | 0 | 34 | | return Results.Unauthorized(); |
| | | 35 | | |
| | 10 | 36 | | HiddenCursor? cursor = null; |
| | 10 | 37 | | if (after is not null && (cursor = HiddenCursors.Parse(after)) is null) |
| | 5 | 38 | | return ApiProblems.FieldProblem(StatusCodes.Status400BadRequest, ErrorCodes.InvalidCursor, nameof(after), In |
| | | 39 | | |
| | 5 | 40 | | var views = await reader.ReadAsync(userId, new HiddenAssets(cursor, HiddenPageSize + 1), ct); |
| | 5 | 41 | | var page = views.Take(HiddenPageSize).ToList(); |
| | 5 | 42 | | return Results.Ok(new HiddenAssetsPageDto( |
| | 5 | 43 | | [.. page.Select(view => new HiddenAssetDto(AssetDtoMapper.ToDto(view, burst: null), HiddenCursors.FormatHidd |
| | 5 | 44 | | views.Count > HiddenPageSize ? HiddenCursors.Format(page[^1]) : null)); |
| | 10 | 45 | | } |
| | | 46 | | |
| | | 47 | | internal static async Task<IResult> PatchInteractionAsync( |
| | | 48 | | Guid assetId, |
| | | 49 | | PatchInteractionRequest? request, |
| | | 50 | | ClaimsPrincipal user, |
| | | 51 | | [FromServices] IAssetInteractionWriter interactions, |
| | | 52 | | CancellationToken ct) |
| | 6 | 53 | | { |
| | 6 | 54 | | if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) |
| | 0 | 55 | | return Results.Unauthorized(); |
| | | 56 | | |
| | 6 | 57 | | var change = new InteractionChange(request?.Starred, request?.Liked, request?.Hidden); |
| | 6 | 58 | | return await interactions.ApplyAsync(userId, assetId, change, ct) is { } state |
| | 6 | 59 | | ? Results.Ok(AssetDtoMapper.ToDto(state)) |
| | 6 | 60 | | : Results.NotFound(); |
| | 6 | 61 | | } |
| | | 62 | | } |