< Summary

Information
Class: Anichron.API.Endpoints.MediaEndpoints
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/MediaEndpoints.cs
Tag: 19_45211875487
Line coverage
93%
Covered lines: 28
Uncovered lines: 2
Coverable lines: 30
Total lines: 62
Line coverage: 93.3%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
MapMediaEndpoints(...)100%11100%
GetHiddenAsync()87.5%8891.66%
PatchInteractionAsync()90%101087.5%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/MediaEndpoints.cs

#LineLine coverage
 1using Anichron.API.Infrastructure;
 2using Anichron.Core.Data.Repository;
 3using Microsoft.AspNetCore.Mvc;
 4using System.Security.Claims;
 5
 6namespace Anichron.API.Endpoints;
 7
 8public sealed record PatchInteractionRequest(bool? Starred, bool? Liked, bool? Hidden);
 9
 10public 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)
 5516    {
 5517        var assets = app.MapGroup(ApiPaths.Assets.Group).WithTags("Media").RequireUser();
 5518        assets.MapPatch(ApiPaths.Assets.Interaction, PatchInteractionAsync)
 5519              .Produces<AssetInteractionDto>()
 5520              .Produces(StatusCodes.Status404NotFound);
 5521        assets.MapGet(ApiPaths.Assets.Hidden, GetHiddenAsync)
 5522              .Produces<HiddenAssetsPageDto>()
 5523              .ProducesValidationProblem();
 5524        return app;
 5525    }
 26
 27    internal static async Task<IResult> GetHiddenAsync(
 28        string? after,
 29        ClaimsPrincipal user,
 30        [FromServices] IAssetViewReader reader,
 31        CancellationToken ct)
 1032    {
 1033        if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId))
 034            return Results.Unauthorized();
 35
 1036        HiddenCursor? cursor = null;
 1037        if (after is not null && (cursor = HiddenCursors.Parse(after)) is null)
 538            return ApiProblems.FieldProblem(StatusCodes.Status400BadRequest, ErrorCodes.InvalidCursor, nameof(after), In
 39
 540        var views = await reader.ReadAsync(userId, new HiddenAssets(cursor, HiddenPageSize + 1), ct);
 541        var page = views.Take(HiddenPageSize).ToList();
 542        return Results.Ok(new HiddenAssetsPageDto(
 543            [.. page.Select(view => new HiddenAssetDto(AssetDtoMapper.ToDto(view, burst: null), HiddenCursors.FormatHidd
 544            views.Count > HiddenPageSize ? HiddenCursors.Format(page[^1]) : null));
 1045    }
 46
 47    internal static async Task<IResult> PatchInteractionAsync(
 48        Guid assetId,
 49        PatchInteractionRequest? request,
 50        ClaimsPrincipal user,
 51        [FromServices] IAssetInteractionWriter interactions,
 52        CancellationToken ct)
 653    {
 654        if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId))
 055            return Results.Unauthorized();
 56
 657        var change = new InteractionChange(request?.Starred, request?.Liked, request?.Hidden);
 658        return await interactions.ApplyAsync(userId, assetId, change, ct) is { } state
 659            ? Results.Ok(AssetDtoMapper.ToDto(state))
 660            : Results.NotFound();
 661    }
 62}