| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.Core.Data.Repository; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using NodaTime.Text; |
| | | 5 | | using System.Security.Claims; |
| | | 6 | | |
| | | 7 | | namespace Anichron.API.Endpoints; |
| | | 8 | | |
| | | 9 | | public sealed record OnThisDayDto(string Date, IReadOnlyList<FlashbackYearDto> Years); |
| | | 10 | | |
| | | 11 | | public static class FlashbackEndpoints |
| | | 12 | | { |
| | | 13 | | internal const string InvalidDateMessage = "date must be a calendar date in the form YYYY-MM-DD."; |
| | | 14 | | |
| | | 15 | | public static IEndpointRouteBuilder MapFlashbackEndpoints(this IEndpointRouteBuilder app) |
| | 52 | 16 | | { |
| | 52 | 17 | | var group = app.MapGroup(ApiPaths.Flashbacks.Group).WithTags("Flashbacks").RequireUser(); |
| | 52 | 18 | | group.MapGet(ApiPaths.Flashbacks.OnThisDay, GetOnThisDayAsync) |
| | 52 | 19 | | .Produces<OnThisDayDto>() |
| | 52 | 20 | | .ProducesValidationProblem(); |
| | 52 | 21 | | return app; |
| | 52 | 22 | | } |
| | | 23 | | |
| | | 24 | | internal static async Task<IResult> GetOnThisDayAsync( |
| | | 25 | | string? date, |
| | | 26 | | ClaimsPrincipal user, |
| | | 27 | | [FromServices] IAssetViewReader reader, |
| | | 28 | | [FromServices] IClock clock, |
| | | 29 | | [FromServices] DateTimeZone zone, |
| | | 30 | | CancellationToken ct) |
| | 14 | 31 | | { |
| | 14 | 32 | | if (!Guid.TryParse(user.FindFirstValue(ClaimTypes.NameIdentifier), out var userId)) |
| | 0 | 33 | | return Results.Unauthorized(); |
| | | 34 | | |
| | | 35 | | LocalDate day; |
| | 14 | 36 | | if (date is null) |
| | 2 | 37 | | day = clock.GetCurrentInstant().InZone(zone).Date; |
| | 12 | 38 | | else if (LocalDatePattern.Iso.Parse(date) is { Success: true } parsed) |
| | 8 | 39 | | day = parsed.Value; |
| | | 40 | | else |
| | 4 | 41 | | return ApiProblems.FieldProblem(StatusCodes.Status400BadRequest, ErrorCodes.InvalidDate, nameof(date), Inval |
| | | 42 | | |
| | 10 | 43 | | var selection = new OnThisDay(day); |
| | 10 | 44 | | var views = await reader.ReadAsync(userId, selection, ct); |
| | 10 | 45 | | return Results.Ok(new OnThisDayDto(LocalDatePattern.Iso.Format(day), FlashbackYears.Group(views, selection))); |
| | 14 | 46 | | } |
| | | 47 | | } |