public ActionResult EditProduct(int id) { // Declare productVM ProductVM model; using (TicketAppDB db = new TicketAppDB()) { // Get the product ProductDTO dto = db.Products.Find(id); // Make sure product exists if (dto == null) { return(Content("That product does not exist.")); } // init model model = new ProductVM(dto); // Make a select list model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name"); // Get all gallery images /*model.GalleryImages = Directory.EnumerateFiles(Server.MapPath("~/Images/Uploads/Products/" + id + "/Gallery/Thumbs")) * .Select(fn => Path.GetFileName(fn));*/ } // Return view with model return(View(model)); }
// GET: Admin/Shop/DeleteProduct/id public ActionResult DeleteProduct(int id) { string product = ""; // Delete product from DB using (TicketAppDB db = new TicketAppDB()) { ProductDTO dto = db.Products.Find(id); PdfDTO pdf = db.Pdfs.Where(x => x.ProductId == id).FirstOrDefault(); product = dto.Name; PhotoDTO photo = db.Photos.Where(x => x.ProductId == id).FirstOrDefault(); //Determine if product is an order if (db.OrderDetails.Any(x => x.ProductId == id)) { OrderDetailsDTO dte = db.OrderDetails.Where(x => x.ProductId == id).FirstOrDefault(); OrderDTO ord = db.Orders.Where(x => x.OrderId == dte.OrderId).FirstOrDefault(); db.OrderDetails.Remove(dte); db.Orders.Remove(ord); } db.Pdfs.Remove(pdf); db.Photos.Remove(photo); db.Products.Remove(dto); db.SaveChanges(); } TempData["SM"] = "You have deleted '" + product + "'!"; // Redirect return(RedirectToAction("Products", "Shop")); }
public string RenameCategory(string newCatName, int id) { using (TicketAppDB db = new TicketAppDB()) { //Check category name is unique if (db.Categories.Any(x => x.Name == newCatName)) { return("titletaken"); } //Get DTO CategoryDTO dto = db.Categories.Find(id); //Change listings that were in old category name to new category name List <ProductDTO> products = db.Products.Where(x => x.CategoryId == id).ToList(); foreach (ProductDTO prod in products) { prod.CategoryName = newCatName; } //Edit DTO dto.Name = newCatName; dto.Slug = newCatName.Replace(" ", "-").ToLower(); //Save db.SaveChanges(); } //Return return("ok"); }
public string AddNewCategory(string catName) { //Declare id string id; using (TicketAppDB db = new TicketAppDB()) { //Check that the category name is unique if (db.Categories.Any(x => x.Name == catName)) { return("titletaken"); } //Init DTO CategoryDTO dto = new CategoryDTO(); //Add to DTO dto.Name = catName; dto.Slug = catName.Replace(" ", "-").ToLower(); dto.Sorting = 100; //Save DTO db.Categories.Add(dto); db.SaveChanges(); //Get the id id = dto.Id.ToString(); } //Return id return(id); }
// GET: Admin/Shop/Products public ActionResult Products(int?page, int?catId) { //Declare a list of ProductVM List <ProductVM> listOfProductVM; //Set page number var pageNumber = page ?? 1; using (TicketAppDB db = new TicketAppDB()) { //Get seller name //Init the list listOfProductVM = db.Products.ToArray() .Where(x => catId == null || catId == 0 || x.CategoryId == catId).Where(x => x.IsSold == false) .Select(x => new ProductVM(x)) .ToList(); //Populate categories select list ViewBag.Categories = new SelectList(db.Categories.ToList(), "Id", "Name"); //Set selected category ViewBag.SelectedCat = catId.ToString(); } //Set pagination var onePageOfProducts = listOfProductVM.ToPagedList(pageNumber, 3); ViewBag.OnePageOfProducts = onePageOfProducts; //Return view with list return(View(listOfProductVM)); }
// GET: /Cart/DecrementProduct public ActionResult DecrementProduct(int productId) { // Init cart List <CartVM> cart = Session["cart"] as List <CartVM>; using (TicketAppDB db = new TicketAppDB()) { // Get model from list CartVM model = cart.FirstOrDefault(x => x.ProductId == productId); // Decrement qty if (model.Quantity > 1) { model.Quantity--; } else { model.Quantity = 0; cart.Remove(model); } // Store needed data var result = new { qty = model.Quantity, price = model.Price }; // Return json return(Json(result, JsonRequestBehavior.AllowGet)); } }
public ActionResult EditProduct(int id) { // Declare productVM ProductVM model; using (TicketAppDB db = new TicketAppDB()) { // Get the product ProductDTO dto = db.Products.Find(id); // Make sure product exists if (dto == null) { return(Content("That product does not exist.")); } // init model model = new ProductVM(dto); // Make a select list model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name"); } // Return view with model return(View(model)); }
public ActionResult Products() { List <ProductsForUserVM> productsForUser = new List <ProductsForUserVM>(); using (TicketAppDB db = new TicketAppDB()) { //Get user id UserDTO user = db.Users.Where(x => x.Username == User.Identity.Name).FirstOrDefault(); int userId = user.Id; //Init List of OrderVM List <ProductVM> products = db.Products.Where(x => x.User.Id == userId).ToArray().Select(x => new ProductVM(x)).ToList(); foreach (var product in products) { productsForUser.Add(new ProductsForUserVM() { ProductId = product.Id, Name = product.Name, Price = product.Price, CategoryName = product.CategoryName, ReservationDate = product.ReservationDate, Verified = product.Verified }); } } return(View(productsForUser)); }
public void PlaceOrder() { // Get cart list List <CartVM> cart = Session["cart"] as List <CartVM>; // Get username string username = User.Identity.Name; int orderId = 0; using (TicketAppDB db = new TicketAppDB()) { // Init OrderDTO OrderDTO orderDTO = new OrderDTO(); // Get user id var q = db.Users.FirstOrDefault(x => x.Username == username); int userId = q.Id; // Add to OrderDTO and save orderDTO.UserId = userId; orderDTO.CreatedAt = DateTime.Now; db.Orders.Add(orderDTO); db.SaveChanges(); // Get inserted id orderId = orderDTO.OrderId; // Init OrderDetailsDTO OrderDetailsDTO orderDetailsDTO = new OrderDetailsDTO(); // Add to OrderDetailsDTO foreach (var item in cart) { orderDetailsDTO.OrderId = orderId; orderDetailsDTO.UserId = userId; orderDetailsDTO.ProductId = item.ProductId; orderDetailsDTO.Quantity = item.Quantity; ProductDTO prod = db.Products.Where(x => x.Id == orderDetailsDTO.ProductId).FirstOrDefault(); prod.IsSold = true; db.OrderDetails.Add(orderDetailsDTO); db.SaveChanges(); } } // Email admin var client = new SmtpClient("smtp.mailtrap.io", 2525) { Credentials = new NetworkCredential("90b88b746f7644", "fe9c04ebbf4216"), EnableSsl = true }; client.Send("*****@*****.**", "*****@*****.**", "New Order", "You have a new order. Order number " + orderId); // Reset session Session["cart"] = null; }
public ActionResult CreateAccount(UserVM model) { // Check model state if (!ModelState.IsValid) { return(View("CreateAccount", model)); } // Check if passwords match if (!model.Password.Equals(model.ConfirmPassword)) { ModelState.AddModelError("", "Passwords do not match."); return(View("CreateAccount", model)); } using (TicketAppDB db = new TicketAppDB()) { // Make sure username is unique if (db.Users.Any(x => x.Username.Equals(model.Username))) { ModelState.AddModelError("", "Username " + model.Username + " is taken."); model.Username = ""; return(View("CreateAccount", model)); } // Create userDTO UserDTO userDTO = new UserDTO() { FirstName = model.FirstName, LastName = model.LastName, EmailAddress = model.EmailAddress, Username = model.Username, Password = model.Password }; // Add the DTO db.Users.Add(userDTO); // Save db.SaveChanges(); // Add to UserRolesDTO int id = userDTO.Id; UserRoleDTO userRolesDTO = new UserRoleDTO() { UserId = id, RoleId = 2 }; db.UserRoles.Add(userRolesDTO); db.SaveChanges(); } // Create a TempData message TempData["SM"] = "You are now registered and can login."; // Redirect return(Redirect("~/account/login")); }
protected void Application_AuthenticateRequest() { // Check if user is logged in if (User == null) { return; } // Get username string username = Context.User.Identity.Name; // Declare array of roles string[] roles = null; using (TicketAppDB db = new TicketAppDB()) { // Populate roles UserDTO dto = db.Users.FirstOrDefault(x => x.Username == username); roles = db.UserRoles.Where(x => x.UserId == dto.Id).Select(x => x.Role.Name).ToArray(); } // Build IPrincipal object IIdentity userIdentity = new GenericIdentity(username); IPrincipal newUserObj = new GenericPrincipal(userIdentity, roles); // Update Context.User Context.User = newUserObj; }
public ActionResult ProductDetails(string name) { // Declare the VM and DTO ProductVM model; ProductDTO dto; // Init product id int id = 0; using (TicketAppDB db = new TicketAppDB()) { // Check if product exists if (!db.Products.Any(x => x.Slug.Equals(name))) { return(RedirectToAction("Index", "Shop")); } // Init productDTO dto = db.Products.Where(x => x.Slug == name).FirstOrDefault(); // Get id id = dto.Id; // Init model model = new ProductVM(dto); } // Return view with model return(View("ProductDetails", model)); }
public ActionResult EditPage(PageVM model) { //Check model state if (!ModelState.IsValid) { return(View(model)); } using (TicketAppDB db = new TicketAppDB()) { //Get page id int id = model.Id; //Init slug string slug = "home"; //Get the page PageDTO dto = db.Pages.Find(id); //DTO the title dto.Title = model.Title; //Check for slug and set it if need be if (model.Slug != "home") { if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } } //Make sure title and slug are unique if (db.Pages.Where(x => x.Id != id).Any(x => x.Title == model.Title) || db.Pages.Where(x => x.Id != id).Any(x => x.Slug == slug)) { ModelState.AddModelError("", "That title or slug already exists."); return(View(model)); } //DTO the rest dto.Slug = slug; dto.Body = model.Body; dto.HasSideBar = model.HasSideBar; //Save the DTO db.SaveChanges(); } //Set TempData message TempData["SM"] = "You have edited the page!"; //Redirect return(RedirectToAction("EditPage")); }
public ActionResult GetUnverified() { using (TicketAppDB db = new TicketAppDB()) { List <ProductDTO> listings = db.Products.Where(x => x.Verified == 0).ToList(); return(PartialView(listings)); } }
public ActionResult AddToCartPartial(int id) { // Init CartVM list List <CartVM> cart = Session["cart"] as List <CartVM> ?? new List <CartVM>(); // Init CartVM CartVM model = new CartVM(); using (TicketAppDB db = new TicketAppDB()) { // Get the product ProductDTO product = db.Products.Find(id); // Check if the product is already in cart var productInCart = cart.FirstOrDefault(x => x.ProductId == id); // If not, add new if (productInCart == null) { cart.Add(new CartVM() { ProductId = product.Id, ProductName = product.Name, Quantity = 1, Price = product.Price, Image = product.ImageName }); } else { // If it is, increment productInCart.Quantity++; } } // Get total qty and price and add to model int qty = 0; decimal price = 0m; foreach (var item in cart) { qty += item.Quantity; price += item.Quantity * item.Price; } model.Quantity = qty; model.Price = price; // Save cart back to session Session["cart"] = cart; // Return partial view with model return(PartialView(model)); }
public ActionResult UserProfile(UserProfileVM model) { // Check model state if (!ModelState.IsValid) { return(View("UserProfile", model)); } // Check if passwords match if need be if (!string.IsNullOrWhiteSpace(model.Password)) { if (!model.Password.Equals(model.ConfirmPassword)) { ModelState.AddModelError("", "Passwords do not match."); return(View("UserProfile", model)); } } using (TicketAppDB db = new TicketAppDB()) { // Get username string username = User.Identity.Name; // Make sure username is unique if (db.Users.Where(x => x.Id != model.Id).Any(x => x.Username == username)) { ModelState.AddModelError("", "Username " + model.Username + " already exists."); model.Username = ""; return(View("UserProfile", model)); } // Edit DTO UserDTO dto = db.Users.Find(model.Id); dto.FirstName = model.FirstName; dto.LastName = model.LastName; dto.EmailAddress = model.EmailAddress; dto.Username = model.Username; if (!string.IsNullOrWhiteSpace(model.Password)) { dto.Password = model.Password; } // Save db.SaveChanges(); } // Set TempData message TempData["SM"] = "You have edited your profile!"; // Redirect return(Redirect("~/account/user-profile")); }
public ActionResult DeleteUser(int id) { using (TicketAppDB db = new TicketAppDB()) { //Get products, orders and details of user List <ProductDTO> listings = db.Products.Where(x => x.UserId == id).ToList(); List <OrderDetailsDTO> userDetails = db.OrderDetails.Where(x => x.UserId == id).ToList(); List <OrderDTO> orders = db.Orders.Where(x => x.UserId == id).ToList(); //Init List of prod details foreach (ProductDTO prod in listings) { if (prod != null) { userDetails.Add(db.OrderDetails.Where(x => x.ProductId == prod.Id).FirstOrDefault()); PdfDTO pdf = db.Pdfs.Where(x => x.ProductId == prod.Id).FirstOrDefault(); PhotoDTO photo = db.Photos.Where(x => x.ProductId == prod.Id).FirstOrDefault(); if (pdf != null) { db.Pdfs.Remove(pdf); db.Photos.Remove(photo); } db.Products.Remove(prod); } } foreach (OrderDetailsDTO det in userDetails) { if (det != null) { orders.Add(db.Orders.Where(x => x.OrderId == det.OrderId).FirstOrDefault()); foreach (OrderDTO or in orders) { if (or != null) { db.Orders.Remove(or); } } db.OrderDetails.Remove(det); } } UserDTO user = db.Users.Where(x => x.Id == id).FirstOrDefault(); string u = user.Username; db.Users.Remove(user); db.SaveChanges(); TempData["SM"] = "You have removed " + u + " from the website."; return(RedirectToAction("Index")); } }
// GET: Admin/Pages public ActionResult Index() { //Declare list of PageVM List <PageVM> pagesList; using (TicketAppDB db = new TicketAppDB()) { //Init the list pagesList = db.Pages.ToArray().OrderBy(x => x.Sorting).Select(x => new PageVM(x)).ToList(); } //Return list with view return(View(pagesList)); }
public ActionResult PagesMenuPartial() { // Declare a list of PageVM List <PageVM> pageVMList; // Get all pages except home using (TicketAppDB db = new TicketAppDB()) { pageVMList = db.Pages.ToArray().OrderBy(x => x.Sorting).Where(x => x.Slug != "home").Select(x => new PageVM(x)).ToList(); } // Return partial view with list return(PartialView(pageVMList)); }
// GET: /Cart/RemoveProduct public void RemoveProduct(int productId) { // Init cart list List <CartVM> cart = Session["cart"] as List <CartVM>; using (TicketAppDB db = new TicketAppDB()) { // Get model from list CartVM model = cart.FirstOrDefault(x => x.ProductId == productId); // Remove model from list cart.Remove(model); } }
public ActionResult AddProduct() { //Init model ProductVM model = new ProductVM(); //Add select list of categories to model using (TicketAppDB db = new TicketAppDB()) { model.Categories = new SelectList(db.Categories.ToList(), "Id", "Name"); } //Return view with model return(View(model)); }
public ActionResult CategoryMenuPartial() { // Declare list of CategoryVM List <CategoryVM> categoryVMList; // Init the list using (TicketAppDB db = new TicketAppDB()) { categoryVMList = db.Categories.ToArray().OrderBy(x => x.Sorting).Select(x => new CategoryVM(x)).ToList(); } // Return partial with list return(PartialView(categoryVMList)); }
public ActionResult AddPage(PageVM model) { //Check model state if (!ModelState.IsValid) { return(View(model)); } using (TicketAppDB db = new TicketAppDB()) { //Declare slug string slug; //Init pageDTO PageDTO dto = new PageDTO(); //DTO title dto.Title = model.Title; //Check for and set slug if need be if (string.IsNullOrWhiteSpace(model.Slug)) { slug = model.Title.Replace(" ", "-").ToLower(); } else { slug = model.Slug.Replace(" ", "-").ToLower(); } //Make sure tytle and slug are unique if (db.Pages.Any(x => x.Title == model.Title) || db.Pages.Any(x => x.Slug == slug)) { ModelState.AddModelError("", "That title or slug already exists."); return(View(model)); } //DTO the rest dto.Slug = slug; dto.Body = model.Body; dto.HasSideBar = model.HasSideBar; dto.Sorting = 100; db.Pages.Add(dto); db.SaveChanges(); } //Set TempData message TempData["SM"] = "You have added a new page!"; //Redirect return(RedirectToAction("AddPage")); }
public ActionResult SidebarPartial() { // Declare model SidebarVM model; // Init model using (TicketAppDB db = new TicketAppDB()) { SidebarDTO dto = db.Sidebar.Find(1); model = new SidebarVM(dto); } // Return partial view with model return(PartialView(model)); }
// GET: Index/{page} public ActionResult Index(string page = "") { // Get/set page slug if (page == "") { page = "home"; } // Declare model and DTO PageVM model; PageDTO dto; // Check if page exists using (TicketAppDB db = new TicketAppDB()) { if (!db.Pages.Any(x => x.Slug.Equals(page))) { return(RedirectToAction("Index", new { page = "" })); } } // Get page DTO using (TicketAppDB db = new TicketAppDB()) { dto = db.Pages.Where(x => x.Slug == page).FirstOrDefault(); } // Set page title ViewBag.PageTitle = dto.Title; // Check for sidebar if (dto.HasSideBar == true) { ViewBag.Sidebar = "Yes"; } else { ViewBag.Sidebar = "No"; } // Init model model = new PageVM(dto); // Return view with model return(View(model)); }
public ActionResult EditSidebar() { //Declare model SidebarVM model; using (TicketAppDB db = new TicketAppDB()) { //Get the DTO SidebarDTO dto = db.Sidebar.Find(1); //Init model model = new SidebarVM(dto); } //Return view with model return(View(model)); }
// GET: Admin/Pages/DeletePage/id public ActionResult DeletePage(int id) { using (TicketAppDB db = new TicketAppDB()) { //Get the page PageDTO dto = db.Pages.Find(id); //Remove the page db.Pages.Remove(dto); //Save db.SaveChanges(); } //Redirect return(RedirectToAction("Index")); }
public ActionResult GetUsers() { List <UserDTO> users = new List <UserDTO>(); using (TicketAppDB db = new TicketAppDB()) { List <UserRoleDTO> roles = db.UserRoles.Where(x => x.RoleId == 2).ToList(); foreach (UserRoleDTO r in roles) { if (db.Users.Any(x => x.Id == r.UserId)) { UserDTO user = db.Users.Where(x => x.Id == r.UserId).FirstOrDefault(); users.Add(user); } } } return(PartialView(users)); }
public ActionResult Categories() { //Declare a list of models List <CategoryVM> categoryVMList; using (TicketAppDB db = new TicketAppDB()) { //Init list categoryVMList = db.Categories .ToArray() .OrderBy(x => x.Sorting) .Select(x => new CategoryVM(x)) .ToList(); } //Return view with list return(View(categoryVMList)); }
// GET: /shop/category/name public ActionResult Category(int?page, int?catId, string name) { // Declare a list of ProductVM List <ProductVM> productVMList; //init page list //Set page number var pageNumber = page ?? 1; using (TicketAppDB db = new TicketAppDB()) { // Get category id CategoryDTO categoryDTO = db.Categories.Where(x => x.Slug == name).FirstOrDefault(); catId = categoryDTO.Id; // Init the list productVMList = db.Products.ToArray().Where(x => x.CategoryId == catId && x.IsSold == false && x.Verified != 0).Select(x => new ProductVM(x)).ToList(); // Get category name var productCat = db.Products.Where(x => x.CategoryId == catId).FirstOrDefault(); if (productCat != null) { ViewBag.CategoryName = productCat.CategoryName; } //Populate categories select list ViewBag.Categories = new SelectList(db.Categories.ToList(), "Id", "Name"); //Set selected category ViewBag.SelectedCat = catId.ToString(); } //Set pagination var onePageOfProducts = productVMList.ToPagedList(pageNumber, 3); ViewBag.OnePageOfProducts = onePageOfProducts; // Return view with list return(View(productVMList)); }