| | 1 | | using EF.Blockchain.Server.Dtos; |
| | 2 | | using EF.Blockchain.Server.Mappers; |
| | 3 | | using Microsoft.AspNetCore.Mvc; |
| | 4 | |
|
| | 5 | | namespace EF.Blockchain.Server.Endpoints; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Provides blockchain status-related endpoints. |
| | 9 | | /// </summary> |
| | 10 | | public static class StatusEndpoints |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Maps the status-related endpoints to the application's route builder. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="app">The endpoint route builder.</param> |
| | 16 | | public static void MapStatusEndpoints(this IEndpointRouteBuilder app) |
| 112 | 17 | | { |
| 112 | 18 | | app.MapGet("/status", GetBlockchainStatus) |
| 112 | 19 | | .WithName("GetBlockchainStatus") |
| 112 | 20 | | .WithTags("Status") |
| 112 | 21 | | .WithSummary("Get blockchain status") |
| 112 | 22 | | .WithDescription("Returns mempool size, block count, validation status, and latest block.") |
| 112 | 23 | | .Produces<StatusDto>(statusCode: StatusCodes.Status200OK) |
| 112 | 24 | | .WithOpenApi(); |
| 112 | 25 | | } |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Handles the GET /status endpoint. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="blockchain">The blockchain instance.</param> |
| | 31 | | /// <returns>Blockchain status information.</returns> |
| | 32 | | private static StatusDto GetBlockchainStatus([FromServices] Domain.Blockchain blockchain) |
| 8 | 33 | | { |
| 8 | 34 | | var lastBlock = blockchain.Blocks.LastOrDefault(); |
| | 35 | |
|
| 8 | 36 | | return new StatusDto |
| 8 | 37 | | { |
| 8 | 38 | | Mempool = blockchain.Mempool.Count, |
| 8 | 39 | | Blocks = blockchain.Blocks.Count, |
| 8 | 40 | | IsValid = blockchain.IsValid().Success, |
| 8 | 41 | | LastBlock = lastBlock is null |
| 8 | 42 | | ? null |
| 8 | 43 | | : BlockMapper.ToDto(lastBlock), |
| 8 | 44 | | Difficulty = blockchain.GetDifficulty() |
| 8 | 45 | | }; |
| 7 | 46 | | } |
| | 47 | | } |