Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
 private decimal GetProductPriceByProductId(int id)
 {
     using (var ctx = new EmployeeDBEntities_Product())
     {
         return(ctx.Products.FirstOrDefault(p => p.ProductId == id).Price);
     }
 }
Exemplo n.º 3
0
 public Product Get(int id)
 {
     using (var ctx = new EmployeeDBEntities_Product())
     {
         return(ctx.Products.FirstOrDefault(p => p.ProductId == id));
     }
 }
Exemplo n.º 4
0
        public IHttpActionResult Put(Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("The data you entered is not valid"));
            }

            using (var ctx = new EmployeeDBEntities_Product())
            {
                var existingProduct = ctx.Products.FirstOrDefault(p => p.ProductId == product.ProductId);
                if (existingProduct != null)
                {
                    existingProduct.Name        = product.Name;
                    existingProduct.Description = product.Description;
                    existingProduct.Price       = product.Price;

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

            return(Ok());
        }
Exemplo n.º 5
0
        public ActionResult Edit(int id)
        {
            Sale sale = new Sale();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://localhost:44350/api/");

                var responseTask = client.GetAsync("sale/get/" + id);
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync <Sale>();
                    readTask.Wait();

                    sale = readTask.Result;
                }
            }

            using (var prodCtx = new EmployeeDBEntities_Product())
            {
                sale.ProductCollection = prodCtx.Products.ToList <Product>();
            }

            using (var empCtx = new EmployeeDBEntities())
            {
                sale.EmployeeCollection = empCtx.Employees.ToList <Employee>();
            }

            return(View(sale));
        }
Exemplo n.º 6
0
        public IHttpActionResult Get()
        {
            IList <Product> products = null;

            using (var ctx = new EmployeeDBEntities_Product())
            {
                products = ctx.Products.ToList();
            }

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

            return(Ok(products));
        }
Exemplo n.º 7
0
        public ActionResult Create()
        {
            Sale saleModel = new Sale();

            using (var prodCtx = new EmployeeDBEntities_Product())
            {
                saleModel.ProductCollection = prodCtx.Products.ToList <Product>();
            }

            using (var empCtx = new EmployeeDBEntities())
            {
                saleModel.EmployeeCollection = empCtx.Employees.ToList <Employee>();
            }

            return(View(saleModel));
        }
Exemplo n.º 8
0
        public IHttpActionResult Delete(int id)
        {
            if (id < 0)
            {
                return(BadRequest("Not a valid Product Id"));
            }

            using (var ctx = new EmployeeDBEntities_Product())
            {
                var product = ctx.Products.FirstOrDefault(p => p.ProductId == id);

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

            return(Ok());
        }
Exemplo n.º 9
0
        public IHttpActionResult PostNewProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new EmployeeDBEntities_Product())
            {
                ctx.Products.Add(new Product()
                {
                    Name        = product.Name,
                    Description = product.Description,
                    Price       = product.Price
                });

                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());
        }