Example #1
0
 public Product Save(Product model)
 {
     using (var db = new ProductDBEntities())
     {
         if (model.id != 0)
         {
             var obj1 = db.Products.SingleOrDefault(o => o.id == model.id);
             obj1.name     = model.name;
             obj1.category = model.category;
             obj1.price    = model.price;
             db.SaveChanges();
         }
         else
         {
             var obj2 = new Products();
             obj2.name     = model.name;
             obj2.category = model.category;
             obj2.price    = model.price;
             db.Products.Add(obj2);
             model.id = obj2.id;
             db.SaveChanges();
         }
         return(model);
     }
 }
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.productId)
            {
                return(BadRequest());
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #3
0
        public ActionResult AddOrder(int id)
        {
            try
            {
                Orders order = new Orders();
                order.ProductID = id;
                order.OrderTime = DateTime.Now;
                order.IsStored  = false;

                _db.Orders.Add(order);
                _db.SaveChanges();

                OrderStatus os = new OrderStatus();
                os.OrderID = order.OrderID;
                os.Status  = true;

                _db.OrderStatus.Add(os);
                _db.SaveChanges();

                ViewBag.Message = "<div class='alert alert-success'><button type='button'class='close'data-dismiss='alert'>×</button><strong>Success!</strong>Ordered</div>";
            }
            catch
            {
                ViewBag.Message = "<div class='alert alert-alert'><button type='button'class='close'data-dismiss='alert'>×</button><strong>Error!</strong> Error.</div><br>";
            }
            return(RedirectToAction("OrderProduct", "Order"));
        }
Example #4
0
        public ActionResult Add(Products product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (product != null)
                    {
                        var checkProduct = _db.Products.Where(p => p.IsActive && p.IsDeleted == false).FirstOrDefault(p => p.ProductName == product.ProductName);
                        if (checkProduct != null)
                        {
                            ViewBag.Message =
                                "<div class='alert alert-danger'><button type='button'class='close'data-dismiss='alert'>×</button><strong>ERROR!</strong> Product has already exist!</div>";
                            return(View(product));
                        }

                        product.IsActive  = true;
                        product.IsDeleted = false;

                        _db.Products.Add(product);
                        _db.SaveChanges();
                        ViewBag.Message =
                            "<div class='alert alert-success'><button type='button'class='close'data-dismiss='alert'>×</button><strong>SUCCESS!</strong> Product has succesfully added</div>";
                    }
                }
                return(View(product));
            }
            catch
            {
                ViewBag.Message = "<div class='alert alert-alert'><button type='button'class='close'data-dismiss='alert'>×</button><strong>ERROR!</strong> Error.</div><br>";
            }
            return(View());
        }
Example #5
0
 public ActionResult Create(product rec)
 {
     try
     {
         if (ModelState.IsValid)
         {
             ProductTbl ptbl = new ProductTbl();
             colorDataDropdown();
             var CheckName = entity.ProductTbls.Any(p => p.ProductName == rec.productName);
             if (CheckName == true)
             {
                 TempData["already"] = "Product Name is Already Exist in Database..";
                 return(View());
             }
             ptbl.ProductName     = rec.productName;
             ptbl.Price           = rec.Price;
             ptbl.Quantity        = rec.Quantity;
             ptbl.IsGstApplicable = rec.IsGSTApplicable;
             ptbl.PurchaseDate    = Convert.ToDateTime(rec.Purchase_Date);
             ptbl.ExpiryDate      = Convert.ToDateTime(rec.Expiry_Date);
             ptbl.Color           = rec.ColorID;
             entity.ProductTbls.Add(ptbl);
             entity.SaveChanges();
             TempData["save"] = "Record Inserted Successfully..";
             return(RedirectToAction("Index"));
         }
         return(View());
     }
     catch (Exception e)
     {
         ModelState.AddModelError("", e.Message);
         return(View());
     }
 }
        public IHttpActionResult PutCategory(int id, Category category)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != category.categoryId)
            {
                return(BadRequest());
            }

            db.Entry(category).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #7
0
        public TblProductList Add(TblProductList item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            // TO DO : Code to save record into database
            ProductDB.TblProductLists.Add(item);
            ProductDB.SaveChanges();
            return(item);
        }
