Пример #1
0
 public void PutProduct(int id, Product product)
 {
     product.Id = id;
     if (!repository.Update(product))
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
 }
Пример #2
0
 //TODO:xieran20121221 需要写客户端代码验证此方法
 public HttpResponseMessage PostProduct(Product item)
 {
     item = repository.Add(item);
     var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item);
     string uri = Url.Link("DefaultApi", new { id = item.Id });
     response.Headers.Location = new Uri(uri);
     return response;
 }
Пример #3
0
        public Product Add(Product item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            item.Id = _nextId++;
            products.Add(item);

            return item;
        }
Пример #4
0
        public bool Update(Product item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            int index = products.FindIndex(p => p.Id == item.Id);

            if (index == -1)
            {
                return false;
            }

            products.RemoveAt(index);
            products.Add(item);
            return true;
        }