From b6d3eb72fe93ce21234edcd8e6736d953807b35d Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sun, 25 Feb 2024 15:40:58 +0000 Subject: [PATCH] feat: add api versioning --- .../Controllers/Api/{ => v1}/BlogApiController.cs | 6 ++++-- OliverBooth/OliverBooth.csproj | 1 + OliverBooth/Program.cs | 9 +++++++++ src/ts/API.ts | 2 +- 4 files changed, 15 insertions(+), 3 deletions(-) rename OliverBooth/Controllers/Api/{ => v1}/BlogApiController.cs (96%) diff --git a/OliverBooth/Controllers/Api/BlogApiController.cs b/OliverBooth/Controllers/Api/v1/BlogApiController.cs similarity index 96% rename from OliverBooth/Controllers/Api/BlogApiController.cs rename to OliverBooth/Controllers/Api/v1/BlogApiController.cs index d11e09d..3b88341 100644 --- a/OliverBooth/Controllers/Api/BlogApiController.cs +++ b/OliverBooth/Controllers/Api/v1/BlogApiController.cs @@ -1,17 +1,19 @@ +using Asp.Versioning; using Humanizer; using Microsoft.AspNetCore.Mvc; using OliverBooth.Data.Blog; using OliverBooth.Data.Web; using OliverBooth.Services; -namespace OliverBooth.Controllers.Api; +namespace OliverBooth.Controllers.Api.v1; /// /// Represents a controller for the blog API. /// [ApiController] -[Route("api/blog")] +[Route("api/v{version:apiVersion}/blog")] [Produces("application/json")] +[ApiVersion(1)] public sealed class BlogApiController : ControllerBase { private readonly IBlogPostService _blogPostService; diff --git a/OliverBooth/OliverBooth.csproj b/OliverBooth/OliverBooth.csproj index 6edb58b..b827e3d 100644 --- a/OliverBooth/OliverBooth.csproj +++ b/OliverBooth/OliverBooth.csproj @@ -9,6 +9,7 @@ + diff --git a/OliverBooth/Program.cs b/OliverBooth/Program.cs index 7d2e7d0..a122bab 100644 --- a/OliverBooth/Program.cs +++ b/OliverBooth/Program.cs @@ -1,3 +1,4 @@ +using Asp.Versioning; using AspNetCore.ReCaptcha; using Markdig; using OliverBooth.Data.Blog; @@ -31,6 +32,14 @@ builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder() .UseSmartyPants() .Build()); +builder.Services.AddApiVersioning(options => +{ + options.AssumeDefaultVersionWhenUnspecified = true; + options.DefaultApiVersion = new ApiVersion(1); + options.ReportApiVersions = true; + options.ApiVersionReader = new UrlSegmentApiVersionReader(); +}); + builder.Services.AddDbContextFactory(); builder.Services.AddDbContextFactory(); builder.Services.AddHttpClient(); diff --git a/src/ts/API.ts b/src/ts/API.ts index a1e070d..fc946dc 100644 --- a/src/ts/API.ts +++ b/src/ts/API.ts @@ -2,7 +2,7 @@ import BlogPost from "./BlogPost"; import Author from "./Author"; class API { - private static readonly BASE_URL: string = "/api"; + private static readonly BASE_URL: string = "/api/v1"; private static readonly BLOG_URL: string = "/blog"; static async getBlogPostCount(): Promise {