Example #1
0
        /* Pasamos una vista para que sea más fácil de editar el JSON y luego
         * lo transformamos en un Products para se pueda actualizar */
        public IHttpActionResult PutProducts(/*int id, */ ProductsView productsView)
        {
            Products product = new Products
            {
                ProductID       = productsView.Id,
                ProductName     = productsView.Name,
                QuantityPerUnit = productsView.QuantityPerUnit,
                UnitPrice       = productsView.Price,
            };

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //if (id != product.ProductID) return BadRequest();

            try
            {
                productsLogic.Update(product);
                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #2
0
        public IHttpActionResult GetProducts(int id)
        {
            Products products = productsLogic.GetData(id);

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

            ProductsView productsView = new ProductsView
            {
                Id              = products.ProductID,
                Name            = products.ProductName,
                QuantityPerUnit = products.QuantityPerUnit,
                Price           = (decimal)products.UnitPrice
            };

            return(Ok(productsView));
        }
Example #3
0
        public IHttpActionResult PostProducts(ProductsView productsView)
        {
            Products product = new Products
            {
                ProductName     = productsView.Name,
                QuantityPerUnit = productsView.QuantityPerUnit,
                UnitPrice       = productsView.Price,
            };

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            productsLogic.Add(product);

            //Para retornar en la vista el ID correcto y no 0
            productsView.Id = product.ProductID;

            //return CreatedAtRoute("DefaultApi", new { id = product.ProductID }, productsView);
            return(Ok());
        }