Esempio n. 1
0
        public async Task<IHttpActionResult> Edit(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != product.ProductID)
            {
                return BadRequest();
            }

            try
            {
                var model = ProductRepository.FindOneBy(x => x.ProductID == id);

                if (model != null)
                {
                    model.ProductName = product.ProductName;
                    model.SupplierID = product.SupplierID;
                    model.CategoryID = product.CategoryID;
                    model.QuantityPerUnit = product.QuantityPerUnit;
                    model.UnitPrice = product.UnitPrice;
                    model.UnitsInStock = product.UnitsInStock;
                    model.UnitsOnOrder = product.UnitsOnOrder;
                    model.ReorderLevel = product.ReorderLevel;
                    model.Discontinued = product.Discontinued;
                }

                await ProductRepository.UpdateAsync(model);
            }
            catch (DbUpdateConcurrencyException)
            {
                var productToEdit = ProductRepository.FindOneBy(x => x.ProductID == id);

                if (productToEdit != null)
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Esempio n. 2
0
        public IHttpActionResult Create(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                var model = new Product()
                {
                    ProductName = product.ProductName,
                    SupplierID = product.SupplierID,
                    CategoryID = product.CategoryID,
                    QuantityPerUnit = product.QuantityPerUnit,
                    UnitPrice = product.UnitPrice,
                    UnitsInStock = product.UnitsInStock,
                    UnitsOnOrder = product.UnitsOnOrder,
                    ReorderLevel = product.ReorderLevel,
                    Discontinued = product.Discontinued
                };

                ProductRepository.Save(model);
            }
            catch (Exception)
            {
                throw;
            }

            return CreatedAtRoute("DefaultApi", new { id = product.ProductID }, product);
        }