public IHttpActionResult PostProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Products.Add(product);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (ProductExists(product.Id))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, product);
        }
Пример #2
0
        public HttpResponseMessage PostProduct(Product product)
        {
           
            Repository.SaveProduct(product);

            var response = Request.CreateResponse<Product>(System.Net.HttpStatusCode.Created, product);
            return response;
        }
Пример #3
0
 public IEnumerable<Product> GetProductsByCategory(string category)
 {
     Product[] products = new Product[]
     {
         new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
         new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
         new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
     };
     return products;
 }
Пример #4
0
        public ActionResult Create(Product collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
Пример #5
0
        public ProductsRepository()
        {
            var ctx = HttpContext.Current;

            if (ctx != null)
            {
                if (ctx.Cache[CacheKey] == null)
                {
                    var products = new Product[]
                    {
                        
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
                new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
                new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
                    };

                    ctx.Cache[CacheKey] = products;
                }
            }
        }
Пример #6
0
        public bool SaveProduct(Product product)
        {
            var ctx = HttpContext.Current;

            if (ctx != null)
            {
                try
                {
                    var currentData = ((Product[])ctx.Cache[CacheKey]).ToList();
                    currentData.Add(product);
                    ctx.Cache[CacheKey] = currentData.ToArray();

                    return true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    return false;
                }

            }

            return false;
        }
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

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

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #8
0
 public string PutProduct(int id, Product product)
 {
     return "put " + product.Name;
 }
Пример #9
0
 //        public HttpResponseMessage PostProduct(Product item)
 public string PostProduct(Product product)
 {
     return "post " + product.Name;
 }
Пример #10
0
        public async Task<IHttpActionResult> UpdateProduct(Product product)
        {
            if (product == null)
                return BadRequest();

            foreach(var p in products)
            {
                if(p.Id == product.Id)
                {
                    p.Category = product.Category;
                    p.Name = product.Name;
                    p.Price = product.Price;

                    return Ok(p);
                }
            }

            return NotFound();
        }
Пример #11
0
 public async Task<IHttpActionResult> AddProduct(Product product)
 {
     products.Add(product);
     return Ok();
 }