コード例 #1
0
 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);
        }
コード例 #3
0
        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;
 }
コード例 #5
0
 //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;
        }
コード例 #9
0
 //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";
 }