From 1195ff345ec9af5449e339c9306474ab829e4cf1 Mon Sep 17 00:00:00 2001 From: Kevin Schneider Date: Wed, 21 Feb 2024 13:44:40 +0100 Subject: [PATCH] Re-organize endpoint/handler classes --- .../API/Endpoints/PackagesEndpointsV1.cs | 34 ++++++++++++++ .../API/Endpoints/VerificationEndpointsV1.cs | 17 +++++++ .../PackageHandlers.cs} | 46 ++----------------- .../API/Handlers/VerificationHandlers.cs | 13 ++++++ src/PackageRegistryService/Program.cs | 9 ++-- 5 files changed, 73 insertions(+), 46 deletions(-) create mode 100644 src/PackageRegistryService/API/Endpoints/PackagesEndpointsV1.cs create mode 100644 src/PackageRegistryService/API/Endpoints/VerificationEndpointsV1.cs rename src/PackageRegistryService/API/{EndpointsV1.cs => Handlers/PackageHandlers.cs} (62%) create mode 100644 src/PackageRegistryService/API/Handlers/VerificationHandlers.cs diff --git a/src/PackageRegistryService/API/Endpoints/PackagesEndpointsV1.cs b/src/PackageRegistryService/API/Endpoints/PackagesEndpointsV1.cs new file mode 100644 index 0000000..ad09369 --- /dev/null +++ b/src/PackageRegistryService/API/Endpoints/PackagesEndpointsV1.cs @@ -0,0 +1,34 @@ +using Microsoft.AspNetCore.Http.HttpResults; +using PackageRegistryService.Authentication; +using Microsoft.AspNetCore.Mvc; +using PackageRegistryService.API.Handlers; + +namespace PackageRegistryService.API.Endpoints +{ + public static class PackagesEndpointsV1 + { + public static RouteGroupBuilder MapPackagesApiV1(this RouteGroupBuilder group) + { + + // packages endpoints + group.MapGet("/", PackageHandlers.GetAllPackages) + .WithOpenApi() + .WithName("GetAllPackages"); + + group.MapGet("/{name}", PackageHandlers.GetLatestPackageByName) + .WithOpenApi() + .WithName("GetLatestPackageByName"); + + group.MapGet("/{name}/{version}", PackageHandlers.GetPackageByNameAndVersion) + .WithOpenApi() + .WithName("GetPackageByNameAndVersion"); + + group.MapPost("/", PackageHandlers.CreatePackage) + .WithOpenApi() + .WithName("CreatePackage") + .AddEndpointFilter(); // creating packages via post requests requires an API key + + return group.WithTags("Validation Packages"); + } + } +} diff --git a/src/PackageRegistryService/API/Endpoints/VerificationEndpointsV1.cs b/src/PackageRegistryService/API/Endpoints/VerificationEndpointsV1.cs new file mode 100644 index 0000000..f6cea4b --- /dev/null +++ b/src/PackageRegistryService/API/Endpoints/VerificationEndpointsV1.cs @@ -0,0 +1,17 @@ +using PackageRegistryService.API.Handlers; +using PackageRegistryService.Authentication; + +namespace PackageRegistryService.API.Endpoints +{ + public static class VerificationEndpointsV1 + { + public static RouteGroupBuilder MapVerificationApiV1(this RouteGroupBuilder group) + { + group.MapPost("/{name}/{version}", VerificationHandlers.Verify) + .WithOpenApi() + .WithName("Verify"); + + return group.WithTags("Content Verification"); + } + } +} diff --git a/src/PackageRegistryService/API/EndpointsV1.cs b/src/PackageRegistryService/API/Handlers/PackageHandlers.cs similarity index 62% rename from src/PackageRegistryService/API/EndpointsV1.cs rename to src/PackageRegistryService/API/Handlers/PackageHandlers.cs index 99a338b..45f12fd 100644 --- a/src/PackageRegistryService/API/EndpointsV1.cs +++ b/src/PackageRegistryService/API/Handlers/PackageHandlers.cs @@ -1,16 +1,10 @@ using Microsoft.AspNetCore.Http.HttpResults; -using Microsoft.EntityFrameworkCore; -using PackageRegistryService; using PackageRegistryService.Models; -using PackageRegistryService.Pages; -using Microsoft.AspNetCore.HttpOverrides; -using PackageRegistryService.Authentication; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; -namespace PackageRegistryService.API +namespace PackageRegistryService.API.Handlers { - public static class APIEndpointsV1 + public class PackageHandlers { // get all validation packages public static async Task> GetAllPackages(ValidationPackageDb database) @@ -73,39 +67,5 @@ public static async Task, Conflict, UnauthorizedHt return TypedResults.Ok(package); } - - public static async Task> Verify(string name, string version, [FromBody] string hash) - { - return TypedResults.UnprocessableEntity(); - } - - public static RouteGroupBuilder MapApiV1(this RouteGroupBuilder group) - { - - // packages endpoints - group.MapGet("/packages", GetAllPackages) - .WithOpenApi() - .WithName("GetAllPackages"); - - group.MapGet("/packages/{name}", GetLatestPackageByName) - .WithOpenApi() - .WithName("GetLatestPackageByName"); - - group.MapGet("/packages/{name}/{version}", GetPackageByNameAndVersion) - .WithOpenApi() - .WithName("GetPackageByNameAndVersion"); - - group.MapPost("/packages", CreatePackage) - .WithOpenApi() - .WithName("CreatePackage") - .AddEndpointFilter(); // creating packages via post requests requires an API key - - // verify endpoints - group.MapPost("/verify/{name}/{version}", Verify) - .WithOpenApi() - .WithName("Verify"); - - return group; - } } } diff --git a/src/PackageRegistryService/API/Handlers/VerificationHandlers.cs b/src/PackageRegistryService/API/Handlers/VerificationHandlers.cs new file mode 100644 index 0000000..3e086e5 --- /dev/null +++ b/src/PackageRegistryService/API/Handlers/VerificationHandlers.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.Mvc; + +namespace PackageRegistryService.API.Handlers +{ + public class VerificationHandlers + { + public static async Task> Verify(string name, string version, [FromBody] string hash) + { + return TypedResults.UnprocessableEntity(); + } + } +} diff --git a/src/PackageRegistryService/Program.cs b/src/PackageRegistryService/Program.cs index f9c5b03..e69cb42 100644 --- a/src/PackageRegistryService/Program.cs +++ b/src/PackageRegistryService/Program.cs @@ -11,6 +11,7 @@ using Microsoft.OpenApi.Models; using NSwag.Generation.Processors.Security; using NSwag.Generation.Processors; +using PackageRegistryService.API.Endpoints; // ------------------------- ApplicationBuilder ------------------------- // in this section, we will add the necessary code to configure the application builder, @@ -85,9 +86,11 @@ // app.MapGet binds a response handler function to a HTTP request on a specific route pattern -app.MapGroup("/api/v1") - .MapApiV1() - .WithTags("Packages"); +app.MapGroup("/api/v1/packages") + .MapPackagesApiV1(); + +app.MapGroup("/api/v1/verify") + .MapVerificationApiV1(); app.MapGroup("/") .MapPageEndpoints();