Esempio n. 1
0
        public IHttpActionResult Put(int id, ProductVM product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            try{
                prodRepo.Update(product);
                //task.Wait();
            }
            catch (DbUpdateConcurrencyException) {
                if (prodRepo.GetProduct(id) == null){
                    return NotFound();
                }
                else{
                    throw;
                }
            }
            return StatusCode(HttpStatusCode.NoContent);
        }
Esempio n. 2
0
        public IHttpActionResult PostProduct(ProductVM product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            prodRepo.Add(product);
             //   task.Wait();

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, product);
        }
Esempio n. 3
0
 public void Update(ProductVM _product)
 {
     var model = ParseModel(_product);
     dbContext.Entry(model).State = System.Data.Entity.EntityState.Modified;
     dbContext.SaveChanges();
 }
Esempio n. 4
0
        private product ParseModel(ProductVM vm)
        {
            product model;
            if (vm.Id == null || vm.Id == 0)
            {
                model = new product();
            }
            else {
                model = dbContext.products.SingleOrDefault(p=> p.id == vm.Id);
            }

            model.name = vm.Name;
            model.description = vm.Description;
            model.img_url = vm.ImgUrl;
            model.is_active = vm.IsActive;
            model.location_id = vm.LocationId;
            model.category_id = vm.CategoryId;
            model.created_date = vm.CreatedDate;
            model.currency = vm.Currency;
            model.price = vm.Price;
            model.quantity = vm.Quantity;
            model.unit_measure = vm.UnitMeasure;

            return model;
        }
Esempio n. 5
0
 public void Add(ProductVM _product)
 {
     var model = ParseModel(_product);
     dbContext.products.Add(model);
     dbContext.SaveChanges();
 }