public async Task <IActionResult> PutCategory(int id, Category category) { if (id != category.Id) { return(BadRequest()); } _context.Entry(category).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutRecipe([FromRoute] int id, [FromBody] Recipe recipe) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != recipe.Id) { return(BadRequest()); } _context.Entry(recipe).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!RecipeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Response <RecipeResponse> > AddRecipe(RecipeRequest recipeRequest, Guid userId) { var amount = new RecipeAmount() { AmountTypeId = await GetAmountTypeId(recipeRequest.Amount.Type), Min = recipeRequest.Amount.Min, Max = recipeRequest.Amount.Max }; await _context.AddAsync(amount); await _context.SaveChangesAsync(); var recipe = new Recipe() { Description = recipeRequest.Description, Name = recipeRequest.Name, Private = recipeRequest.Private, RecipeAmountId = amount.Id, UserId = userId, VideoId = recipeRequest.VideoId }; await _context.AddAsync(recipe); await _context.SaveChangesAsync(); if (recipeRequest.Ingredients != null) { var ingredients = recipeRequest.Ingredients.Select(x => new Ingredient() { Amount = x.Amount, Name = x.Name, RecipeId = recipe.Id, Unit = x.Unit }); await _context.AddRangeAsync(ingredients); } if (recipeRequest.Links != null) { var links = recipeRequest.Links.Select(x => new Link() { RecipeId = recipe.Id, Url = x.Url }); await _context.AddRangeAsync(links); } await _context.SaveChangesAsync(); return(new Response <RecipeResponse>() { Data = _mapper.Convert(recipe, false, userId), Success = true, }); //var ingredients = recipeResponse.Ingredients.Select(x => new Ingredient() { RecipeId = recipeResponse }) }