public override async Task <CartModel> DoHandleRequest(UpdateCartItemsRequest request, CancellationToken cancellationToken)
        {
            var currentCart = await RecipeDomainContext.GetCurrentCart(request.User);

            if (currentCart == null)
            {
                throw new NoCurrentCartException($"No current cart exists [User ID = {request.User.Id}");
            }

            var cartItems = request.CartItemUpdates.Select(i => new CartItem()
            {
                CartId       = currentCart.Id,
                CreatedAt    = DateTime.UtcNow,
                IngredientId = i.IngredientId
            });

            currentCart.Items = cartItems.ToList();

            await RecipeDomainContext.SaveChangesAsync();

            // Refresh the current cart entity
            currentCart = await RecipeDomainContext.GetCurrentCart(request.User);

            return(CartModel.From(currentCart));
        }
        /// <inheritdoc/>
        public override async Task <CartModel> Handle(GetCurrentCartQuery request, CancellationToken cancellationToken)
        {
            var cart = await RecipeDomainContext.GetCurrentCart(request.User);

            return(cart == null ?
                   throw new NoCurrentCartException($"No current cart exists [User ID = {request.User.Id}]") :
                   CartModel.From(cart));
        }
        public override async Task <Unit> Handle(DeleteRecipeByIdRequest request, CancellationToken cancellationToken)
        {
            var recipeToRemove = await RecipeDomainContext
                                 .GetRecipesForUser(request.User)
                                 .FirstOrDefaultAsync(r => r.Id == request.RecipeId);

            if (recipeToRemove != null)
            {
                // First, remove any cart items associated with ingredients from this recipe.
                var ingredientIds = new HashSet <Guid>(
                    recipeToRemove.Ingredients.Select(i => i.Id));
                var cart = await RecipeDomainContext.GetCurrentCart(request.User);

                cart.Items = cart.Items.Where(ci => !ingredientIds.Contains(ci.Ingredient.Id)).ToList();

                // Now, delete the recipe.
                RecipeDomainContext.Recipes.Remove(recipeToRemove);

                await RecipeDomainContext.SaveChangesAsync();
            }

            return(Unit.Value);
        }