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); }
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); }
public static List<CategoryModel> GetAllCategories() { using (ClothesShopEntities entity = new ClothesShopEntities()) { return new List<CategoryModel>(entity.Categories.Select(category => new CategoryModel() { Name = category.CategoryName, ID = category.ID })); } }
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 }); }
public static string GetCategoriesTree() { List<CategoryNode> result; using (ClothesShopEntities entities = new ClothesShopEntities()) { result = new List<CategoryNode>(); foreach (ClothesShop.Category cat in entities.Categories) { CategoryNode node = new CategoryNode(); node.CategoryId = cat.ID; node.CategoryName = cat.CategoryName; node.Level = 0; node.HasChildren = true; foreach (ClothesShop.SubCategory subCat in cat.SubCategories) { CategoryNode subNode = new CategoryNode(); subNode.CategoryName = subCat.SubCategoryName + "(" + subCat.Products.Count + ")"; subNode.CategoryId = subCat.ID; subNode.Level = 1; subNode.HasChildren = false; node.ChildNodes.Add(subNode); } result.Add(node); } } JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); string json = jsonSerializer.Serialize(result); return json; }
public static List<SubcategoryModel> GetAllSubcategories() { using (ClothesShopEntities entity = new ClothesShopEntities()) { return new List<SubcategoryModel>(entity.SubCategories.Select(subcategory => new SubcategoryModel() { SubcategoryName = subcategory.SubCategoryName, ID = subcategory.ID , CategoryName = subcategory.Category.CategoryName, CategoryID = subcategory.CategoryID})); } }
public ActionResult Thumbnail(int id, int? size) { using (ClothesShopEntities entities = new ClothesShopEntities()) { Image image = entities.Images.Where(x => x.ID == id).FirstOrDefault(); if (image == null) { return new HttpStatusCodeResult(404); } return new ThumbnailResult(image.FileName, size ?? 120); } }
public static List<ProductModel> GetAllProducts(int? categoryId = null, int? subCategoryId = null) { using (ClothesShopEntities entity = new ClothesShopEntities()) { if (subCategoryId != null && subCategoryId.HasValue) { return new List<ProductModel>(entity.Products.Where(x => x.SubCategoryID == subCategoryId.Value).Select(x => new ProductModel() { Entity = x })); } else if (categoryId != null && categoryId.HasValue) { return new List<ProductModel>(entity.Products.Where(x => x.SubCategory.CategoryID == categoryId.Value).Select(x => new ProductModel() { Entity = x })); } else { return new List<ProductModel>(entity.Products.Select(x => new ProductModel() { Entity = x })); } } }
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"); }
public ActionResult LogOn(LogOnModel model) { string returnUrl = model.ReturnUrl; if (ModelState.IsValid) { using (ClothesShopEntities entities = new ClothesShopEntities()) { ClothesShop.User user = entities.Users.Where(x => x.Username == model.UserName && x.Password == model.Password).FirstOrDefault(); if (user != null) { Session["Username"] = model.UserName; Session["UserID"] = user.ID; Session["IsAuthenticated"] = true; Session["IsAdmin"] = user.IsAdmin; Session["Basket"] = new BasketModel(); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } } // If we got this far, something failed, redisplay form return View(model); }
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"); }
public static object GetCategoryItems(int categoryID) { using (ClothesShopEntities entity = new ClothesShopEntities()) { var result = entity.Products .Where(product => product.SubCategory.CategoryID == categoryID) .Select(product => new { ProductName = product.Name, ProductID = product.ID, UnitPrice = product.Price, ImageFileName = product.Images.FirstOrDefault() != null ? product.Images.FirstOrDefault().FileName : "noimage.png" }); return result.ToList(); } }
public static List<QuantityModel> GetProductQuantities(int id) { using (ClothesShopEntities entity = new ClothesShopEntities()) { return new List<QuantityModel>(entity.Products.Where(x => x.ID == id).First().Quantities.Select(x => new QuantityModel() { ProductId = x.ProductID, ID = x.ID, Quantity = x.Quantity1, Size = (int)x.Size })); } }
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"); }
public ActionResult ViewOrders(DateTime startDate, DateTime endDate) { OrdersModel model = new OrdersModel(startDate, endDate); endDate = endDate.Date + new TimeSpan(1, 0, 0, 0); using (ClothesShopEntities entity = new ClothesShopEntities()) { model.AddRange(entity.Orders.Where(order => order.OrderDate >= startDate && order.OrderDate <= endDate).OrderBy(order => order.OrderDate) .Select(order => new OrderItem() { Date = order.OrderDate, ID = order.ID, Username = order.User.Username })); } return View(model); }
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 }); }
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"); }
public static Product GetProduct(int productId) { ClothesShopEntities entities = new ClothesShopEntities(); return entities.Products.Where(x => x.ID == productId).FirstOrDefault(); }
public ActionResult OrderDetails(int id) { using (ClothesShopEntities entity = new ClothesShopEntities()) { BasketModel model = new BasketModel(); Order order = entity.Orders.Where(o => o.ID == id).FirstOrDefault(); if (order != null) { model.AddRange(order.OrderedProducts.Select(op => new BasketItem(new ProductModel(op.Product), op.Quantity, op.Size))); } return View(model); } }
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 }); }
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"); }
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 }); }
public ActionResult EditProduct(int id) { ViewBag.SubCategories = DataHelper.GetAllSubcategories(); using (ClothesShopEntities entity = new ClothesShopEntities()) { return View(new ProductModel(entity.Products.Where(x => x.ID == id).FirstOrDefault())); } }
public static object GetSubCategoryItems(int subCategoryID) { using (ClothesShopEntities entities = new ClothesShopEntities()) { var result = from product in entities.Products where product.SubCategoryID == subCategoryID select new { ProductName = product.Name, ProductID = product.ID, UnitPrice = product.Price, ImageFileName = product.Images.FirstOrDefault() != null ? product.Images.FirstOrDefault().FileName : "noimage.png" }; return result.ToList(); } }
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"); }
public static List<PictureModel> GetProductImages(int id) { using (ClothesShopEntities entity = new ClothesShopEntities()) { return new List<PictureModel>(entity.Products.Where(x => x.ID == id).First().Images.Select(x => new PictureModel() { ProductId = x.ProductID, ID = x.ID, FileName = x.FileName })); } }