-
Notifications
You must be signed in to change notification settings - Fork 3
Add blog management #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
albx
wants to merge
26
commits into
main
Choose a base branch
from
130-add-blog-management
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add blog management #131
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 d8f3605
#130 - define commands, some refactor and removed unused components
albx 74e8291
#130 - define blog posts endpoints and services
albx 464bbbf
#130 - add creation endpoints, revert usage of FromQuery attribute wi…
albx 3ec4782
#130 - add blog post update endpoint, add generic contents endpoints
albx 5531da1
#130 - add forwarder for blog posts and content, add blank page for b…
albx e420d08
#130 - update packages
albx 4d08b5a
#130 - add markdown editor
albx caa44c2
#130 - add write post component
albx af03566
#130 - change return type for blog endpoints
albx 7d65492
#130 - add import page
albx 361a84c
#130 - setup blog post detail page
albx 8556e28
#130 - update aspire and packages
albx ce3733b
#130 - add post detail page
albx c3b5d3b
#130 - add blog posts list page
albx b573afd
#130 - add UI for post publication
albx e47c1fc
#130 - publish content
albx 3baa182
#130 - add update and cancel to psot detail form
albx c7f0e44
#130 - delete blog post
albx 93025a8
#130 - add search and pagination
albx e084472
#130 - add translations for blog list page
albx 0828f03
#130 - add translations for write, import and post detail pages
albx 2e092b5
#130 - add translations for publish content control and dialog
albx 1c2103d
#130 - update aspire
albx 29aa2d4
#130 - update nuget pacakges
albx f4f4b54
#130 - apply review comments
albx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| 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
154
src/KITT.Cms.Web.Api/BlogPosts/BlogPostsEndpointsServices.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
|
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); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.