Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fb836ca
#130 - add first class implementation, update packages
albx Jul 21, 2026
d8f3605
#130 - define commands, some refactor and removed unused components
albx Aug 1, 2026
74e8291
#130 - define blog posts endpoints and services
albx Aug 1, 2026
464bbbf
#130 - add creation endpoints, revert usage of FromQuery attribute wi…
albx Aug 3, 2026
3ec4782
#130 - add blog post update endpoint, add generic contents endpoints
albx Aug 11, 2026
5531da1
#130 - add forwarder for blog posts and content, add blank page for b…
albx Aug 13, 2026
e420d08
#130 - update packages
albx Aug 13, 2026
4d08b5a
#130 - add markdown editor
albx Aug 14, 2026
caa44c2
#130 - add write post component
albx Aug 14, 2026
af03566
#130 - change return type for blog endpoints
albx Aug 16, 2026
7d65492
#130 - add import page
albx Aug 18, 2026
361a84c
#130 - setup blog post detail page
albx Aug 18, 2026
8556e28
#130 - update aspire and packages
albx Aug 20, 2026
ce3733b
#130 - add post detail page
albx Aug 20, 2026
c3b5d3b
#130 - add blog posts list page
albx Aug 29, 2026
b573afd
#130 - add UI for post publication
albx Aug 29, 2026
e47c1fc
#130 - publish content
albx Sep 1, 2026
3baa182
#130 - add update and cancel to psot detail form
albx Sep 2, 2026
c7f0e44
#130 - delete blog post
albx Sep 2, 2026
93025a8
#130 - add search and pagination
albx Sep 8, 2026
e084472
#130 - add translations for blog list page
albx Sep 8, 2026
0828f03
#130 - add translations for write, import and post detail pages
albx Sep 8, 2026
2e092b5
#130 - add translations for publish content control and dialog
albx Sep 8, 2026
1c2103d
#130 - update aspire
albx Sep 30, 2026
29aa2d4
#130 - update nuget pacakges
albx Sep 30, 2026
f4f4b54
#130 - apply review comments
albx Sep 30, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/KITT.AppHost/KITT.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<Project Sdk="Aspire.AppHost.Sdk/13.4.0">
<Project Sdk="Aspire.AppHost.Sdk/13.6.0">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>9088a778-e43f-44ea-8c00-27313c124862</UserSecretsId>
<AspireUseCliBundle>true</AspireUseCliBundle>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.Azure" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting.Azure.AppContainers" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting.Azure.KeyVault" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting.Azure.Sql" Version="13.4.0" />
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="13.4.0" />
<PackageReference Include="AzureKeyVaultEmulator.Aspire.Hosting" Version="3.1.0" />
<PackageReference Include="CommunityToolkit.Aspire.Hosting.SqlServer.Extensions" Version="13.3.0" />
<PackageReference Include="Aspire.Hosting.Azure" Version="13.6.0" />
<PackageReference Include="Aspire.Hosting.Azure.AppContainers" Version="13.6.0" />
<PackageReference Include="Aspire.Hosting.Azure.KeyVault" Version="13.6.0" />
<PackageReference Include="Aspire.Hosting.Azure.Sql" Version="13.6.0" />
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="13.6.0" />
<PackageReference Include="AzureKeyVaultEmulator.Aspire.Hosting" Version="3.1.3" />
<PackageReference Include="CommunityToolkit.Aspire.Hosting.SqlServer.Extensions" Version="13.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/KITT.Cms.Settings/KITT.Cms.Settings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Data.Tables" Version="12.11.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.13.0" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions src/KITT.Cms.Web.Api/BlogPosts/BlogPostQueryParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using KITT.Cms.Web.Models;
using KITT.Cms.Web.Models.BlogPosts;
using Microsoft.AspNetCore.Mvc;

namespace KITT.Cms.Web.Api.BlogPosts;

internal record BlogPostQueryParameters
{
[FromQuery(Name = "q")]
public string? Query { get; set; }

[FromQuery]
public ContentStatus? Status { get; set; }

[FromQuery(Name = "sort")]
public SortDirection PublishSort { get; set; } = SortDirection.Ascending;

[FromQuery(Name = "p")]
public int Page { get; set; } = 1;

[FromQuery(Name = "s")]
public int Size { get; set; } = 10;

public static implicit operator BlogPostsQueryModel(BlogPostQueryParameters parameters) =>
new()
{
Query = parameters.Query,
Status = parameters.Status,
PublishSort = parameters.PublishSort,
Page = parameters.Page,
Size = parameters.Size
};
}
125 changes: 125 additions & 0 deletions src/KITT.Cms.Web.Api/BlogPosts/BlogPostsEndpoints.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using KITT.Cms.Web.Models.BlogPosts;
using KITT.Web.Shared.Security;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace KITT.Cms.Web.Api.BlogPosts;

