From fc9604972860bc8d5d29dcd96618ffd96a90275b Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 28 Nov 2023 22:23:49 +0100 Subject: [PATCH] Basic management of tags and categories --- .../Admin/BlogCategoriesManagerViewModel.cs | 5 ++++- .../Admin/BlogPostsManagerViewModel.cs | 2 +- .../Admin/BlogTagsManagerViewModel.cs | 5 ++++- .../MZikmund.Web.Core/Blog/DeleteTagHandler.cs | 18 ++++++++++++++++++ .../Configurations/PostConfiguration.cs | 4 ++-- 5 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/web/MZikmund.Web.Core/Blog/DeleteTagHandler.cs diff --git a/src/app/MZikmund/ViewModels/Admin/BlogCategoriesManagerViewModel.cs b/src/app/MZikmund/ViewModels/Admin/BlogCategoriesManagerViewModel.cs index 0e927957..54bc2e30 100644 --- a/src/app/MZikmund/ViewModels/Admin/BlogCategoriesManagerViewModel.cs +++ b/src/app/MZikmund/ViewModels/Admin/BlogCategoriesManagerViewModel.cs @@ -44,7 +44,7 @@ private async Task RefreshListAsync() //TODO: Refresh collection based on IDs var categories = await _api.GetCategoriesAsync(); Categories.Clear(); - Categories.AddRange(categories.Content!); + Categories.AddRange(categories.Content!.OrderBy(t => t.DisplayName)); } catch (Exception ex) { @@ -68,6 +68,7 @@ private async Task AddCategoryAsync() return; } + using var loadingScope = _loadingIndicator.BeginLoading(); var apiResponse = await _api.AddCategoryAsync(new Category() { DisplayName = viewModel.Category.DisplayName, @@ -91,6 +92,8 @@ private async Task UpdateCategoryAsync(Category? category) RouteName = category.RouteName }); var result = await _dialogService.ShowAsync(viewModel); + + using var loadingScope = _loadingIndicator.BeginLoading(); if (result == ContentDialogResult.Primary) { var apiResponse = await _api.UpdateCategoryAsync(category.Id, new EditCategory() diff --git a/src/app/MZikmund/ViewModels/Admin/BlogPostsManagerViewModel.cs b/src/app/MZikmund/ViewModels/Admin/BlogPostsManagerViewModel.cs index 4945d539..b854f873 100644 --- a/src/app/MZikmund/ViewModels/Admin/BlogPostsManagerViewModel.cs +++ b/src/app/MZikmund/ViewModels/Admin/BlogPostsManagerViewModel.cs @@ -5,5 +5,5 @@ namespace MZikmund.ViewModels.Admin; public class BlogPostsManagerViewModel : PageViewModel { - public override string Title => Localizer.Instance.GetString("BlogPosts"); + public override string Title => Localizer.Instance.GetString("Posts"); } diff --git a/src/app/MZikmund/ViewModels/Admin/BlogTagsManagerViewModel.cs b/src/app/MZikmund/ViewModels/Admin/BlogTagsManagerViewModel.cs index 33145bf3..58eabf8e 100644 --- a/src/app/MZikmund/ViewModels/Admin/BlogTagsManagerViewModel.cs +++ b/src/app/MZikmund/ViewModels/Admin/BlogTagsManagerViewModel.cs @@ -44,7 +44,7 @@ private async Task RefreshListAsync() //TODO: Refresh collection based on IDs var tags = await _api.GetTagsAsync(); Tags.Clear(); - Tags.AddRange(tags.Content!); + Tags.AddRange(tags.Content!.OrderBy(t => t.DisplayName)); } catch (Exception ex) { @@ -68,6 +68,7 @@ private async Task AddTagAsync() return; } + using var loadingScope = _loadingIndicator.BeginLoading(); var apiResponse = await _api.AddTagAsync(new Tag() { DisplayName = viewModel.Tag.DisplayName, @@ -91,6 +92,8 @@ private async Task UpdateTagAsync(Tag? tag) RouteName = tag.RouteName }); var result = await _dialogService.ShowAsync(viewModel); + + using var loadingScope = _loadingIndicator.BeginLoading(); if (result == ContentDialogResult.Primary) { var apiResponse = await _api.UpdateTagAsync(tag.Id, new EditTag() diff --git a/src/web/MZikmund.Web.Core/Blog/DeleteTagHandler.cs b/src/web/MZikmund.Web.Core/Blog/DeleteTagHandler.cs new file mode 100644 index 00000000..2cfa8fd8 --- /dev/null +++ b/src/web/MZikmund.Web.Core/Blog/DeleteTagHandler.cs @@ -0,0 +1,18 @@ +using MediatR; +using MZikmund.Web.Data.Entities; +using MZikmund.Web.Data.Infrastructure; + +namespace MZikmund.Web.Core.Blog; + +public class DeleteTagHandler : IRequestHandler +{ + private readonly IRepository _categoriesRepository; + + public DeleteTagHandler(IRepository categoriesRepository) + { + _categoriesRepository = categoriesRepository ?? throw new ArgumentNullException(nameof(categoriesRepository)); + } + + public async Task Handle(DeleteTagCommand request, CancellationToken cancellationToken) => + await _categoriesRepository.DeleteAsync(request.TagId, cancellationToken); +} diff --git a/src/web/MZikmund.Web.Data/Entities/Configurations/PostConfiguration.cs b/src/web/MZikmund.Web.Data/Entities/Configurations/PostConfiguration.cs index 9da1be4e..8bdc2ad6 100644 --- a/src/web/MZikmund.Web.Data/Entities/Configurations/PostConfiguration.cs +++ b/src/web/MZikmund.Web.Data/Entities/Configurations/PostConfiguration.cs @@ -22,7 +22,7 @@ public void Configure(EntityTypeBuilder builder) j => j .HasOne(pt => pt.Category) .WithMany() - .HasForeignKey(pt => pt.CategoryId), + .HasForeignKey(pt => pt.CategoryId), // TODO: Restrict delete j => j .HasOne(pt => pt.Post) .WithMany() @@ -35,7 +35,7 @@ public void Configure(EntityTypeBuilder builder) j => j .HasOne(pt => pt.Tag) .WithMany() - .HasForeignKey(pt => pt.TagId), + .HasForeignKey(pt => pt.TagId), // TODO: Restrict delete j => j .HasOne(pt => pt.Post) .WithMany()