Example #1
0
        public void UpdatePut(Guid id, PutProduct updateProduct)
        {
            var findProduct = _context.Products.Find(id);

            _mapper.Map(updateProduct, findProduct);
            _context.SaveChanges();
        }
        public async Task <ActionResult <PutProductDTO> > PutProduct(string id, PutProduct putProduct)
        {
            var productToPut = await _context.Products.FindAsync(id);

            PutProduct putProductModel = putProduct;

            if (productToPut == null)
            {
                return(BadRequest(this.GetError(Error.PRODUCT_NOT_FOUND)));
            }
            if (!this.ValidTokenAdmin(putProductModel.AccessToken))
            {
                return(BadRequest(this.GetError(Error.INVALID_TOKEN)));
            }

            if (putProductModel.Name != null && putProductModel.Name.Length > 0)
            {
                productToPut.Name = putProduct.Name;
            }
            if (putProductModel.Type != null && putProductModel.Type.Length > 0)
            {
                productToPut.Type = putProduct.Type;
            }
            if (putProductModel.Description != null && putProductModel.Description.Length > 0)
            {
                productToPut.Description = putProduct.Description;
            }
            if (putProductModel.ImagePath != null && putProductModel.ImagePath.Length > 0)
            {
                productToPut.ImagePath = putProduct.ImagePath;
            }
            if (putProductModel.Price != null && putProductModel.Price.Length > 0)
            {
                productToPut.Price = Convert.ToDecimal(putProductModel.Price);
            }
            if (putProductModel.Status != null && putProductModel.Status.Length > 0)
            {
                productToPut.Status = putProduct.Status;
            }

            _context.Entry(productToPut).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            PutProductDTO putProductDTO = new PutProductDTO(productToPut);

            return(putProductDTO);
        }
Example #3
0
 public IActionResult Put(Guid id, [FromForm] PutProduct updateProduct)
 {
     try
     {
         _services.UpdatePut(id, updateProduct);
         return(Ok());
     }
     catch
     {
         throw;
     }
 }
Example #4
0
 public string UpdateProduct(int productId, string name, decimal price)
 {
     using (var client = new WebClient())
     {
         var product = new PutProduct();
         product.Name  = name;
         product.Price = price;
         client.Headers.Add("Content-Type:application/json");
         client.Headers.Add("Accept:application/json");
         var result = client.UploadString(ProductAPIString + "/" + productId, "PUT", JsonConvert.SerializeObject(product));
         return(result);
     }
 }
Example #5
0
        public async Task <IHttpActionResult> Put(PutProduct model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var product = new Product
            {
                Id   = model.Id,
                Name = model.Name
            };

            await service.Update(product);

            return(Ok());
        }