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)); }
public override async Task <CartModel> DoHandleRequest(CreateCartRequest request, CancellationToken cancellationToken) { var cart = new Domain.Entities.Cart() { UserId = request.User.Id, CreatedAt = DateTime.UtcNow, IsCurrent = true, Items = new CartItem[] { } }; await RecipeDomainContext.Carts.AddAsync(cart); await RecipeDomainContext.SaveChangesAsync(); return(CartModel.From(cart)); }
public override async Task <RecipeModel> DoHandleRequest(CreateRecipeRequest request, CancellationToken cancellationToken) { var recipe = new Recipe() { Name = request.Name, Ingredients = new Ingredient[] { }, Instructions = new Instruction[] { }, RecipeGroupLinks = new RecipeGroupLink[] {}, UserId = request.User.Id }; await RecipeDomainContext.Recipes.AddAsync(recipe, cancellationToken); await RecipeDomainContext.SaveChangesAsync(); return(RecipeModel.From(recipe)); }
public override async Task <RecipeModel> DoHandleRequest(UpdateRecipeRequest request, CancellationToken cancellationToken) { var recipe = await RecipeDomainContext .GetRecipesForUser(request.User) .FirstOrDefaultAsync(r => r.Id == request.RecipeId); if (recipe == null) { throw new RecipeNotFoundException($"No recipe found [ID = {request.RecipeId}]"); } recipe.Name = request.Name; await RecipeDomainContext.SaveChangesAsync(); return(RecipeModel.From(recipe)); }
/// <inheritdoc/> public override async Task <RecipeModel> DoHandleRequest(UpdateRecipeGroupsRequest request, CancellationToken cancellationToken) { var recipe = await RecipeDomainContext .GetRecipesForUser(request.User) .FirstOrDefaultAsync(r => r.Id == request.RecipeId); if (recipe == null) { throw new RecipeNotFoundException($"No recipe found [ID = {request.RecipeId}]"); } // First, create any new recipe groups. var newRecipeGroups = await CreateNewRecipeGroups(request); // Now, update the recipe group links recipe.RecipeGroupLinks = newRecipeGroups.Select(rg => new RecipeGroupLink() { RecipeId = recipe.Id, RecipeGroupId = rg.Id }).ToList(); foreach (var recipeGroup in request.RecipeGroupsToAssociate) { recipe.RecipeGroupLinks.Add(new RecipeGroupLink() { RecipeId = recipe.Id, RecipeGroupId = recipeGroup.RecipeGroupId }); } await RecipeDomainContext.SaveChangesAsync(); // Refresh data recipe = await RecipeDomainContext .GetRecipesForUser(request.User) .FirstOrDefaultAsync(r => r.Id == request.RecipeId); return(RecipeModel.From(recipe)); }
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); }
public override async Task <RecipeModel> DoHandleRequest(UpdateRecipeIngredientsRequest request, CancellationToken cancellationToken) { var recipe = await RecipeDomainContext .GetRecipesForUser(request.User) .FirstOrDefaultAsync(r => r.Id == request.RecipeId); var existingIngredientCategories = await RecipeDomainContext .IngredientCategories .ForUser(request.User) .ToListAsync(); if (recipe == null) { throw new RecipeNotFoundException($"No recipe found [ID = {request.RecipeId}]"); } var ingredients = request.Ingredients.Select(i => new Ingredient() { RecipeId = request.RecipeId, Name = i.Name, Amount = i.Amount, // Try to find the existing category, creating a new one if none is found. Category = existingIngredientCategories .FirstOrDefault(ic => ic.Name == i.Category) ?? new IngredientCategory() { Name = i.Category, UserId = request.User.Id } }); recipe.Ingredients = ingredients.ToList(); await RecipeDomainContext.SaveChangesAsync(); return(RecipeModel.From(recipe)); }