public void PutProduct(Product product)
 {
     if (!repository.Update(product))
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
 }
        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;
        }
        public Product Add(Product item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            context.Products.Add(item);
            context.SaveChanges();
            return item;
        }
        public bool Update(Product item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            var product = context.Products.FirstOrDefault(p => p.Id == item.Id);
            if (product == null)
            {
                return false;
            }
            context.SetValues(product, item);
            context.SaveChanges();
            return true;
        }
Exemple #5
0
        public void Can_Add_Product()
        {
            var mock = CreateMockObject();
            var product = new Product{ Id = 1, Name = "Prod1", Category = "Gaming", Price = 255};
            var config = new HttpConfiguration();
            var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products/1");
            var route = config.Routes.MapHttpRoute("DefaultApi", "api/products/{id}");
            mock.Setup(m => m.Add(It.IsAny<Product>())).Returns<Product>(m => new Product());

            ProductsController controller = new ProductsController(mock.Object);
            controller.Request = request;
            controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
            controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = route;
            var result = controller.PostProduct(product);

            mock.Verify(m => m.Add(It.IsAny<Product>()), Times.Once());
            Assert.AreEqual(System.Net.HttpStatusCode.Created, result.StatusCode);
        }
 public void SetValues(Product current, Product newVal)
 {
     Entry(current).CurrentValues.SetValues(newVal);
 }