public static class BlogPostsEndpoints
{
extension(IEndpointRouteBuilder builder)
{
public IEndpointRouteBuilder MapBlogPostsEndpoints()
{
var blogPostsGroup = builder
.MapGroup("api/blogposts")
.RequireAuthorization();

blogPostsGroup
.MapGet("", GetAllBlogPosts)
.WithName(nameof(GetAllBlogPosts));

blogPostsGroup
.MapGet("{id:guid}", GetBlogPostDetail)
.WithName(nameof(GetBlogPostDetail));

blogPostsGroup
.MapPost("", PublishBlogPost)
.WithName(nameof(PublishBlogPost));

blogPostsGroup
.MapPost("draft", CreateDraftBlogPost)
.WithName(nameof(CreateDraftBlogPost));

blogPostsGroup
.MapPost("import", ImportBlogPost)
.WithName(nameof(ImportBlogPost));

blogPostsGroup
.MapPut("{id:guid}", UpdateBlogPost)
.WithName(nameof(UpdateBlogPost));

return builder;
}
}

private static async Task<Ok<BlogPostListModel>> GetAllBlogPosts(
BlogPostsEndpointsServices services,
ClaimsPrincipal user,
[AsParameters] BlogPostQueryParameters query)
{
var userId = user.GetUserId();

var model = await services.GetAllBlogPostsAsync(query, userId);
return TypedResults.Ok(model);
}

private static async Task<Results<Ok<BlogPostDetailModel>, NotFound>> GetBlogPostDetail(
BlogPostsEndpointsServices services,
ClaimsPrincipal user,
Guid id)
{
var userId = user.GetUserId();
var model = await services.GetBlogPostDetailAsync(id, userId);
if (model is null)
{
return TypedResults.NotFound();
}

return TypedResults.Ok(model);
}

private static async Task<Results<CreatedAtRoute<BlogPostDetailModel>, BadRequest, ValidationProblem>> PublishBlogPost(
BlogPostsEndpointsServices services,
ClaimsPrincipal user,
[FromBody] PublishBlogPostModel model)
{
var userId = user.GetUserId();
var postId = await services.PublishBlogPostAsync(model, userId);

return TypedResults.CreatedAtRoute(model.ToDetailModel(postId), nameof(GetBlogPostDetail), new { id = postId });
}

private static async Task<Results<CreatedAtRoute<BlogPostDetailModel>, BadRequest, ValidationProblem>> CreateDraftBlogPost(
BlogPostsEndpointsServices services,
ClaimsPrincipal user,
[FromBody] DraftBlogPostModel model)
{
var userId = user.GetUserId();
var postId = await services.CreateDraftBlogPostAsync(model, userId);

return TypedResults.CreatedAtRoute(model.ToDetailModel(postId), nameof(GetBlogPostDetail), new { id = postId });
}

private static async Task<Results<CreatedAtRoute<BlogPostDetailModel>, BadRequest, ValidationProblem>> ImportBlogPost(
BlogPostsEndpointsServices services,
ClaimsPrincipal user,
[FromBody] ImportBlogPostModel model)
{
var userId = user.GetUserId();
var postId = await services.ImportBlogPostAsync(model, userId);

return TypedResults.CreatedAtRoute(model.ToDetailModel(postId), nameof(GetBlogPostDetail), new { id = postId });
}

private static async Task<Results<NoContent, NotFound, BadRequest, ValidationProblem>> UpdateBlogPost(
BlogPostsEndpointsServices services,
ClaimsPrincipal user,
Guid id,
[FromBody] UpdateBlogPostModel model)
{
Comment thread
albx marked this conversation as resolved.
var userId = user.GetUserId();

try
{
await services.UpdateBlogPostAsync(id, model, userId);
return TypedResults.NoContent();
}
catch (InvalidOperationException)
{
return TypedResults.NotFound();
}

}
}
154 changes: 154 additions & 0 deletions src/KITT.Cms.Web.Api/BlogPosts/BlogPostsEndpointsServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using KITT.Cms.Web.Api.Mapping;
using KITT.Cms.Web.Models;
using KITT.Cms.Web.Models.BlogPosts;
using KITT.Core.Commands;
using KITT.Core.ReadModels;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;

namespace KITT.Cms.Web.Api.BlogPosts;

public class BlogPostsEndpointsServices(IDatabase database, IBlogPostCommands commands)
{
public async Task<BlogPostListModel?> GetAllBlogPostsAsync(BlogPostsQueryModel query, string userId)
{
var ascending = query.PublishSort == SortDirection.Ascending;
var postsQuery = database.BlogPosts
.ByUserId(userId)
.OrderedByPublicationDate(ascending);

postsQuery = query.Status switch
{
ContentStatus.Draft => postsQuery.DraftsOnly(),
ContentStatus.Published => postsQuery.PublishedOnly(),
ContentStatus.Unpublished => postsQuery.UnpublishedOnly(),
_ => postsQuery
};

if (!string.IsNullOrWhiteSpace(query.Query))
{
postsQuery = postsQuery.Where(p => p.Title.Contains(query.Query));
}

var skip = (query.Page - 1) * query.Size;

var rawPosts = await postsQuery
.Select(p => new
{
Comment thread
albx marked this conversation as resolved.
p.Id,
p.Title,
p.Slug,
p.PublicationDate,
p.Status
})
.Skip(skip)
.Take(query.Size)
.ToArrayAsync();

// The enum-to-enum cast between the entity's ContentStatus and the model's ContentStatus
// cannot be translated to SQL, so the mapping is performed in memory after materialization.
var posts = rawPosts.Select(p => new BlogPostListModel.BlogPostListItemModel
{
Id = p.Id,
Title = p.Title,
Slug = p.Slug,
PublishedOn = p.PublicationDate,
Status = MapStatus(p.Status)
});

return new()
{
TotalItems = await postsQuery.CountAsync(),
Items = posts,
};
}

private static ContentStatus MapStatus(Core.Models.Content.ContentStatus status) => status switch
{
Core.Models.Content.ContentStatus.Draft => ContentStatus.Draft,
Core.Models.Content.ContentStatus.Published => ContentStatus.Published,
Core.Models.Content.ContentStatus.Unpublished => ContentStatus.Unpublished,
_ => throw new ArgumentOutOfRangeException(nameof(status), status, null)
};

public async Task<BlogPostDetailModel?> GetBlogPostDetailAsync(Guid postId, string userId)
{
var post = await database.BlogPosts
.ByUserId(userId)
.SingleOrDefaultAsync(p => p.Id == postId);

if (post is null)
{
return null;
}

return new()
{
Id = post.Id,
Content = post.Content,
PostAbstract = post.Abstract,
Seo = post.Seo.ToModel(),
Slug = post.Slug,
Title = post.Title,
CreationDate = post.CreationDate,
PublicationDate = post.PublicationDate,
Status = MapStatus(post.Status)
};
}

public Task<Guid> PublishBlogPostAsync(PublishBlogPostModel model, string userId)
{
return commands.PublishPostAsync(
model.Title,
model.Slug,
model.PostAbstract,
model.Content,
model.Seo.ToEntity(),
userId);
}

public Task<Guid> CreateDraftBlogPostAsync(DraftBlogPostModel model, string userId)
{
return commands.CreatePostAsDraftAsync(
model.Title,
model.Slug,
model.PostAbstract,
model.Content,
model.Seo.ToEntity(),
userId);
}

public Task<Guid> ImportBlogPostAsync(ImportBlogPostModel model, string userId)
{
if (!model.CreationDate.HasValue)
{
throw new ValidationException("Creation date is required");
}

if (!model.PublicationDate.HasValue)
{
throw new ValidationException("Publication date is required");
}

return commands.ImportPostAsync(
model.Title,
model.Slug,
model.PostAbstract,
model.Content,
model.CreationDate.Value,
model.PublicationDate.Value,
model.Seo.ToEntity(),
userId);
}

public Task UpdateBlogPostAsync(Guid postId, UpdateBlogPostModel model, string userId)
{
return commands.UpdatePostAsync(
postId,
model.Title,
model.PostAbstract,
model.Content,
model.Seo.ToEntity(),
userId);
}
}
Loading
Loading