| | | 1 | | using Anichron.API.Infrastructure; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Anichron.Core.Work; |
| | | 4 | | using Microsoft.AspNetCore.Http.HttpResults; |
| | | 5 | | using Microsoft.AspNetCore.Mvc; |
| | | 6 | | |
| | | 7 | | namespace Anichron.API.Endpoints; |
| | | 8 | | |
| | | 9 | | public static class WorkItemEndpoints |
| | | 10 | | { |
| | | 11 | | internal const int DefaultPageSize = 50; |
| | | 12 | | internal const int MaxPageSize = 200; |
| | | 13 | | |
| | | 14 | | public static IEndpointRouteBuilder MapWorkItemEndpoints(this IEndpointRouteBuilder app) |
| | 50 | 15 | | { |
| | 50 | 16 | | var group = app.MapGroup(ApiPaths.WorkItems.Group).WithTags("Work items") |
| | 50 | 17 | | .RequireAdmin(); |
| | 50 | 18 | | group.MapGet(string.Empty, ListAsync); |
| | 50 | 19 | | group.MapGet(ApiPaths.WorkItems.Events, StreamEvents); |
| | 50 | 20 | | group.MapGet(ApiPaths.WorkItems.ById, GetAsync); |
| | 50 | 21 | | group.MapPost(ApiPaths.WorkItems.Cancel, CancelAsync); |
| | 50 | 22 | | return app; |
| | 50 | 23 | | } |
| | | 24 | | |
| | | 25 | | internal static async Task<Results<Ok<WorkItemPageDto>, ValidationProblem>> ListAsync( |
| | | 26 | | WorkItemStatus? status, |
| | | 27 | | WorkKind? kind, |
| | | 28 | | int? page, |
| | | 29 | | int? pageSize, |
| | | 30 | | [FromServices] IWorkItemReader reader, |
| | | 31 | | CancellationToken ct) |
| | 14 | 32 | | { |
| | 14 | 33 | | var number = page ?? 1; |
| | 14 | 34 | | var size = pageSize ?? DefaultPageSize; |
| | 14 | 35 | | if (size is < 1 or > MaxPageSize) |
| | 2 | 36 | | return ApiProblems.InvalidField(ErrorCodes.InvalidPageSize, nameof(pageSize), $"pageSize must be from 1 to { |
| | 12 | 37 | | if (number < 1 || number > int.MaxValue / size) |
| | 3 | 38 | | return ApiProblems.InvalidField(ErrorCodes.InvalidPage, nameof(page), "page is out of range."); |
| | | 39 | | |
| | 9 | 40 | | var cursor = await reader.LatestUpdateAsync(ct) ?? NodaConstants.UnixEpoch; |
| | 9 | 41 | | var found = await reader.ListAsync(new WorkItemQuery(status, kind, (number - 1) * size, size), ct); |
| | 9 | 42 | | return TypedResults.Ok(new WorkItemPageDto( |
| | 9 | 43 | | [.. found.Items.Select(WorkItemDtoMapper.ToDto)], number, size, found.Total, WorkItemDtoMapper.Format(cursor |
| | 14 | 44 | | } |
| | | 45 | | |
| | | 46 | | internal static async Task<Results<Ok<WorkItemDto>, NotFound>> GetAsync( |
| | | 47 | | Guid workItemId, |
| | | 48 | | [FromServices] IWorkItemReader reader, |
| | | 49 | | CancellationToken ct) |
| | 3 | 50 | | => await reader.FindAsync(workItemId, ct) is { } item |
| | 3 | 51 | | ? TypedResults.Ok(WorkItemDtoMapper.ToDto(item)) |
| | 3 | 52 | | : TypedResults.NotFound(); |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Cancels a queued item at once (200), or asks the running item's Worker to cancel it at its |
| | | 56 | | /// next heartbeat (202). A finished item is a 409 showing the item as it is. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="workItemId">The item to cancel.</param> |
| | | 59 | | /// <param name="canceller">Makes the status-guarded cancel write.</param> |
| | | 60 | | /// <param name="reader">Reads the item after the cancel.</param> |
| | | 61 | | /// <param name="ct">Cancels the request.</param> |
| | | 62 | | /// <returns>The item after the cancel, or 404 when there is none.</returns> |
| | | 63 | | internal static async Task<Results<Ok<WorkItemDto>, Accepted<WorkItemDto>, Conflict<WorkItemDto>, NotFound>> CancelA |
| | | 64 | | Guid workItemId, |
| | | 65 | | [FromServices] IWorkItemCanceller canceller, |
| | | 66 | | [FromServices] IWorkItemReader reader, |
| | | 67 | | CancellationToken ct) |
| | 8 | 68 | | { |
| | 8 | 69 | | var outcome = await canceller.CancelAsync(workItemId, ct); |
| | 8 | 70 | | if (outcome == CancelOutcome.NotFound || await reader.FindAsync(workItemId, ct) is not { } item) |
| | 4 | 71 | | return TypedResults.NotFound(); |
| | | 72 | | |
| | 4 | 73 | | var dto = WorkItemDtoMapper.ToDto(item); |
| | 4 | 74 | | return outcome switch |
| | 4 | 75 | | { |
| | 1 | 76 | | CancelOutcome.Cancelled => TypedResults.Ok(dto), |
| | 1 | 77 | | CancelOutcome.Requested => TypedResults.Accepted(ApiPaths.WorkItems.ItemLocation(workItemId), dto), |
| | 2 | 78 | | _ => TypedResults.Conflict(dto), |
| | 4 | 79 | | }; |
| | 8 | 80 | | } |
| | | 81 | | |
| | | 82 | | internal static ServerSentEventsResult<WorkItemDto> StreamEvents( |
| | | 83 | | [FromHeader(Name = "Last-Event-ID")] string? lastEventId, |
| | | 84 | | [FromServices] WorkItemEvents events, |
| | | 85 | | CancellationToken ct) |
| | 2 | 86 | | => TypedResults.ServerSentEvents(events.StreamAsync(lastEventId, ct)); |
| | | 87 | | } |