Exemplo n.º 1
0
        public async Task<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
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Exemplo n.º 2
0
        public async Task<IHttpActionResult> PostProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Product.Add(product);
            await db.SaveChangesAsync();

            //load all the details of this product
            db.Entry(product).Reference(x => x.Category).Load();
            db.Entry(product).Reference(x => x.Section).Load();

            var dto = new ProductDTO()
            {
                Id = product.Id,
                CategoryId = product.CategoryId,
                CategoryName = product.Category.Name,
                Name = product.Name,
                Description = product.Description,
                ImageName = product.ImageName,
                Price = product.Price,
                SectionId = product.SectionId,
                SectionName = product.Section.Name,
                Stock = product.Stock
            };

            return CreatedAtRoute("DefaultApi", new { id = product.Id }, product);
        }