Exemplo n.º 1
0
        public IHttpActionResult PostIngredient(IngredientDTO ingredientDTO)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var id = ((ClaimsIdentity)User.Identity).GetUserId();
            Ingredient ingredient = new Ingredient()
            {
                Name = ingredientDTO.Name,
                QuantityInventory = ingredientDTO.QuantityInventory,
                CreatedBy = id,
                CreatedDate = DateTime.Now,
                ModifiedBy = id,
                ModifiedDate = DateTime.Now,
                IsActive = true,
                RestaurantId = id,
                Unit = db.Units.Where(x => x.UnitId == ingredientDTO.Unit.UnitId).FirstOrDefault()
            };

            try
            {

                db.Ingredients.Add(ingredient);
                db.SaveChanges();

            }
            catch (Exception e)
            {
                ErrorSignal.FromCurrentContext().Raise(e);
                throw new Exception("There was a problem saving this record: " + e.Message);

            }

            return CreatedAtRoute("GetIngredientId", new { id = ingredient.IngredientId }, ingredient);
        }
Exemplo n.º 2
0
        public IHttpActionResult PutIngredient(int id, Ingredient ingredient)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != ingredient.IngredientId)
            {
                return BadRequest();
            }

            db.Entry(ingredient).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!IngredientExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }