public bool Update(Product item)
 {
     int index = products.FindIndex(p => p.Id == item.Id);
     products.RemoveAt(index);
     products.Insert(index, item);
     return true;
 }
Пример #2
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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

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

            return StatusCode(HttpStatusCode.NoContent);
        }
        public Product Add(Product item)
        {
            item.Id = _nextId++;
            products.Add(item);

            return item;
        }
Пример #4
0
 /// <summary>
 /// Creates a new product with default values
 /// </summary>
 /// <returns></returns>
 internal Product Create()
 {
     Product product = new Product
     {
       ReleaseDate = DateTime.Now
     };
     return product;
 }
 //Update a product
 public IEnumerable<Product> PutProduct(int id, Product product)
 {
     product.Id = id;
     if (repository.Update(product))
     {
         return repository.GetAll();
     }
     else
     {
         return null;
     }
 }
Пример #6
0
        public IHttpActionResult PostProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Product.Add(product);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product);
        }
Пример #7
0
        /// <summary>
        /// Saves a new product.
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        internal Product Save(Product product)
        {
            // Read in the existing products
            var products = this.Retrieve();

            // Assign a new Id
            var maxId = products.Max(p => p.ProductId);
            product.ProductId = maxId + 1;
            products.Add(product);

            WriteData(products);
            return product;
        }
Пример #8
0
        /// <summary>
        /// Updates an existing product
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        internal Product Save(int id, Product product)
        {
            // Read in the existing products
            var products = this.Retrieve();

            // Locate and replace the item
            var itemIndex = products.FindIndex(p => p.ProductId == product.ProductId);
            if (itemIndex > 0)
            {
              products[itemIndex] = product;
            }
            else
            {
              return null;
            }

            WriteData(products);
            return product;
        }
 //Add a product
 public Product PostProduct(Product item)
 {
     return repository.Add(item);
 }
Пример #10
0
 public void PutProduct(Product product)
 {
     ProductsRepository.update(product);
 }
Пример #11
0
 public void PostProduct(Product product)
 {
     ProductsRepository.add(product);
 }
Пример #12
0
 public void Put(Product product)
 {
     products.Remove(products.First(c => c.Id == product.Id));
     products.Add(product);
 }
Пример #13
0
 public void Post(Product product)
 {
     Interlocked.Increment(ref counter);
     product.Id = counter.ToString("D3");
     products.Add(product);
 }
Пример #14
0
 public string Post(Product request)
 {
     _products.Add(request);
     return "ok";
 }
Пример #15
0
 public string Put(int id, Product request)
 {
     var model = _products.FirstOrDefault(x => x.Id.Equals(id));
     if (model == null) return "未找到该产品";
     model.Name = request.Name;
     model.Price = request.Price;
     model.Description = request.Description;
     return "ok";
 }