public IHttpActionResult PutRecipe(int id, RecipeDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var recipe = MatsMatModelFactory.Create(dto);

            if (id != recipe.RecipeID)
            {
                return BadRequest();
            }

            foreach (var inst in recipe.Instructions)
            {
                if (inst.RecipeID == recipe.RecipeID && inst.InstructionID > 0)
                {
                    MatsMatRepo.Entry(inst).State = EntityState.Modified;
                }
            }

            List<Instruction> oldInstructions = MatsMatRepo.GetInstructions().Where(x => x.RecipeID == recipe.RecipeID).ToList();
            List<Instruction> addedInstructions = recipe.Instructions.ExceptBy(oldInstructions, x => x.InstructionID).ToList();
            List<Instruction> deletedInstructions = oldInstructions.ExceptBy(recipe.Instructions, x => x.InstructionID).ToList();

            addedInstructions.ForEach(x => MatsMatRepo.Entry(x).State = EntityState.Added);
            deletedInstructions.ForEach(x => MatsMatRepo.Entry(x).State = EntityState.Deleted);

            foreach (var item in addedInstructions)
            {
                item.RecipeID = recipe.RecipeID;
            }

            MatsMatRepo.Entry(recipe).State = EntityState.Modified;

            try
            {
                MatsMatRepo.SaveAll();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MatsMatRepo.RecipeExist(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemple #2
0
 public Recipe Create(RecipeDTO recipe)
 {
     return new Recipe()
     {
         RecipeID = recipe.RecipeID,
         Name = recipe.Name,
         ShortDescription = recipe.ShortDescription,
         CookingTime = recipe.CookingTime != null ? recipe.CookingTime : 0,
         TypeID = recipe.TypeID,
         MainIngredientID = recipe.MainIngredientID,
         ImageUrl = recipe.ImageUrl != null && recipe.ImageUrl != string.Empty
             ? recipe.ImageUrl
             : WebApiApplication.AzureBlogStorageUrl + "recipes/pannkaka.jpg",
         Instructions = (from item in recipe.Instructions
                         select new Instruction()
                         {
                             InstructionID = item.InstructionID,
                             Text = item.Text,
                             Index = item.Index,
                             RecipeID = item.RecipeID
                         }).ToList()
     };
 }
        public IHttpActionResult PostRecipe(RecipeDTO dto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var recipe = MatsMatModelFactory.Create(dto);

            if(MatsMatRepo.AddRecipe(recipe) != null && MatsMatRepo.SaveAll())
            {
                return CreatedAtRoute("DefaultApi", new { id = recipe.RecipeID }, recipe);
            }

            return InternalServerError();
        }