diff --git a/My1kWordsEe/Components/Pages/FavoritesPage.razor b/My1kWordsEe/Components/Pages/FavoritesPage.razor index 6010cd6..dcab7d4 100644 --- a/My1kWordsEe/Components/Pages/FavoritesPage.razor +++ b/My1kWordsEe/Components/Pages/FavoritesPage.razor @@ -16,17 +16,18 @@ @inject IdentityUserAccessor UserAccessor @inject AddToFavoritesCommand AddToFavoritesCommand @inject RemoveFromFavoritesCommand RemoveFromFavoritesCommand +@inject ReorderFavoritesCommand ReorderFavoritesCommand @code { [Inject] protected PreloadService PreloadService { get; set; } = default!; - private ClaimsPrincipal? User; + private Data.ApplicationUser? User; private Maybe> Favorites; private List SampleWordsList = new List(); - private void OnWordsListUpdate(SortableListEventArgs args) + private async void OnWordsListUpdate(SortableListEventArgs args) { var itemToMove = SampleWordsList[args.OldIndex]; @@ -36,6 +37,9 @@ SampleWordsList.Insert(args.NewIndex, itemToMove); else SampleWordsList.Add(itemToMove); + + + await ReorderFavoritesCommand.Invoke(User.Id, SampleWordsList); } public record Employee(int Id, string? Name); @@ -45,9 +49,8 @@ var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); if (authState != null) { - User = authState.User; - var user = await UserAccessor.GetRequiredUserAsync(authState.User); - Favorites = await GetFavoritesQuery.Invoke(user.Id); + User = await UserAccessor.GetRequiredUserAsync(authState.User); + Favorites = await GetFavoritesQuery.Invoke(User.Id); SampleWordsList = Favorites.Value.Value.Words.Values.ToList(); } @@ -57,8 +60,7 @@ private async Task AddToFavorites(dynamic favorite) { PreloadService.Show(SpinnerColor.Light, "Saving data..."); - var user = await UserAccessor.GetRequiredUserAsync(User); - Favorites = await AddToFavoritesCommand.Invoke(user.Id, favorite); + Favorites = await AddToFavoritesCommand.Invoke(User.Id, favorite); await Task.Delay(300); PreloadService.Hide(); StateHasChanged(); @@ -67,8 +69,7 @@ private async Task RemoveFromFavorites(dynamic favorite) { PreloadService.Show(SpinnerColor.Light, "Saving data..."); - var user = await UserAccessor.GetRequiredUserAsync(User); - Favorites = await RemoveFromFavoritesCommand.Invoke(user.Id, favorite); + Favorites = await RemoveFromFavoritesCommand.Invoke(User.Id, favorite); await Task.Delay(300); PreloadService.Hide(); StateHasChanged(); diff --git a/My1kWordsEe/Program.cs b/My1kWordsEe/Program.cs index 930770f..12ef360 100644 --- a/My1kWordsEe/Program.cs +++ b/My1kWordsEe/Program.cs @@ -58,6 +58,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/Favorites/ReorderFavoritesCommand.cs b/My1kWordsEe/Services/Cqs/Favorites/ReorderFavoritesCommand.cs new file mode 100644 index 0000000..87b3a7a --- /dev/null +++ b/My1kWordsEe/Services/Cqs/Favorites/ReorderFavoritesCommand.cs @@ -0,0 +1,34 @@ +using CSharpFunctionalExtensions; + +using My1kWordsEe.Models; +using My1kWordsEe.Services.Db; + +namespace My1kWordsEe.Services.Cqs +{ + public class ReorderFavoritesCommand + { + private readonly GetFavoritesQuery getFavoritesCommand; + private readonly AzureStorageClient azureBlobService; + + public ReorderFavoritesCommand( + GetFavoritesQuery getFavoritesCommand, + AzureStorageClient azureBlobService) + { + this.getFavoritesCommand = getFavoritesCommand; + this.azureBlobService = azureBlobService; + } + + public async Task> Invoke(string userId, IEnumerable sampleWords) + { + return await this.getFavoritesCommand.Invoke(userId).Bind(async favorites => + { + var reorderedFavorites = new Favorites( + userId: favorites.UserId, + words: sampleWords.ToDictionary(w => w.EeWord), + sentences: favorites.Sentences); + return await this.azureBlobService.SaveFavorites(reorderedFavorites).Bind(_ => + Result.Success(reorderedFavorites)); + }); + } + } +}