public async Task <string> Handle(SaveApprenticeshipFavouriteBasketCommand request, CancellationToken cancellationToken) { _logger.LogInformation("Handling SaveApprenticeshipFavouriteCommand for basket: {basketId}", request.BasketId); var basketContentTask = _basketStore.GetAsync(request.BasketId); var employerAccountId = request.ChosenHashedAccountId ?? await GetEmployerAccountId(request.UserId); var favouritesTask = _readRepository.GetApprenticeshipFavourites(employerAccountId); await Task.WhenAll(basketContentTask, favouritesTask); var favourites = favouritesTask.Result ?? new Domain.ReadModel.ApprenticeshipFavourites(); var basketContent = basketContentTask.Result; var writeModel = favourites.MapToWriteModel(); if (basketContent == null || !basketContent.Any()) { return(employerAccountId); } bool changesMade = false; foreach (var item in basketContent) { changesMade |= writeModel.Update(item.ApprenticeshipId, item.Providers); } if (changesMade) { await _writeRepository.SaveApprenticeshipFavourites(employerAccountId, writeModel); _logger.LogDebug("Updated basket: {basketId}", request.BasketId); } else { _logger.LogDebug("No changes required for basket: {basketId}", request.BasketId); } _logger.LogDebug("Post save clean up. Deleting basket: {basketId}", request.BasketId); await _basketStore.RemoveAsync(request.BasketId); return(employerAccountId); }
public async Task <Guid> Handle(RemoveBasketCommand request, CancellationToken cancellationToken) { await _basketStore.RemoveAsync(request.BasketId.Value); return(Guid.Empty); }