Exemplo n.º 1
0
        public IHttpActionResult PostSale(Sale sale)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            ObjectParameter saleId = new ObjectParameter("SaleId", typeof(int));
            db.spInsertSale(sale.ClientId, sale.DateSale, saleId);

            foreach (SaleProduct saleProduct in sale.SaleProducts)
            {
                db.spInsertSaleProduct((int)saleId.Value, saleProduct.ProductId, saleProduct.Quantity);
            }

            db.SaveChanges();

            sale = db.Sales.Find((int)saleId.Value);
            return CreatedAtRoute("DefaultApi", new { id = sale.Id }, sale);
        }
Exemplo n.º 2
0
        public IHttpActionResult PutSale(int id, Sale sale)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

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

            Sale s = db.Sales.Find(id);

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

            s.SaleProducts.Clear();

            db.SaveChanges();

            foreach (SaleProduct saleProduct in sale.SaleProducts)
            {
                db.spInsertSaleProduct(s.Id, saleProduct.ProductId, saleProduct.Quantity);
            }

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }

            return StatusCode(HttpStatusCode.NoContent);
        }