Ejemplo n.º 1
0
        public async Task <ActionResult <bool> > Post([FromBody] ProductAddOrUpdateModel model)
        {
            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            var newProduct = new Product
            {
                ProductID = Guid.NewGuid(),
                Name      = model.Name,
                Price     = model.Price
            };

            DB.Products.Add(newProduct);
            await DB.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <bool> > Put(Guid id, [FromBody] ProductAddOrUpdateModel model)
        {
            var product = await DB.Products
                          .Where(p => p.ProductID == id)
                          .FirstOrDefaultAsync();

            if (product == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid == false)
            {
                return(BadRequest(ModelState));
            }

            product.Name  = model.Name;
            product.Price = model.Price;

            await DB.SaveChangesAsync();

            return(true);
        }