Skip to content

Commit

Permalink
Re-organize endpoint/handler classes
Browse files Browse the repository at this point in the history
  • Loading branch information
kMutagene committed Feb 21, 2024
1 parent 90db549 commit 1195ff3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 46 deletions.
34 changes: 34 additions & 0 deletions src/PackageRegistryService/API/Endpoints/PackagesEndpointsV1.cs
Original file line number Diff line number Diff line change
@@ -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<APIKeyEndpointFilter>(); // creating packages via post requests requires an API key

return group.WithTags("Validation Packages");
}
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
Original file line number Diff line number Diff line change
@@ -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<Ok<ValidationPackage[]>> GetAllPackages(ValidationPackageDb database)
Expand Down Expand Up @@ -73,39 +67,5 @@ public static async Task<Results<Ok<ValidationPackage>, Conflict, UnauthorizedHt

return TypedResults.Ok(package);
}

public static async Task<Results<Ok, UnprocessableEntity>> 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<APIKeyEndpointFilter>(); // creating packages via post requests requires an API key

// verify endpoints
group.MapPost("/verify/{name}/{version}", Verify)
.WithOpenApi()
.WithName("Verify");

return group;
}
}
}
13 changes: 13 additions & 0 deletions src/PackageRegistryService/API/Handlers/VerificationHandlers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;

namespace PackageRegistryService.API.Handlers
{
public class VerificationHandlers
{
public static async Task<Results<Ok, UnprocessableEntity>> Verify(string name, string version, [FromBody] string hash)
{
return TypedResults.UnprocessableEntity();
}
}
}
9 changes: 6 additions & 3 deletions src/PackageRegistryService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 1195ff3

Please sign in to comment.