示例#1
0
        public Product AddProduct(Product item)
        {
            _db.Products.Add(item);
            _db.SaveChanges();

            return item;
        }
示例#2
0
        /// <summary>
        /// Insert new product
        /// </summary>
        /// <param name="product">Product object</param>
        /// <returns></returns>
        public IHttpActionResult PostProduct(Product product)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var addedProduct = _prodService.AddProduct(product);
            if (addedProduct == null)
                return BadRequest();

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, product);
        }
示例#3
0
        public Product UpdateProduct(Product item)
        {
            bool productExists = item.Id > 0 && _db.Products.Any(c => c.Id == item.Id);
            if (productExists)
            {
                _db.Entry(item).State = System.Data.Entity.EntityState.Modified;
                _db.SaveChanges();

                return item;
            }

            return null;
        }
示例#4
0
        public Product AddOrUpdateProduct(Product item)
        {
            bool itemExists = item.Id > 0 && _db.Products.Any(p => p.Id == item.Id);

            if (itemExists)
            {
                _db.Entry(item).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                _db.Products.Add(item);
            }

            _db.SaveChanges();

            return item;
        }
示例#5
0
        /// <summary>
        /// Update existing product
        /// </summary>
        /// <param name="id">Product id</param>
        /// <param name="product">Product object</param>
        /// <returns></returns>
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (id != product.Id)
                return BadRequest();

            try
            {
                var updatedProduct = _prodService.UpdateProduct(product);
                if (updatedProduct == null)
                    return NotFound();
            }
            catch (Exception)
            {
                throw;
            }

            return StatusCode(HttpStatusCode.NoContent);
        }