refactor!: move API to separate project
This change fundamentally alters URI format
This commit is contained in:
parent
b24e24f3f7
commit
ab76264cd0
|
@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents an API controller which allows reading authors of blog posts.
|
||||||
|
/// </summary>
|
||||||
|
[ApiController]
|
||||||
|
[Route("v{version:apiVersion}/blog/author")]
|
||||||
|
[Produces("application/json")]
|
||||||
|
[ApiVersion(1)]
|
||||||
|
public sealed class AuthorController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="AuthorController" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userService">The <see cref="IUserService" />.</param>
|
||||||
|
public AuthorController(IUserService userService)
|
||||||
|
{
|
||||||
|
_userService = userService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns an object representing the author with the specified ID.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The ID of the author.</param>
|
||||||
|
/// <returns>An object representing the author.</returns>
|
||||||
|
[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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents an API controller which allows reading and writing of blog posts.
|
||||||
|
/// </summary>
|
||||||
|
[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;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="PostController" /> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
|
||||||
|
public PostController(IBlogPostService blogPostService)
|
||||||
|
{
|
||||||
|
_blogPostService = blogPostService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection of all blog posts on the specified page.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="page">The page number.</param>
|
||||||
|
/// <returns>An array of <see cref="IBlogPost" /> objects.</returns>
|
||||||
|
[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<IBlogPost> allPosts = _blogPostService.GetBlogPosts(page, ItemsPerPage);
|
||||||
|
return Ok(allPosts.Select(post => BlogPost.FromBlogPost(post, _blogPostService)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the number of publicly published blog posts.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The number of publicly published blog posts.</returns>
|
||||||
|
[HttpGet("count")]
|
||||||
|
[EndpointDescription("Returns the number of publicly published blog posts.")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||||
|
public IActionResult Count()
|
||||||
|
{
|
||||||
|
return Ok(new { count = _blogPostService.GetBlogPostCount() });
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns an object representing the blog post with the specified ID.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The ID of the blog post.</param>
|
||||||
|
/// <returns>An object representing the blog post.</returns>
|
||||||
|
[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));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a collection of all blog posts which contain the specified tag on the specified page.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="tag">The tag for which to search.</param>
|
||||||
|
/// <param name="page">The page number.</param>
|
||||||
|
/// <returns>An array of <see cref="IBlogPost" /> objects.</returns>
|
||||||
|
[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<IBlogPost> allPosts = _blogPostService.GetBlogPosts(page, ItemsPerPage);
|
||||||
|
allPosts = allPosts.Where(post => post.Tags.Contains(tag)).ToList();
|
||||||
|
return Ok(allPosts.Select(post => BlogPost.FromBlogPost(post, _blogPostService)));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<string> Tags { get; private set; } = ArraySegment<string>.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
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -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"]
|
|
@ -0,0 +1,18 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\OliverBooth.Common\OliverBooth.Common.csproj"/>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
|
@ -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
|
||||||
|
|
||||||
|
###
|
|
@ -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();
|
|
@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
||||||
Gulpfile.mjs = Gulpfile.mjs
|
Gulpfile.mjs = Gulpfile.mjs
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
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}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OliverBooth.Common", "OliverBooth.Common\OliverBooth.Common.csproj", "{77DC9941-E648-442F-935A-C66FC401ECBC}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
|
@ -25,6 +27,10 @@ Global
|
||||||
{A58A6FA3-480C-400B-822A-3786741BF39C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{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.ActiveCfg = Release|Any CPU
|
||||||
{A58A6FA3-480C-400B-822A-3786741BF39C}.Release|Any CPU.Build.0 = 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.ActiveCfg = Debug|Any CPU
|
||||||
{77DC9941-E648-442F-935A-C66FC401ECBC}.Debug|Any CPU.Build.0 = 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
|
{77DC9941-E648-442F-935A-C66FC401ECBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
|
|
@ -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;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Represents a controller for the blog API.
|
|
||||||
/// </summary>
|
|
||||||
[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;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes a new instance of the <see cref="BlogApiController" /> class.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="blogPostService">The <see cref="IBlogPostService" />.</param>
|
|
||||||
/// <param name="userService">The <see cref="IUserService" />.</param>
|
|
||||||
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<IBlogPost> 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<IBlogPost> 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
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,31 +2,33 @@ import BlogPost from "./BlogPost";
|
||||||
import Author from "./Author";
|
import Author from "./Author";
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
private static readonly BASE_URL: string = "/api/v1";
|
|
||||||
private static readonly BLOG_URL: string = "/blog";
|
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<number> {
|
static async getBlogPostCount(): Promise<number> {
|
||||||
const response = await API.get(`${API.BLOG_URL}/count`);
|
const response = await API.get(`${API.POST_URL}/count`);
|
||||||
return response.count;
|
return response.count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getBlogPost(id: string): Promise<BlogPost> {
|
static async getBlogPost(id: string): Promise<BlogPost> {
|
||||||
const response = await API.get(`${API.BLOG_URL}/post/${id}`);
|
const response = await API.get(`${API.POST_URL}/${id}`);
|
||||||
return new BlogPost(response);
|
return new BlogPost(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getBlogPosts(page: number): Promise<BlogPost[]> {
|
static async getBlogPosts(page: number): Promise<BlogPost[]> {
|
||||||
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));
|
return response.map(obj => new BlogPost(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getBlogPostsByTag(tag: string, page: number): Promise<BlogPost[]> {
|
static async getBlogPostsByTag(tag: string, page: number): Promise<BlogPost[]> {
|
||||||
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));
|
return response.map(obj => new BlogPost(obj));
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getAuthor(id: string): Promise<Author> {
|
static async getAuthor(id: string): Promise<Author> {
|
||||||
const response = await API.get(`${API.BLOG_URL}/author/${id}`);
|
const response = await API.get(`${API.AUTHOR_URL}/${id}`);
|
||||||
return new Author(response);
|
return new Author(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue