public bool Update(Product item) { int index = products.FindIndex(p => p.Id == item.Id); products.RemoveAt(index); products.Insert(index, item); return true; }
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; }
/// <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; } }
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); }
/// <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; }
/// <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); }
public void PutProduct(Product product) { ProductsRepository.update(product); }
public void PostProduct(Product product) { ProductsRepository.add(product); }
public void Put(Product product) { products.Remove(products.First(c => c.Id == product.Id)); products.Add(product); }
public void Post(Product product) { Interlocked.Increment(ref counter); product.Id = counter.ToString("D3"); products.Add(product); }
public string Post(Product request) { _products.Add(request); return "ok"; }
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"; }