Skip to content

Commit

Permalink
Refactor UpdatePostCommandHandler to improve post updates
Browse files Browse the repository at this point in the history
Refactored the `UpdatePostCommandHandler` class to enhance the `Handle` method by adding steps to update tags and categories before saving changes. Introduced `UpdateTags` method for tag processing and refactored it to use `tagString` parameter. Removed redundant calls to `UpdateCats`, `UpdateAsync`, `_cache.Remove`, and `_logger.LogInformation`. Ensured cache is cleared and log entry is created after updating the post.
  • Loading branch information
EdiWang committed Nov 1, 2024
1 parent 99abf41 commit 810a40b
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Moonglade.Core/PostFeature/UpdatePostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,23 @@ public async Task<PostEntity> Handle(UpdatePostCommand request, CancellationToke

UpdatePostDetails(post, postEditModel, utcNow);

await UpdateTags(post, postEditModel.Tags, ct);
await UpdateCats(post, postEditModel.SelectedCatIds, ct);

await _postRepo.UpdateAsync(post, ct);

_cache.Remove(BlogCachePartition.Post.ToString(), post.RouteLink);

_logger.LogInformation($"Post updated: {post.Id}");
return post;
}

private async Task UpdateTags(PostEntity post, string tagString, CancellationToken ct)
{
// 1. Add new tags to tag lib
var tags = string.IsNullOrWhiteSpace(postEditModel.Tags) ?
var tags = string.IsNullOrWhiteSpace(tagString) ?
[] :
postEditModel.Tags.Split(',').ToArray();
tagString.Split(',').ToArray();

foreach (var item in tags)
{
Expand Down Expand Up @@ -92,15 +105,6 @@ await _tagRepo.AddAsync(new()
if (tag is not null) post.Tags.Add(tag);
}
}

await UpdateCats(post, postEditModel.SelectedCatIds, ct);

await _postRepo.UpdateAsync(post, ct);

_cache.Remove(BlogCachePartition.Post.ToString(), post.RouteLink);

_logger.LogInformation($"Post updated: {post.Id}");
return post;
}

private void UpdatePostDetails(PostEntity post, PostEditModel postEditModel, DateTime utcNow)
Expand Down

0 comments on commit 810a40b

Please sign in to comment.