From 3dd303ba175cfd52b1f57268bca5b983834943a4 Mon Sep 17 00:00:00 2001 From: NICK BRAIN Date: Thu, 5 Dec 2024 20:32:31 +0200 Subject: [PATCH] + RedoSampleWordCommand --- My1kWordsEe/Components/Pages/WordPage.razor | 49 ++++++++++++++-- My1kWordsEe/Program.cs | 1 + .../Services/Cqs/RedoSampleWordCommand.cs | 56 +++++++++++++++++++ 3 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs diff --git a/My1kWordsEe/Components/Pages/WordPage.razor b/My1kWordsEe/Components/Pages/WordPage.razor index db92663..a27e5b1 100644 --- a/My1kWordsEe/Components/Pages/WordPage.razor +++ b/My1kWordsEe/Components/Pages/WordPage.razor @@ -22,13 +22,14 @@ @inject AddToFavoritesCommand AddToFavoritesCommand @inject RemoveFromFavoritesCommand RemoveFromFavoritesCommand @inject DeleteSampleSentenceCommand DeleteSampleSentenceCommand +@inject RedoSampleWordCommand RedoSampleWordCommand @code { [Inject] protected PreloadService PreloadService { get; set; } = default!; [Inject] protected ToastService ToastService { get; set; } = default!; - private ConfirmDialog DeleteSampleDialog = default!; + private ConfirmDialog ConfirmDialog = default!; [Parameter] public required string EeWord { get; init; } @@ -90,7 +91,7 @@ StateHasChanged(); } - private async Task DeleteSample(SampleSentence sampleSentence) + private async Task DeleteSampleSentence(SampleSentence sampleSentence) { var options = new ConfirmDialogOptions { @@ -100,7 +101,7 @@ AutoFocusYesButton = false, }; - var confirmation = await DeleteSampleDialog.ShowAsync( + var confirmation = await ConfirmDialog.ShowAsync( title: "Are you sure you want to delete this?", message1: "This will delete the record. Once deleted can not be rolled back.", message2: "Do you want to proceed?", options); @@ -126,6 +127,42 @@ } } + private async Task RedoSample() + { + var options = new ConfirmDialogOptions + { + IsScrollable = true, + Dismissable = true, + IsVerticallyCentered = true, + AutoFocusYesButton = false, + }; + + var confirmation = await ConfirmDialog.ShowAsync( + title: "Is this word described inadequately?", + message1: "This will regenerate the word explanation and all the samples will removed.", + message2: "Do you want to proceed?", options); + + if (confirmation) + { + PreloadService.Show(SpinnerColor.Light, "Saving data..."); + var redoResult = await this.RedoSampleWordCommand.Invoke(Value.EeWord); + if (redoResult.IsSuccess) + { + WordMetadata = redoResult; + StateHasChanged(); + } + else + { + ToastService.Notify(new ToastMessage(ToastType.Warning, redoResult.Error)); + } + PreloadService.Hide(); + } + else + { + ToastService.Notify(new ToastMessage(ToastType.Secondary, $"Redo action canceled.")); + } + } + private bool IsSampleGenerationInProgress = false; private async Task GenerateSample(MouseEventArgs e) @@ -190,7 +227,7 @@ @if (IsLoggedIn) { - + } @@ -224,7 +261,7 @@ {
+ DeleteSample="DeleteSampleSentence" IsFavorite="IsFavorite(sample)">
} @@ -249,5 +286,5 @@ - + \ No newline at end of file diff --git a/My1kWordsEe/Program.cs b/My1kWordsEe/Program.cs index 44cdbfa..6b36e1a 100644 --- a/My1kWordsEe/Program.cs +++ b/My1kWordsEe/Program.cs @@ -56,6 +56,7 @@ public static WebApplication BuildWebHost(string[] args) builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); // Blazor-specific services builder.Services diff --git a/My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs b/My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs new file mode 100644 index 0000000..4abc255 --- /dev/null +++ b/My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs @@ -0,0 +1,56 @@ +using CSharpFunctionalExtensions; + +using My1kWordsEe.Models; +using My1kWordsEe.Services.Db; + +namespace My1kWordsEe.Services.Cqs +{ + public class RedoSampleWordCommand + { + private readonly AzureStorageClient azureBlobService; + private readonly AddSampleWordCommand addSampleWordCommand; + private readonly DeleteSampleSentenceCommand deleteSampleSentenceCommand; + + public RedoSampleWordCommand( + AzureStorageClient azureBlobService, + AddSampleWordCommand addSampleWordCommand, + DeleteSampleSentenceCommand deleteSampleSentenceCommand) + { + this.azureBlobService = azureBlobService; + this.addSampleWordCommand = addSampleWordCommand; + this.deleteSampleSentenceCommand = deleteSampleSentenceCommand; + } + + public async Task> Invoke(string eeWord) + { + if (!eeWord.ValidateWord()) + { + return Result.Failure("Not an Estonian word"); + } + + (await azureBlobService.GetWordData(eeWord)).Deconstruct( + out bool _, + out bool isBlobAccessFailure, + out Maybe savedWord, + out string blobAccessError); + + if (isBlobAccessFailure) + { + return Result.Failure(blobAccessError); + } + + var redoTask = this.addSampleWordCommand.Invoke(eeWord); + + if (savedWord.HasValue) + { + await Parallel.ForEachAsync(savedWord.Value.Samples, async (sample, ct) => + { + if (ct.IsCancellationRequested) { return; } + await deleteSampleSentenceCommand.Invoke(sample); + }); + } + + return await redoTask; + } + } +} \ No newline at end of file