Example #1
0
        public ActionResult AddImage(int id)
        {
            HttpPostedFileBase file = Request.Files[0];
            //TODO: check file extensions

            string fileName = Guid.NewGuid().ToString() + ".png";
            string dir = Server.MapPath("/ProductPictures");
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            file.SaveAs(Server.MapPath("/ProductPictures/" + fileName));

            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                Image image = new Image()
                {
                    ProductID = id,
                    FileName = fileName
                };
                entity.Images.AddObject(image);
                entity.SaveChanges();
            }

            return RedirectToAction("ProductImages", new { id = id });
        }
Example #2
0
        public ActionResult CheckOut()
        {
            bool success = false;
            BasketModel basket = Session["Basket"] as BasketModel;
            if (basket != null)
            {
                using (ClothesShopEntities entity = new ClothesShopEntities())
                {
                    Order order = new Order() { OrderDate = DateTime.Now, UserID = (int)Session["UserID"] };
                    foreach (BasketItem item in basket)
                    {
                        OrderedProduct orderedProduct = new OrderedProduct() { ProductID = item.Product.ID, Quantity = item.Quantity };
                        order.OrderedProducts.Add(orderedProduct);
                    }

                    entity.Orders.AddObject(order);
                    try
                    {
                        entity.SaveChanges();
                        success = true;
                    }
                    catch
                    {
                        success = false;
                    }
                    basket.Clear();
                }
            }

            return View(success);
        }
Example #3
0
        public ActionResult AddCategory(CategoryModel categoryModel)
        {
            if (ModelState.IsValid)
            {
                using (ClothesShopEntities entity = new ClothesShopEntities())
                {
                    if (entity.Categories.Where(category => category.CategoryName == categoryModel.Name).Count() == 0)
                    {
                        Category newCategory = new Category() { CategoryName = categoryModel.Name };
                        entity.AddToCategories(newCategory);
                        entity.SaveChanges();
                    }
                    else
                    {
                        ModelState.AddModelError("", "A category with the same name already exists.");
                    }
                }
            }

            return RedirectToAction("Categories");
        }
Example #4
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                using (ClothesShopEntities entities = new ClothesShopEntities())
                {
                    if (entities.Users.Where(x => x.Username == model.UserName).Count() > 0)
                    {
                        ModelState.AddModelError("", "Username already exists");
                        return View(model);
                    }

                    ClothesShop.User user = new ClothesShop.User();
                    user.Username = model.UserName;
                    user.Password = model.Password;
                    user.IsAdmin = false;
                    entities.Users.AddObject(user);
                    entities.SaveChanges();

                    Session["Username"] = user.Username;
                    Session["IsAuthenticated"] = true;
                    return RedirectToAction("Index", "Home");
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Example #5
0
        public ActionResult AddProduct(ProductModel product)
        {
            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                Product prod = new Product()
                {
                    SubCategoryID = product.SubCategoryID,
                    No = product.Number,
                    Name = product.Name,
                    NameEN = product.NameEN,
                    Description = product.Description,
                    DescriptionEN = product.DescriptionEN,
                    Price = product.Price,
                    Weight = product.Weight,
                    Special = product.Special,
                    QuantityType = (byte)product.QuantityType,
                    Quantity = product.Quantity,
                };
                entity.Products.AddObject(prod);
                entity.SaveChanges();
            }

            return RedirectToAction("Products");
        }
Example #6
0
 public ActionResult RemoveSubCategory(int id)
 {
     if (ModelState.IsValid)
     {
         using (ClothesShopEntities entity = new ClothesShopEntities())
         {
             SubCategory subcategory = entity.SubCategories.FirstOrDefault(c => c.ID == id);
             if (subcategory != null)
             {
                 entity.SubCategories.DeleteObject(subcategory);
                 entity.SaveChanges();
             }
             else
             {
                 ModelState.AddModelError("", "A subcategory with the given ID does not exist.");
             }
         }
     }
     return RedirectToAction("SubCategories");
 }
Example #7
0
        public ActionResult RemoveProduct(int id)
        {
            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                entity.Products.DeleteObject(entity.Products.Where(x => x.ID == id).FirstOrDefault());
                entity.SaveChanges();
            }

            return RedirectToAction("Products");
        }
Example #8
0
        public ActionResult RemoveImage(int id)
        {
            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                Image image = entity.Images.Where(x => x.ID == id).FirstOrDefault();
                string fileName = Server.MapPath("/ProductPictures/" + image.FileName);

                if (System.IO.File.Exists(fileName))
                {
                    System.IO.File.Delete(fileName);
                }

                entity.Images.DeleteObject(image);
                entity.SaveChanges();
            }

            return RedirectToAction("ProductImages", new { id = id });
        }
Example #9
0
        public ActionResult EditProduct(ProductModel product)
        {
            ViewBag.SubCategories = DataHelper.GetAllSubcategories();

            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                Product prod = entity.Products.Where(x => x.ID == product.ID).FirstOrDefault();

                prod.SubCategoryID = product.SubCategoryID;
                prod.No = product.Number;
                prod.Name = product.Name;
                prod.NameEN = product.NameEN;
                prod.Description = product.Description;
                prod.DescriptionEN = product.DescriptionEN;
                prod.Price = product.Price;
                prod.Weight = product.Weight;
                prod.Special = product.Special;
                prod.QuantityType = (byte)product.QuantityType;
                prod.Quantity = product.Quantity;

                entity.SaveChanges();
            }

            return RedirectToAction("Products");
        }
Example #10
0
        public ActionResult DeleteQuantity(int id)
        {
            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                Quantity q = entity.Quantities.Where(x => x.ID == id).FirstOrDefault();
                entity.DeleteObject(q);
                entity.SaveChanges();
            }

            return RedirectToAction("ProductQuantity", new { id = id });
        }
Example #11
0
        public ActionResult AddSubCategory(SubcategoryModel model)
        {
            if (ModelState.IsValid)
            {
                using (ClothesShopEntities entity = new ClothesShopEntities())
                {
                    if (entity.Categories.Where(category => category.ID == model.CategoryID).Count() != 0)
                    {
                        if (entity.SubCategories.Where(subcategory => subcategory.SubCategoryName == model.SubcategoryName && subcategory.CategoryID == model.CategoryID).Count() == 0)
                        {
                            SubCategory newSubcategory = new SubCategory() { SubCategoryName = model.SubcategoryName, CategoryID = model.CategoryID };
                            entity.AddToSubCategories(newSubcategory);
                            entity.SaveChanges();
                        }
                        else
                        {
                            ModelState.AddModelError("", "A subcategory with the same name already exists.");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "A category with the given ID does not exist.");
                    }
                }
            }

            return RedirectToAction("SubCategories");
        }
Example #12
0
        public ActionResult AddQuantity(int id, int size, int quantity)
        {
            using (ClothesShopEntities entity = new ClothesShopEntities())
            {
                Product product = entity.Products.Where(x => x.ID == id).FirstOrDefault();
                Quantity q = new Quantity();
                q.Size = size;
                q.Quantity1 = quantity;
                product.Quantities.Add(q);
                entity.SaveChanges();
            }

            return RedirectToAction("ProductQuantity", new { id = id });
        }