Skip to content

Commit

Permalink
+ ReorderFavoritesCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklausBrain committed Dec 26, 2024
1 parent c9e70b0 commit aac3ade
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
19 changes: 10 additions & 9 deletions My1kWordsEe/Components/Pages/FavoritesPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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<Result<Favorites>> Favorites;

private List<SampleWord> SampleWordsList = new List<SampleWord>();

private void OnWordsListUpdate(SortableListEventArgs args)
private async void OnWordsListUpdate(SortableListEventArgs args)
{
var itemToMove = SampleWordsList[args.OldIndex];

Expand All @@ -36,6 +37,9 @@
SampleWordsList.Insert(args.NewIndex, itemToMove);
else
SampleWordsList.Add(itemToMove);


await ReorderFavoritesCommand.Invoke(User.Id, SampleWordsList);

Check warning on line 42 in My1kWordsEe/Components/Pages/FavoritesPage.razor

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
}

public record Employee(int Id, string? Name);
Expand All @@ -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();
}

Expand All @@ -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);

Check warning on line 63 in My1kWordsEe/Components/Pages/FavoritesPage.razor

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
await Task.Delay(300);
PreloadService.Hide();
StateHasChanged();
Expand All @@ -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);

Check warning on line 72 in My1kWordsEe/Components/Pages/FavoritesPage.razor

View workflow job for this annotation

GitHub Actions / test

Dereference of a possibly null reference.
await Task.Delay(300);
PreloadService.Hide();
StateHasChanged();
Expand Down
1 change: 1 addition & 0 deletions My1kWordsEe/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static WebApplication BuildWebHost(string[] args)
builder.Services.AddSingleton<RemoveFromFavoritesCommand>();
builder.Services.AddSingleton<RedoSampleWordCommand>();
builder.Services.AddSingleton<ValidateSampleWordCommand>();
builder.Services.AddSingleton<ReorderFavoritesCommand>();

// Blazor-specific services
builder.Services
Expand Down
34 changes: 34 additions & 0 deletions My1kWordsEe/Services/Cqs/Favorites/ReorderFavoritesCommand.cs
Original file line number Diff line number Diff line change
@@ -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<Result<Favorites>> Invoke(string userId, IEnumerable<SampleWord> 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));
});
}
}
}

0 comments on commit aac3ade

Please sign in to comment.