Exemplo n.º 1
0
        public ActionResult UpdateOrCreateProduct(MyProduk model)
        {
            try
            {
                Onboard2DbContext db = new Onboard2DbContext();
                if (model.Id > 0)
                {
                    //update
                    Product prod = db.Products.SingleOrDefault(x => x.Id == model.Id);
                    prod.Id    = model.Id;
                    prod.Name  = model.Name;
                    prod.Price = model.Price;
                    db.SaveChanges();
                }
                else
                {
                    //Insert
                    Product prod = new Product();
                    prod.Name  = model.Name;
                    prod.Price = model.Price;
                    db.Products.Add(prod);
                    db.SaveChanges();
                }

                return(RedirectToAction("GetProductList"));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 2
0
 public ActionResult UpdateOrCreateCustomer(MyCustomer model)
 {
     try
     {
         Onboard2DbContext db = new Onboard2DbContext();
         if (model.Id > 0)
         {
             //update
             Customer cust = db.Customers.FirstOrDefault(x => x.Id == model.Id);
             if (cust != null)
             {
                 cust.Id      = model.Id;
                 cust.Name    = model.Name;
                 cust.Address = model.Address;
             }
             db.SaveChanges();
         }
         else
         {
             //Insert
             Customer cust = new Customer();
             cust.Name    = model.Name;
             cust.Address = model.Address;
             db.Customers.Add(cust);
             db.SaveChanges();
         }
         return(RedirectToAction("GetCustomerList"));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
 public ActionResult UpdateOrCreateStore(MyStore model)
 {
     try
     {
         Onboard2DbContext db = new Onboard2DbContext();
         if (model.Id > 0)
         {
             //update
             Store stor = db.Stores.FirstOrDefault(x => x.Id == model.Id);
             if (stor != null)
             {
                 stor.Id      = model.Id;
                 stor.Name    = model.Name;
                 stor.Address = model.Address;
             }
             db.SaveChanges();
         }
         else
         {
             //Insert
             Store stor = new Store();
             stor.Name    = model.Name;
             stor.Address = model.Address;
             db.Stores.Add(stor);
             db.SaveChanges();
         }
         return(RedirectToAction("GetStoreList"));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 4
0
        public ActionResult UpdateOrCreateProductSold(ProductSold model)
        {
            var db = new Onboard2DbContext();

            if (model.Id > 0)
            {
                // UPDATE
                var prodsold = db.ProductSolds.SingleOrDefault(x => x.Id == model.Id);

                prodsold.ProductId  = model.ProductId;
                prodsold.CustomerId = model.CustomerId;
                prodsold.StoreId    = model.StoreId;
                prodsold.DateSold   = model.DateSold;
                db.SaveChanges();
            }

            else
            {
                // INSERT
                ProductSold prodsold = new ProductSold();
                prodsold.ProductId  = model.ProductId;
                prodsold.CustomerId = model.CustomerId;
                prodsold.StoreId    = model.StoreId;
                prodsold.DateSold   = model.DateSold;

                db.ProductSolds.Add(prodsold);
                db.SaveChanges();
            }
            return(Json(model));
        }
Exemplo n.º 5
0
        // DELETE
        public JsonResult DeleteProduct(int Id)
        {
            using (var db = new Onboard2DbContext())
            {
                var prod     = db.Products.FirstOrDefault(x => x.Id == Id);
                var prodsold = db.ProductSolds.FirstOrDefault(x => x.Id == Id);
                var prodid   = db.ProductSolds.FirstOrDefault(x => x.ProductId == Id);

                if (prod != null)
                {
                    db.Products.Remove(prod);
                }
                if (prodsold != null)
                {
                    db.ProductSolds.Remove(prodsold);
                }
                if (prodid != null)
                {
                    db.ProductSolds.Remove(prodid);
                }
                db.SaveChanges();

                return(Json(JsonRequestBehavior.AllowGet));
            }
        }