| | 1 | | using EF.Blockchain.Domain; |
| | 2 | | using EF.Blockchain.Server.Dtos; |
| | 3 | | using EF.Blockchain.Server.Mappers; |
| | 4 | | using Microsoft.AspNetCore.Mvc; |
| | 5 | |
|
| | 6 | | namespace EF.Blockchain.Server.Endpoints; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Provides endpoints for accessing and submitting blocks in the blockchain. |
| | 10 | | /// </summary> |
| | 11 | | public static class BlockEndpoints |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Maps the block-related endpoints to the application. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="app">The route builder to register endpoints.</param> |
| | 17 | | public static void MapBlockEndpoints(this IEndpointRouteBuilder app) |
| 112 | 18 | | { |
| 112 | 19 | | app.MapGet("/blocks/next", GetNextBlock) |
| 112 | 20 | | .WithName("GetNextBlock") |
| 112 | 21 | | .WithTags("Block") |
| 112 | 22 | | .WithSummary("Get info for the next block to be mined") |
| 112 | 23 | | .WithDescription("Returns the data needed for mining the next block, including transactions and metadata.") |
| 112 | 24 | | .Produces<BlockInfoDto>(StatusCodes.Status200OK) |
| 112 | 25 | | .WithOpenApi(); |
| | 26 | |
|
| 112 | 27 | | app.MapGet("/blocks/{indexOrHash}", GetBlockByIndexOrHash) |
| 112 | 28 | | .WithName("GetBlockByIndexOrHash") |
| 112 | 29 | | .WithTags("Block") |
| 112 | 30 | | .WithSummary("Get block by index or hash") |
| 112 | 31 | | .WithDescription("Returns a block from the chain by its index or hash.") |
| 112 | 32 | | .Produces<BlockDto>(StatusCodes.Status200OK) |
| 112 | 33 | | .Produces(StatusCodes.Status404NotFound) |
| 112 | 34 | | .WithOpenApi(); |
| | 35 | |
|
| 112 | 36 | | app.MapPost("/blocks", PostBlock) |
| 112 | 37 | | .WithName("PostBlock") |
| 112 | 38 | | .WithTags("Block") |
| 112 | 39 | | .WithSummary("Add a new block to the blockchain") |
| 112 | 40 | | .WithDescription("Receives a mined block and attempts to add it to the blockchain.") |
| 112 | 41 | | .Accepts<BlockDto>("application/json") |
| 112 | 42 | | .Produces<BlockDto>(StatusCodes.Status201Created) |
| 112 | 43 | | .Produces(StatusCodes.Status400BadRequest) |
| 112 | 44 | | .Produces(StatusCodes.Status422UnprocessableEntity) |
| 112 | 45 | | .WithOpenApi(); |
| 112 | 46 | | } |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Handles GET /blocks/next |
| | 50 | | /// </summary> |
| | 51 | | /// <param name="blockchain">Injected blockchain instance.</param> |
| | 52 | | /// <returns>Returns the next block to be mined.</returns> |
| | 53 | | private static IResult GetNextBlock([FromServices] Domain.Blockchain blockchain) |
| 8 | 54 | | { |
| 8 | 55 | | var info = blockchain.GetNextBlock(); |
| 8 | 56 | | return Results.Ok(BlockInfoMapper.ToDto(info)); |
| 8 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Handles GET /blocks/{indexOrHash} |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="indexOrHash">Block index (int) or hash (string).</param> |
| | 63 | | /// <param name="blockchain">Injected blockchain instance.</param> |
| | 64 | | /// <returns>Returns the block if found, or 404 if not.</returns> |
| | 65 | | private static IResult GetBlockByIndexOrHash( |
| | 66 | | [FromRoute] string indexOrHash, |
| | 67 | | [FromServices] Domain.Blockchain blockchain) |
| 24 | 68 | | { |
| 24 | 69 | | Block? block = int.TryParse(indexOrHash, out var index) |
| 24 | 70 | | ? blockchain.Blocks.ElementAtOrDefault(index) |
| 32 | 71 | | : blockchain.Blocks.FirstOrDefault(b => b.Hash == indexOrHash); |
| | 72 | |
|
| 24 | 73 | | return block is null ? Results.NotFound() : Results.Ok(BlockMapper.ToDto(block)); |
| 24 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Handles POST /blocks |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="context">HTTP context to read request body.</param> |
| | 80 | | /// <param name="blockchain">Injected blockchain instance.</param> |
| | 81 | | /// <returns>Returns 201 if block is added, otherwise error code.</returns> |
| | 82 | | private static async Task<IResult> PostBlock( |
| | 83 | | HttpContext context, |
| | 84 | | [FromServices] Domain.Blockchain blockchain) |
| 24 | 85 | | { |
| 24 | 86 | | var blockDto = await context.Request.ReadFromJsonAsync<BlockDto>(); |
| | 87 | |
|
| 24 | 88 | | if (blockDto is null || string.IsNullOrEmpty(blockDto.Hash)) |
| 8 | 89 | | return Results.UnprocessableEntity("Missing or invalid hash"); |
| | 90 | |
|
| 16 | 91 | | var block = BlockMapper.ToDomain(blockDto); |
| 16 | 92 | | var result = blockchain.AddBlock(block); |
| | 93 | |
|
| 16 | 94 | | return result.Success |
| 16 | 95 | | ? Results.Created($"/blocks/{block.Index}", BlockMapper.ToDto(block)) |
| 16 | 96 | | : Results.BadRequest(result); |
| 24 | 97 | | } |
| | 98 | | } |