Esempio n. 1
0
        public IHttpActionResult Put(Sale sale)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("The data you entered is not valid"));
            }

            using (var ctx = new EmployeeDBEntities_Sale())
            {
                var existingSale = ctx.Sales.FirstOrDefault(s => s.SaleId == sale.SaleId);
                if (existingSale != null)
                {
                    existingSale.ProductId         = sale.ProductId;
                    existingSale.EmployeeId        = sale.EmployeeId;
                    existingSale.CustomerFirstName = sale.CustomerFirstName;
                    existingSale.CustomerLastName  = sale.CustomerLastName;
                    existingSale.Comment           = sale.Comment;
                    existingSale.Quantity          = sale.Quantity;
                    existingSale.Amount            = GetProductPriceByProductId((int)sale.ProductId) * sale.Quantity;

                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
Esempio n. 2
0
        public IHttpActionResult Delete(int id)
        {
            if (id < 0)
            {
                return(BadRequest("Not a valid Product Id"));
            }

            using (var ctx = new EmployeeDBEntities_Sale())
            {
                var sale = ctx.Sales.FirstOrDefault(s => s.SaleId == id);

                ctx.Entry(sale).State = System.Data.Entity.EntityState.Deleted;
                ctx.SaveChanges();
            }

            return(Ok());
        }
Esempio n. 3
0
        public IHttpActionResult PostNewProduct(Sale sale)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new EmployeeDBEntities_Sale())
            {
                ctx.Sales.Add(new Sale()
                {
                    ProductId         = sale.ProductId,
                    EmployeeId        = sale.EmployeeId,
                    CustomerFirstName = sale.CustomerFirstName,
                    CustomerLastName  = sale.CustomerLastName,
                    Comment           = sale.Comment,
                    Quantity          = sale.Quantity,
                    Amount            = GetProductPriceByProductId((int)sale.ProductId) * sale.Quantity
                });

                try
                {
                    ctx.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                          eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Console.WriteLine("- Property: \"{0}\", Value: \"{1}\", Error: \"{2}\"",
                                              ve.PropertyName,
                                              eve.Entry.CurrentValues.GetValue <object>(ve.PropertyName),
                                              ve.ErrorMessage);
                        }
                    }
                    throw;
                }
            }

            return(Ok());
        }