Esempio n. 1
0
 public Sale Get(int id)
 {
     using (var ctx = new EmployeeDBEntities_Sale())
     {
         return(ctx.Sales.FirstOrDefault(p => p.SaleId == id));
     }
 }
Esempio n. 2
0
        public IHttpActionResult Get()
        {
            IList <Sale> sales = null;

            using (var ctx = new EmployeeDBEntities_Sale())
            {
                sales = ctx.Sales.ToList();
            }

            using (var ctxEmp = new EmployeeDBEntities())
            {
                foreach (var sale in sales)
                {
                    sale.EmployeeName = ctxEmp.Employees.FirstOrDefault(e => e.ID == sale.EmployeeId)?.FirstName + " " +
                                        ctxEmp.Employees.FirstOrDefault(e => e.ID == sale.EmployeeId)?.LastName;
                }
            }

            using (var ctxProd = new EmployeeDBEntities_Product())
            {
                foreach (var sale in sales)
                {
                    sale.ProductName = ctxProd.Products.FirstOrDefault(p => p.ProductId == sale.ProductId)?.Name;
                }
            }

            if (sales.Count == 0)
            {
                return(NotFound());
            }

            return(Ok(sales));
        }
Esempio n. 3
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. 4
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. 5
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());
        }
Esempio n. 6
0
        public IHttpActionResult GetEmployeeSalesTotals()
        {
            //List<object> employeeSales = new List<object>();
            using (var ctx = new EmployeeDBEntities_Sale())
            {
                var employeeSales = ctx.Sales
                                    .GroupBy(s => s.EmployeeId)
                                    .Select(sl => new
                {
                    Employee   = sl.FirstOrDefault().EmployeeId,
                    saleAmount = sl.Sum(s => s.Amount)
                }).ToList();

                if (employeeSales.Count == 0)
                {
                    return(NotFound());
                }

                return(Ok(employeeSales));
            }
        }