Skip to content

Commit

Permalink
+ RedoSampleWordCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklausBrain committed Dec 5, 2024
1 parent 50ffa5e commit 3dd303b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
49 changes: 43 additions & 6 deletions My1kWordsEe/Components/Pages/WordPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -90,7 +91,7 @@
StateHasChanged();
}

private async Task DeleteSample(SampleSentence sampleSentence)
private async Task DeleteSampleSentence(SampleSentence sampleSentence)
{
var options = new ConfirmDialogOptions
{
Expand All @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -190,7 +227,7 @@

@if (IsLoggedIn)
{
<i role="button" class="bi bi-arrow-clockwise float-end"></i>
<i role="button" class="bi bi-arrow-clockwise float-end" @onclick="RedoSample"></i>
}
</h1>

Expand Down Expand Up @@ -224,7 +261,7 @@
{
<div class="col col-12 col-sm-6 p-1">
<SampleV2 Sample="@sample" AddToFavorites="AddToFavorites" RemoveFromFavorites="RemoveFromFavorites"
DeleteSample="DeleteSample" IsFavorite="IsFavorite(sample)">
DeleteSample="DeleteSampleSentence" IsFavorite="IsFavorite(sample)">
</SampleV2>
</div>
}
Expand All @@ -249,5 +286,5 @@
</div>

<Preload LoadingText="Loading..." />
<ConfirmDialog @ref="DeleteSampleDialog" />
<ConfirmDialog @ref="ConfirmDialog" />
<Toasts class="p-3" AutoHide="true" StackLength="3" Placement="ToastsPlacement.TopRight" />
1 change: 1 addition & 0 deletions My1kWordsEe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static WebApplication BuildWebHost(string[] args)
builder.Services.AddSingleton<GetFavoritesQuery>();
builder.Services.AddSingleton<AddToFavoritesCommand>();
builder.Services.AddSingleton<RemoveFromFavoritesCommand>();
builder.Services.AddSingleton<RedoSampleWordCommand>();

// Blazor-specific services
builder.Services
Expand Down
56 changes: 56 additions & 0 deletions My1kWordsEe/Services/Cqs/RedoSampleWordCommand.cs
Original file line number Diff line number Diff line change
@@ -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<Result<SampleWord>> Invoke(string eeWord)
{
if (!eeWord.ValidateWord())
{
return Result.Failure<SampleWord>("Not an Estonian word");
}

(await azureBlobService.GetWordData(eeWord)).Deconstruct(
out bool _,
out bool isBlobAccessFailure,
out Maybe<SampleWord> savedWord,
out string blobAccessError);

if (isBlobAccessFailure)
{
return Result.Failure<SampleWord>(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;
}
}
}

0 comments on commit 3dd303b

Please sign in to comment.