diff --git a/OliverBooth.Api/Controllers/Blog/AuthorController.cs b/OliverBooth.Api/Controllers/Blog/AuthorController.cs
new file mode 100644
index 0000000..9c605e2
--- /dev/null
+++ b/OliverBooth.Api/Controllers/Blog/AuthorController.cs
@@ -0,0 +1,47 @@
+using Asp.Versioning;
+using Microsoft.AspNetCore.Mvc;
+using OliverBooth.Api.Data;
+using OliverBooth.Common.Data.Web.Users;
+using OliverBooth.Common.Services;
+
+namespace OliverBooth.Api.Controllers.Blog;
+
+///
+/// Represents an API controller which allows reading authors of blog posts.
+///
+[ApiController]
+[Route("v{version:apiVersion}/blog/author")]
+[Produces("application/json")]
+[ApiVersion(1)]
+public sealed class AuthorController : ControllerBase
+{
+ private readonly IUserService _userService;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The .
+ public AuthorController(IUserService userService)
+ {
+ _userService = userService;
+ }
+
+ ///
+ /// Returns an object representing the author with the specified ID.
+ ///
+ /// The ID of the author.
+ /// An object representing the author.
+ [HttpGet("{id:guid}")]
+ [EndpointDescription("Returns an object representing the author with the specified ID.")]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(Author))]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public IActionResult GetAuthor(Guid id)
+ {
+ if (!_userService.TryGetUser(id, out IUser? author))
+ {
+ return NotFound();
+ }
+
+ return Ok(Author.FromUser(author));
+ }
+}
diff --git a/OliverBooth.Api/Controllers/Blog/PostController.cs b/OliverBooth.Api/Controllers/Blog/PostController.cs
new file mode 100644
index 0000000..e6c788b
--- /dev/null
+++ b/OliverBooth.Api/Controllers/Blog/PostController.cs
@@ -0,0 +1,92 @@
+using Asp.Versioning;
+using Microsoft.AspNetCore.Mvc;
+using OliverBooth.Api.Data;
+using OliverBooth.Common.Data.Blog;
+using OliverBooth.Common.Services;
+
+namespace OliverBooth.Api.Controllers.Blog;
+
+///
+/// Represents an API controller which allows reading and writing of blog posts.
+///
+[ApiController]
+[Route("v{version:apiVersion}/blog/post")]
+[Produces("application/json")]
+[ApiVersion(1)]
+public sealed class PostController : ControllerBase
+{
+ private const int ItemsPerPage = 10;
+ private readonly IBlogPostService _blogPostService;
+
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ /// The .
+ public PostController(IBlogPostService blogPostService)
+ {
+ _blogPostService = blogPostService;
+ }
+
+ ///
+ /// Returns a collection of all blog posts on the specified page.
+ ///
+ /// The page number.
+ /// An array of objects.
+ [HttpGet("all/{page:int?}")]
+ [EndpointDescription("Returns a collection of all blog posts on the specified page.")]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BlogPost[]))]
+ public IActionResult All(int page = 0)
+ {
+ IReadOnlyList allPosts = _blogPostService.GetBlogPosts(page, ItemsPerPage);
+ return Ok(allPosts.Select(post => BlogPost.FromBlogPost(post, _blogPostService)));
+ }
+
+ ///
+ /// Returns the number of publicly published blog posts.
+ ///
+ /// The number of publicly published blog posts.
+ [HttpGet("count")]
+ [EndpointDescription("Returns the number of publicly published blog posts.")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ public IActionResult Count()
+ {
+ return Ok(new { count = _blogPostService.GetBlogPostCount() });
+ }
+
+ ///
+ /// Returns an object representing the blog post with the specified ID.
+ ///
+ /// The ID of the blog post.
+ /// An object representing the blog post.
+ [HttpGet("{id:guid}")]
+ [EndpointDescription("Returns an object representing the blog post with the specified ID.")]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BlogPost))]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public IActionResult GetPost(Guid id)
+ {
+ if (!_blogPostService.TryGetPost(id, out IBlogPost? post))
+ {
+ return NotFound();
+ }
+
+ return Ok(BlogPost.FromBlogPost(post, _blogPostService, true));
+ }
+
+ ///
+ /// Returns a collection of all blog posts which contain the specified tag on the specified page.
+ ///
+ /// The tag for which to search.
+ /// The page number.
+ /// An array of objects.
+ [HttpGet("tagged/{tag}/{page:int?}")]
+ [EndpointDescription("Returns a collection of all blog posts which contain the specified tag on the specified page.")]
+ [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BlogPost[]))]
+ public IActionResult Tagged(string tag, int page = 0)
+ {
+ tag = tag.Replace('-', ' ').ToLowerInvariant();
+
+ IReadOnlyList allPosts = _blogPostService.GetBlogPosts(page, ItemsPerPage);
+ allPosts = allPosts.Where(post => post.Tags.Contains(tag)).ToList();
+ return Ok(allPosts.Select(post => BlogPost.FromBlogPost(post, _blogPostService)));
+ }
+}
diff --git a/OliverBooth.Api/Data/Author.cs b/OliverBooth.Api/Data/Author.cs
new file mode 100644
index 0000000..0d0b73b
--- /dev/null
+++ b/OliverBooth.Api/Data/Author.cs
@@ -0,0 +1,27 @@
+using System.Text.Json.Serialization;
+using OliverBooth.Common.Data.Blog;
+using OliverBooth.Common.Data.Web.Users;
+
+namespace OliverBooth.Api.Data;
+
+internal sealed class Author
+{
+ [JsonPropertyName("avatarUrl"), JsonInclude, JsonPropertyOrder(2)]
+ public Uri AvatarUrl { get; private set; } = null!;
+
+ [JsonPropertyName("id"), JsonInclude, JsonPropertyOrder(0)]
+ public Guid Id { get; private set; }
+
+ [JsonPropertyName("name"), JsonInclude, JsonPropertyOrder(1)]
+ public string Name { get; private set; } = string.Empty;
+
+ public static Author FromUser(IUser author)
+ {
+ return new Author
+ {
+ Id = author.Id,
+ Name = author.DisplayName,
+ AvatarUrl = author.AvatarUrl
+ };
+ }
+}
diff --git a/OliverBooth.Api/Data/BlogPost.cs b/OliverBooth.Api/Data/BlogPost.cs
new file mode 100644
index 0000000..c7c295c
--- /dev/null
+++ b/OliverBooth.Api/Data/BlogPost.cs
@@ -0,0 +1,82 @@
+using System.Text.Json.Serialization;
+using Humanizer;
+using OliverBooth.Common.Data.Blog;
+using OliverBooth.Common.Services;
+
+namespace OliverBooth.Api.Data;
+
+internal sealed class BlogPost
+{
+ [JsonPropertyName("author"), JsonInclude, JsonPropertyOrder(3)]
+ public Guid Author { get; private set; }
+
+ [JsonPropertyName("commentsEnabled"), JsonInclude, JsonPropertyOrder(1)]
+ public bool CommentsEnabled { get; private set; }
+
+ [JsonPropertyName("content"), JsonInclude, JsonPropertyOrder(11)]
+ public string? Content { get; private set; }
+
+ [JsonPropertyName("excerpt"), JsonInclude, JsonPropertyOrder(10)]
+ public string Excerpt { get; private set; } = string.Empty;
+
+ [JsonPropertyName("formattedPublishDate"), JsonInclude, JsonPropertyOrder(7)]
+ public string FormattedPublishDate { get; private set; } = string.Empty;
+
+ [JsonPropertyName("formattedUpdateDate"), JsonInclude, JsonPropertyOrder(8)]
+ public string? FormattedUpdateDate { get; private set; }
+
+ [JsonPropertyName("humanizedTimestamp"), JsonInclude, JsonPropertyOrder(9)]
+ public string HumanizedTimestamp { get; private set; } = string.Empty;
+
+ [JsonPropertyName("id"), JsonInclude, JsonPropertyOrder(0)]
+ public Guid Id { get; private set; }
+
+ [JsonPropertyName("identifier"), JsonInclude, JsonPropertyOrder(2)]
+ public string Identifier { get; private set; } = string.Empty;
+
+ [JsonPropertyName("trimmed"), JsonInclude, JsonPropertyOrder(12)]
+ public bool IsTrimmed { get; private set; }
+
+ [JsonPropertyName("published"), JsonInclude, JsonPropertyOrder(5)]
+ public long Published { get; private set; }
+
+ [JsonPropertyName("tags"), JsonInclude, JsonPropertyOrder(13)]
+ public IEnumerable Tags { get; private set; } = ArraySegment.Empty;
+
+ [JsonPropertyName("title"), JsonInclude, JsonPropertyOrder(4)]
+ public string Title { get; private set; } = string.Empty;
+
+ [JsonPropertyName("updated"), JsonInclude, JsonPropertyOrder(6)]
+ public long? Updated { get; private set; }
+
+ [JsonPropertyName("url"), JsonInclude] public object Url { get; private set; } = null!;
+
+ public static BlogPost FromBlogPost(IBlogPost post, IBlogPostService blogPostService,
+ bool includeContent = false)
+ {
+ return new()
+ {
+ Id = post.Id,
+ CommentsEnabled = post.EnableComments,
+ Identifier = post.GetDisqusIdentifier(),
+ Author = post.Author.Id,
+ Title = post.Title,
+ Published = post.Published.ToUnixTimeSeconds(),
+ Updated = post.Updated?.ToUnixTimeSeconds(),
+ FormattedPublishDate = post.Published.ToString("dddd, d MMMM yyyy HH:mm"),
+ FormattedUpdateDate = post.Updated?.ToString("dddd, d MMMM yyyy HH:mm"),
+ HumanizedTimestamp = post.Updated?.Humanize() ?? post.Published.Humanize(),
+ Excerpt = blogPostService.RenderExcerpt(post, out bool trimmed),
+ Content = includeContent ? blogPostService.RenderPost(post) : null,
+ IsTrimmed = trimmed,
+ Tags = post.Tags.Select(t => t.Replace(' ', '-')),
+ Url = new
+ {
+ year = post.Published.ToString("yyyy"),
+ month = post.Published.ToString("MM"),
+ day = post.Published.ToString("dd"),
+ slug = post.Slug
+ }
+ };
+ }
+}
diff --git a/OliverBooth.Api/Dockerfile b/OliverBooth.Api/Dockerfile
new file mode 100644
index 0000000..5db56cc
--- /dev/null
+++ b/OliverBooth.Api/Dockerfile
@@ -0,0 +1,22 @@
+FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
+USER $APP_UID
+WORKDIR /app
+EXPOSE 80
+EXPOSE 443
+
+FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
+ARG BUILD_CONFIGURATION=Release
+WORKDIR /src
+COPY ["OliverBooth.Api/OliverBooth.Api.csproj", "OliverBooth.Api/"]
+COPY . .
+
+WORKDIR "/src/OliverBooth.Api"
+
+FROM build AS publish
+ARG BUILD_CONFIGURATION=Release
+RUN dotnet publish "OliverBooth.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false
+
+FROM base AS final
+WORKDIR /app
+COPY --from=publish /app/publish .
+ENTRYPOINT ["dotnet", "OliverBooth.Api.dll"]
diff --git a/OliverBooth.Api/OliverBooth.Api.csproj b/OliverBooth.Api/OliverBooth.Api.csproj
new file mode 100644
index 0000000..9918e70
--- /dev/null
+++ b/OliverBooth.Api/OliverBooth.Api.csproj
@@ -0,0 +1,18 @@
+
+
+
+ net8.0
+ enable
+ enable
+ Linux
+
+
+
+
+
+
+
+
+
+
+
diff --git a/OliverBooth.Api/OliverBooth.Api.http b/OliverBooth.Api/OliverBooth.Api.http
new file mode 100644
index 0000000..a0ac0cd
--- /dev/null
+++ b/OliverBooth.Api/OliverBooth.Api.http
@@ -0,0 +1,7 @@
+@OliverBooth.Api_HostAddress = https://localhost:2844
+@TestPostId = bdcd9789-f0a6-4721-a66d-b6f7e2acd0f7
+
+GET {{OliverBooth.Api_HostAddress}}/v1/blog/post/{{TestPostId}}
+Accept: application/json
+
+###
diff --git a/OliverBooth.Api/Program.cs b/OliverBooth.Api/Program.cs
new file mode 100644
index 0000000..9394630
--- /dev/null
+++ b/OliverBooth.Api/Program.cs
@@ -0,0 +1,54 @@
+using Asp.Versioning;
+using OliverBooth.Common.Extensions;
+using Serilog;
+
+Log.Logger = new LoggerConfiguration()
+ .WriteTo.Console()
+ .WriteTo.File("logs/latest.log", rollingInterval: RollingInterval.Day)
+#if DEBUG
+ .MinimumLevel.Debug()
+#endif
+ .CreateLogger();
+
+WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
+builder.Configuration.AddTomlFile("data/config.toml", true, true);
+builder.Logging.ClearProviders();
+builder.Logging.AddSerilog();
+
+builder.Services.AddCors(options =>
+{
+ options.AddPolicy("localhost", policy => policy.WithOrigins("https://localhost:2845"));
+ options.AddPolicy("site", policy => policy.WithOrigins("https://oliverbooth.dev"));
+});
+
+builder.Services.AddCommonServices();
+builder.Services.AddEndpointsApiExplorer();
+builder.Services.AddSwaggerGen();
+builder.Services.AddControllers();
+builder.Services.AddRouting(options => options.LowercaseUrls = true);
+builder.Services.AddApiVersioning(options =>
+{
+ options.AssumeDefaultVersionWhenUnspecified = true;
+ options.DefaultApiVersion = new ApiVersion(1);
+ options.ReportApiVersions = true;
+ options.ApiVersionReader = new UrlSegmentApiVersionReader();
+});
+
+if (builder.Environment.IsProduction())
+{
+ builder.WebHost.AddCertificateFromEnvironment(2844, 5048);
+}
+
+WebApplication app = builder.Build();
+
+if (app.Environment.IsDevelopment())
+{
+ app.UseSwagger();
+ app.UseSwaggerUI();
+}
+
+app.UseHttpsRedirection();
+app.MapControllers();
+app.UseCors(app.Environment.IsDevelopment() ? "localhost" : "site");
+
+app.Run();
diff --git a/OliverBooth.sln b/OliverBooth.sln
index fa82af3..fbabd7b 100644
--- a/OliverBooth.sln
+++ b/OliverBooth.sln
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Gulpfile.mjs = Gulpfile.mjs
EndProjectSection
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OliverBooth.Api", "OliverBooth.Api\OliverBooth.Api.csproj", "{37423EB9-C025-45C5-B3B9-B09610FC4829}"
+EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OliverBooth.Common", "OliverBooth.Common\OliverBooth.Common.csproj", "{77DC9941-E648-442F-935A-C66FC401ECBC}"
EndProject
Global
@@ -25,6 +27,10 @@ Global
{A58A6FA3-480C-400B-822A-3786741BF39C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.Build.0 = Release|Any CPU
+ {37423EB9-C025-45C5-B3B9-B09610FC4829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {37423EB9-C025-45C5-B3B9-B09610FC4829}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {37423EB9-C025-45C5-B3B9-B09610FC4829}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {37423EB9-C025-45C5-B3B9-B09610FC4829}.Release|Any CPU.Build.0 = Release|Any CPU
{77DC9941-E648-442F-935A-C66FC401ECBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77DC9941-E648-442F-935A-C66FC401ECBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77DC9941-E648-442F-935A-C66FC401ECBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
diff --git a/OliverBooth/Controllers/Api/v1/BlogApiController.cs b/OliverBooth/Controllers/Api/v1/BlogApiController.cs
deleted file mode 100644
index a6ccc67..0000000
--- a/OliverBooth/Controllers/Api/v1/BlogApiController.cs
+++ /dev/null
@@ -1,112 +0,0 @@
-using Asp.Versioning;
-using Humanizer;
-using Microsoft.AspNetCore.Mvc;
-using OliverBooth.Common.Data.Blog;
-using OliverBooth.Common.Data.Web.Users;
-using OliverBooth.Common.Services;
-
-namespace OliverBooth.Controllers.Api.v1;
-
-///
-/// Represents a controller for the blog API.
-///
-[ApiController]
-[Route("api/v{version:apiVersion}/blog")]
-[Produces("application/json")]
-[ApiVersion(1)]
-public sealed class BlogApiController : ControllerBase
-{
- private readonly IBlogPostService _blogPostService;
- private readonly IUserService _userService;
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The .
- /// The .
- public BlogApiController(IBlogPostService blogPostService, IUserService userService)
- {
- _blogPostService = blogPostService;
- _userService = userService;
- }
-
- [Route("count")]
- public IActionResult Count()
- {
- return Ok(new { count = _blogPostService.GetBlogPostCount() });
- }
-
- [HttpGet("posts/{page:int?}")]
- public IActionResult GetAllBlogPosts(int page = 0)
- {
- const int itemsPerPage = 10;
- IReadOnlyList allPosts = _blogPostService.GetBlogPosts(page, itemsPerPage);
- return Ok(allPosts.Select(post => CreatePostObject(post)));
- }
-
- [HttpGet("posts/tagged/{tag}/{page:int?}")]
- public IActionResult GetTaggedBlogPosts(string tag, int page = 0)
- {
- const int itemsPerPage = 10;
- tag = tag.Replace('-', ' ').ToLowerInvariant();
-
- IReadOnlyList allPosts = _blogPostService.GetBlogPosts(page, itemsPerPage);
- allPosts = allPosts.Where(post => post.Tags.Contains(tag)).ToList();
- return Ok(allPosts.Select(post => CreatePostObject(post)));
- }
-
- [HttpGet("author/{id:guid}")]
- public IActionResult GetAuthor(Guid id)
- {
- if (!_userService.TryGetUser(id, out IUser? author))
- {
- return NotFound();
- }
-
- return Ok(new
- {
- id = author.Id,
- name = author.DisplayName,
- avatarUrl = author.AvatarUrl,
- });
- }
-
- [HttpGet("post/{id:guid?}")]
- public IActionResult GetPost(Guid id)
- {
- if (!_blogPostService.TryGetPost(id, out IBlogPost? post))
- {
- return NotFound();
- }
-
- return Ok(CreatePostObject(post, true));
- }
-
- private object CreatePostObject(IBlogPost post, bool includeContent = false)
- {
- return new
- {
- id = post.Id,
- commentsEnabled = post.EnableComments,
- identifier = post.GetDisqusIdentifier(),
- author = post.Author.Id,
- title = post.Title,
- published = post.Published.ToUnixTimeSeconds(),
- updated = post.Updated?.ToUnixTimeSeconds(),
- formattedPublishDate = post.Published.ToString("dddd, d MMMM yyyy HH:mm"),
- formattedUpdateDate = post.Updated?.ToString("dddd, d MMMM yyyy HH:mm"),
- humanizedTimestamp = post.Updated?.Humanize() ?? post.Published.Humanize(),
- excerpt = _blogPostService.RenderExcerpt(post, out bool trimmed),
- content = includeContent ? _blogPostService.RenderPost(post) : null,
- trimmed,
- tags = post.Tags.Select(t => t.Replace(' ', '-')),
- url = new
- {
- year = post.Published.ToString("yyyy"),
- month = post.Published.ToString("MM"),
- day = post.Published.ToString("dd"),
- slug = post.Slug
- }
- };
- }
-}
diff --git a/src/ts/app/API.ts b/src/ts/app/API.ts
index 4863db5..b41cf6a 100644
--- a/src/ts/app/API.ts
+++ b/src/ts/app/API.ts
@@ -2,31 +2,33 @@ import BlogPost from "./BlogPost";
import Author from "./Author";
class API {
- private static readonly BASE_URL: string = "/api/v1";
private static readonly BLOG_URL: string = "/blog";
+ private static readonly BASE_URL: string = `https://localhost:2840/v1${API.BLOG_URL}`;
+ private static readonly AUTHOR_URL: string = "/author";
+ private static readonly POST_URL: string = "/post";
static async getBlogPostCount(): Promise {
- const response = await API.get(`${API.BLOG_URL}/count`);
+ const response = await API.get(`${API.POST_URL}/count`);
return response.count;
}
static async getBlogPost(id: string): Promise {
- const response = await API.get(`${API.BLOG_URL}/post/${id}`);
+ const response = await API.get(`${API.POST_URL}/${id}`);
return new BlogPost(response);
}
static async getBlogPosts(page: number): Promise {
- const response = await API.get(`${API.BLOG_URL}/posts/${page}`);
+ const response = await API.get(`${API.POST_URL}/all/${page}`);
return response.map(obj => new BlogPost(obj));
}
static async getBlogPostsByTag(tag: string, page: number): Promise {
- const response = await API.get(`${API.BLOG_URL}/posts/tagged/${tag}/${page}`);
+ const response = await API.get(`${API.POST_URL}/tagged/${tag}/${page}`);
return response.map(obj => new BlogPost(obj));
}
static async getAuthor(id: string): Promise {
- const response = await API.get(`${API.BLOG_URL}/author/${id}`);
+ const response = await API.get(`${API.AUTHOR_URL}/${id}`);
return new Author(response);
}