public static void RemoveProducts(this NorthwindEntities db, ProductsRequest request)
 {
     foreach (var product in request.Models)
     {
         db.RemoveProduct(product.ProductID);
     }
 }
 public static void UpdateProducts(this NorthwindEntities db, ProductsRequest request)
 {
     foreach (var product in request.Models)
     {
         db.UpdateProduct(product);                
     }
 }
Esempio n. 3
0
 public static void RemoveProducts(this NorthwindEntities db, ProductsRequest request)
 {
     foreach (var product in request.Models)
     {
         db.RemoveProduct(product.ProductID);
     }
 }
Esempio n. 4
0
 public static void UpdateProducts(this NorthwindEntities db, ProductsRequest request)
 {
     foreach (var product in request.Models)
     {
         db.UpdateProduct(product);
     }
 }
        public static IEnumerable<ProductViewModel> AddProducts(this NorthwindEntities db, ProductsRequest request)
        {
            foreach (var product in request.Models)
            {
                db.AddProduct(product);                
            }

            return request.Models;
        }
        // DELETE api/product/5
        public HttpResponseMessage Delete(ProductsRequest request)
        {
            try
            {
                db.RemoveProducts(request);
            }
            catch (DbUpdateConcurrencyException)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            return Request.CreateResponse(HttpStatusCode.OK, new DataSourceResult { Data = request.Models });
        }
        // POST api/product
        public HttpResponseMessage Post(ProductsRequest request)
        {
            if (ModelState.IsValid)
            {
                var products = db.AddProducts(request);
                
                var response = Request.CreateResponse(HttpStatusCode.Created, new DataSourceResult { Data =  products });
                response.Headers.Location = new Uri(Url.Link("DefaultApi", null));
                return response;
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage);

                return Request.CreateResponse(HttpStatusCode.BadRequest, errors);
            }
        }
        // PUT api/product/5
        public HttpResponseMessage Put(ProductsRequest request) // the ProductsRequest is required in order the list of Product to be correctly bind from the request
        {
            if (ModelState.IsValid)
            {
                try
                {
                    db.UpdateProducts(request); 
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors).Select(error => error.ErrorMessage);
                return Request.CreateResponse(HttpStatusCode.BadRequest, errors);
            }
        }
Esempio n. 9
0
        public static IEnumerable <ProductViewModel> AddProducts(this NorthwindEntities db, ProductsRequest request)
        {
            foreach (var product in request.Models)
            {
                db.AddProduct(product);
            }

            return(request.Models);
        }