Example #8
0
 public HttpResponseMessage Post([FromBody] Product product)
 {
     try
     {
         _context.Products.Add(product);
         _context.SaveChanges();
         var message = Request.CreateResponse(HttpStatusCode.Created, product);
         message.Headers.Location = new Uri(Request.RequestUri + product.ID.ToString());
         return(message);
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
        /// <summary>
        /// Stores a product
        /// </summary>
        /// <param name="product">the product that is stored</param>
        /// <returns>stored product</returns>
        public tblProduct StoreProduct(tblProduct product)
        {
            if (TotalQuantity() + product.Quantity <= 100)
            {
                try
                {
                    using (ProductDBEntities context = new ProductDBEntities())
                    {
                        tblProduct productToStore = (from ss in context.tblProducts where ss.ProductID == product.ProductID select ss).First();

                        productToStore.Stored = true;

                        context.SaveChanges();
                        return(product);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception" + ex.Message.ToString());
                    return(null);
                }
            }
            else
            {
                return(product);
            }
        }
        /// <summary>
        /// Deletes the product depending if the productID already exists
        /// </summary>
        /// <param name="productID">the product that is being deleted</param>
        /// <returns>list of products</returns>
        public void DeleteProduct(int productID)
        {
            try
            {
                using (ProductDBEntities context = new ProductDBEntities())
                {
                    bool isProduct = IsProductID(productID);

                    if (isProduct == true)
                    {
                        // find the product removing them
                        tblProduct productToDelete = (from r in context.tblProducts where r.ProductID == productID select r).First();
                        context.tblProducts.Remove(productToDelete);

                        context.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("Cannot delete the product");
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
            }
        }
        public HttpResponseMessage SaveProduct(tblProduct asproduct)
        {
            int result = 0;

            try
            {
                dbContext.tblProducts.Add(asproduct);
                dbContext.SaveChanges();
                result = 1;
            }
            catch (Exception e)
            {
                result = 0;
            }

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
Example #12
0
 public void Delete(int id)
 {
     using (var db = new ProductDBEntities())
     {
         var obj = db.Products.SingleOrDefault(o => o.id == id);
         db.Products.Remove(obj);
         db.SaveChanges();
     }
 }
        /// <summary>
        /// Creates or edits a product
        /// </summary>
        /// <param name="product">the product that is added</param>
        /// <returns>a new or edited product</returns>
        public tblProduct AddProduct(tblProduct product)
        {
            try
            {
                using (ProductDBEntities context = new ProductDBEntities())
                {
                    if (product.ProductID == 0)
                    {
                        tblProduct newProduct = new tblProduct
                        {
                            ProductName = product.ProductName,
                            ProductCode = product.ProductCode,
                            Quantity    = product.Quantity,
                            Price       = product.Price,
                            Stored      = false
                        };

                        context.tblProducts.Add(newProduct);
                        context.SaveChanges();
                        product.ProductID = newProduct.ProductID;
                        return(product);
                    }
                    else
                    {
                        tblProduct productToEdit = (from ss in context.tblProducts where ss.ProductID == product.ProductID select ss).First();

                        productToEdit.ProductName = product.ProductName;
                        productToEdit.ProductCode = product.ProductCode;
                        productToEdit.Quantity    = product.Quantity;
                        productToEdit.Price       = product.Price;
                        productToEdit.Stored      = product.Stored;
                        productToEdit.ProductID   = product.ProductID;

                        context.SaveChanges();
                        return(product);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception" + ex.Message.ToString());
                return(null);
            }
        }
Example #14
0
        public ActionResult AddProduct(HttpPostedFileBase file, HttpPostedFileBase file1, tblProduct insertprd)
        {
            string filename  = Path.GetFileName(file.FileName);
            string _filename = DateTime.Now.ToString("yymmssfff") + filename;
            string extension = Path.GetExtension(file.FileName);
            string path      = Path.Combine(Server.MapPath("~/Image/"), _filename);

            insertprd.SmallImage = "~/Image/" + _filename;

            string filename1  = Path.GetFileName(file1.FileName);
            string _filename1 = DateTime.Now.ToString("yymmssfff") + filename1;
            string extension1 = Path.GetExtension(file1.FileName);
            string path1      = Path.Combine(Server.MapPath("~/Image/"), _filename1);

            insertprd.LargeImage = "~/Image/" + _filename1;

            if ((extension.ToLower() == ".jpg" || extension.ToLower() == ".jpeg" || extension.ToLower() == ".png") && (extension1.ToLower() == ".jpg" || extension1.ToLower() == ".jpeg" || extension1.ToLower() == ".png"))
            {
                if (file.ContentLength <= 1000000 && file1.ContentLength <= 1000000)
                {
                    pd.tblProducts.Add(insertprd);
                    if (pd.SaveChanges() > 0)
                    {
                        file.SaveAs(path);
                        file1.SaveAs(path1);
                        ViewBag.msg = "Record Added";
                        ModelState.Clear();
                    }
                }
                else
                {
                    ViewBag.msg = "Size is not valid";
                }
            }
            HttpClient hc = new HttpClient();

            hc.BaseAddress = new Uri("https://localhost:44324/api/ProductCrud");

            var insertrecord = hc.PostAsJsonAsync <tblProduct>("ProductCrud", insertprd);

            insertrecord.Wait();

            var savedata = insertrecord.Result;

            ViewBag.tblProducts = new SelectList(pd.tblProducts, "Id", "Name");


            if (savedata.IsSuccessStatusCode)
            {
                TempData["SuccessMessage"] = "Saved SucessFully";
                return(RedirectToAction("ProductList"));
            }
            return(View("ProductList"));
        }
 public ActionResult Create(product p)
 {
     try
     {
         string fileName  = Path.GetFileNameWithoutExtension(p.ImageFile.FileName);
         string extension = Path.GetExtension(p.ImageFile.FileName);
         fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
         p.Image  = "~/Image/" + fileName;
         fileName = Path.Combine(Server.MapPath("~/Image/"), fileName);
         p.ImageFile.SaveAs(fileName);
         db.products.Add(p);
         db.SaveChanges();
         ModelState.Clear();
         return(RedirectToAction("Index"));
     }
     catch
     {
         return(View());
     }
 }
Example #16
0
 public bool CreateProduct(Product productToCreate)
 {
     try
     {
         _entities.AddToProductSet(productToCreate);
         _entities.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Example #17
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            product.CreateDate      = DateTime.Now;
            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult AddProduct(ProductModel objProductModel)
        {
            objProductDBEntities = new ProductDBEntities();
            Product objProduct = new Product();

            objProduct.Category  = objProductModel.Category;
            objProduct.Code      = objProductModel.Code;
            objProduct.Name      = objProductModel.Name;
            objProduct.StockQty  = objProductModel.StockQty;
            objProduct.UnitPrice = objProductModel.UnitPrice;
            objProductDBEntities.Products.Add(objProduct);
            objProductDBEntities.SaveChanges();
            return(Ok("Product is Added.."));
        }
Example #19
0
        public IHttpActionResult Put(ProductClass pc)
        {
            var updateprd = pd.tblProducts.Where(x => x.Id == pc.Id).FirstOrDefault <tblProduct>();

            if (updateprd != null)
            {
                updateprd.Id               = pc.Id;
                updateprd.Name             = pc.Name;
                updateprd.Category         = pc.Category;
                updateprd.Price            = pc.Price;
                updateprd.Quantity         = pc.Quantity;
                updateprd.ShortDescription = pc.ShortDescription;
                updateprd.LongDescription  = pc.LongDescription;
                updateprd.SmallImage       = pc.SmallImage;
                updateprd.LargeImage       = pc.LargeImage;

                pd.SaveChanges();
            }
            else
            {
                return(NotFound());
            }
            return(Ok());
        }
Example #20
0
        public ActionResult RegisterUser(RegistrationViewModel model)
        {
            ProductDBEntities db = new ProductDBEntities();

            User user = new User()
            {
                Username = model.Username,
                Password = model.Password,
                RoleId   = 3
            };

            db.Users.Add(user);
            db.SaveChanges();

            return(View());
        }
Example #21
0
        public ActionResult DelSelPrd(string[] prdids)
        {
            int[] getid = null;
            if (prdids != null)
            {
                getid = new int[prdids.Length];
                int j = 0;
                foreach (string i in prdids)
                {
                    int.TryParse(i, out getid[j++]);
                }
                List <tblProduct> getprdids = new List <tblProduct>();
                ProductDBEntities pd        = new ProductDBEntities();
                getprdids = pd.tblProducts.Where(x => getid.Contains(x.Id)).ToList();

                foreach (var s in getprdids)
                {
                    pd.tblProducts.Remove(s);
                }
                pd.SaveChanges();
            }
            return(RedirectToAction("ProductList"));
        }