Esempio n. 1
0
 public void PutProduct(int id, Product contact)
 {
     contact.Id = id;
     if (!repository.Update(contact))
     {
         throw new HttpResponseException(new HttpResponseMessage(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;
 }
 //the product will be deserialized from the request body
 //PUT
 //browsers dont send put requests but you can send the programatically thru .NET or AJAX
 //alternatively you can tunnel the request thru a post
 public void PutProduct(int id, Product product)
 {
     product.Id = id;
     if (!repository.Update(product))
     {
         throw new HttpResponseException(HttpStatusCode.NotFound);
     }
 }
 // POST
 public HttpResponseMessage PostProduct(Product item)
 {
     item = repository.Add(item);
     var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item); //http 1.1 states that when a resource is created you should return with 201
     //CreateResponse automatically serializes the Product object into the body of an http header
     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");
     }
     mySQLDBDriver.addProduct(item);
     return item;
 }
 public Product Add(Product item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     item.Id = _nextId++;
     products.Add(item);
     return item;
 }
 /// <summary>
 /// Adds the product.
 /// </summary>
 /// <param name="product">The product.</param>
 /// <returns>
 /// Returns the integer count value of affected rows by the non-query operation.
 /// </returns>
 public int addProduct(Product product)
 {
     string sqlStatement = "INSERT INTO products (id, name, category, price, manufacturer, date_received) VALUES (NULL, @name, @category, @price, @manufacturer, @dateReceived);";
     MySqlParameter[] parameters = new MySqlParameter[5];
     parameters[0] = new MySqlParameter("name", product.Name);
     parameters[1] = new MySqlParameter("category", product.Category);
     parameters[2] = new MySqlParameter("price", product.Price);
     parameters[3] = new MySqlParameter("manufacturer", product.Manufacturer);
     parameters[4] = new MySqlParameter("dateReceived", product.DateReceived);
     return MySqlHelper.ExecuteNonQuery(connectionString, sqlStatement, parameters);
 }
Esempio n. 8
0
 public bool Update(Product item)
 {
     int index = products.FindIndex(p => p.Id == item.Id);
     if (index == -1)
     {
         return false;
     }
     products.RemoveAt(index);
     products.Add(item);
     return true;
 }
        //public HttpResponseMessage DeleteProduct(int id)
        //if you have the above signature, you need to specify the id in the query string, model binding does not match the action method
        public HttpResponseMessage DeleteProduct(Product product)
        {
            Product item = repository.GetAll().FirstOrDefault(p => p.Id == product.Id);
            if (item == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            repository.Remove(item.Id);
            return new HttpResponseMessage(HttpStatusCode.NoContent);
        }
        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;
        }
 public bool Update(Product item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("item");
     }
     if (mySQLDBDriver.getProduct(item.Id) == null)
     {
         return false;
     }
     mySQLDBDriver.updateProduct(item);
     return true;
 }
        // POST api/Admin
        public HttpResponseMessage PostProduct(Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, product);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = product.Id }));
                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
        // PUT api/Admin/5
        public HttpResponseMessage PutProduct(int id, Product product)
        {
            if (ModelState.IsValid && id == product.Id)
            {
                db.Entry(product).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Esempio n. 14
0
 public Product Add(Product item)
 {
     item.Id = _nextId++;
     products.Add(item);
     return item;
 }
 public Product Add(Product item)
 {
     db.Products.Add(item);
     db.SaveChanges();
     return item;
 }
 public void RemoveProduct(Product product)
 {
     db.Products.Remove(product);
 }
 public void PutProduct(int id, Product product)
 {
     db.Entry(product).State = EntityState.Modified;
     Save();
 }
 public void PostProduct(Product product)
 {
     db.Products.Add(product);
     Save();
 }
 public bool Update(Product item)
 {
     db.Entry(item).State = EntityState.Modified;
     db.SaveChanges();
     return true;
 }
        // PUT api/Admin/5
        public HttpResponseMessage PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != product.Id)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
        public HttpResponseMessage PostProduct(Product item)
        {
            if (ModelState.IsValid)
            {
                // Do something with the product.
                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;

                //return new HttpResponseMessage(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
        /// <summary>
        // PUT api/Admin/5
        //Updates a product.
        //UrI:api/products/id
        //HTTP Method:PUT
        /// </summary>
        /// <param name="id"></param>
        /// <param name="product"></param>
        /// <returns></returns>
        public HttpResponseMessage PutProduct(int id, Product product)
        {
            if (ModelState.IsValid && id == product.Id)
            {
                try
                {
                    repository.PutProduct(id, product);
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
 /// <summary>
 /// Updates the product.
 /// </summary>
 /// <param name="id">The unique identifier.</param>
 /// <param name="product">The product.</param>
 /// <returns>
 /// Returns the integer count value of affected rows by the non-query operation.
 /// </returns>
 public int updateProduct(Product product)
 {
     string sqlStatement = "UPDATE products SET name = @name, category = @category, price = @price, manufacturer = @manufacturer, date_received = @dateReceived WHERE id =  @id;";
     MySqlParameter[] parameters = new MySqlParameter[6];
     parameters[0] = new MySqlParameter("id", product.Id);
     parameters[1] = new MySqlParameter("name", product.Name);
     parameters[2] = new MySqlParameter("category", product.Category);
     parameters[3] = new MySqlParameter("price", product.Price);
     parameters[4] = new MySqlParameter("manufacturer", product.Manufacturer);
     parameters[5] = new MySqlParameter("dateReceived", product.DateReceived);
     return MySqlHelper.ExecuteNonQuery(connectionString, sqlStatement, parameters);
 }