// used to get a cart item from DB, not in use public ProductVisit GetProductVisit(string sessionID) { MC_ShoppingCartEntities db = new MC_ShoppingCartEntities(); ProductVisit sessionData = db.ProductVisits.Where(pv => pv.sessionID == sessionID).FirstOrDefault(); return(sessionData); }
public async Task <ActionResult> Create([Bind(Include = "ProductId,UserId,Name,Description,Price,Category,Createtime,StreetNo,StreetRoute,Suburb,State,Postcode,Status")] Product product) { product.UserId = User.Identity.GetUserId(); product.Createtime = DateTime.Now; product.Status = true; //product; var errors = ModelState.Values.SelectMany(v => v.Errors); if (ModelState.IsValid) { _productDb.Products.Add(product); await _productDb.SaveChangesAsync(); ProductVisit pv = new ProductVisit { ProductId = product.ProductId, VisitTimes = 0 }; _productDb.ProductVisits.Add(pv); await _productDb.SaveChangesAsync(); TempData["productId"] = product.ProductId; return(RedirectToAction("Create", "Image")); } return(View(product)); }
public virtual bool SetVisit(string commercial, string content, Visit visit) { var result = DecryptString(content, commercial); ProductVisit visitProduct = JsonConvert.DeserializeObject <ProductVisit>(result); if (visitProduct != null) { visit.products = new List <ProductVisit> { new ProductVisit { name = visitProduct.name, price = visitProduct.price, facets = visitProduct.facets, rack = visitProduct.rack, missing = visitProduct.missing } }; return(true); } else { return(false); } }
public ActionResult Add(int?productID) { // then didnt come from products page if (productID == null) { return(RedirectToAction("Index")); // send them there } // used to send current qunatity to the page string sessionID = System.Web.HttpContext.Current.Session.SessionID; ShoppingCartRepo cartRepo = new ShoppingCartRepo(); ProductVisit cartItem = cartRepo.GetCartItem(sessionID, (int)productID); ProductVM productVM; if (cartItem == null) { ProductRepo pRepo = new ProductRepo(); Product product = pRepo.GetProduct(productID); productVM = new ProductVM(product); productVM.SetTotalCost(); // quntity times price productVM.image = product.Image.imageTitle; productVM.quantity = 1; } else { productVM = new ProductVM(cartItem.Product); productVM.SetTotalCost(); // quntity times price productVM.image = cartItem.Product.Image.imageTitle; productVM.quantity = cartItem.qtyOrdered; } return(View(productVM)); }
public void RemoveCartBySessionNoTime(string sessionID) { ShoppingCart743Entities context = new ShoppingCart743Entities(); Visit visitToRemove = context.Visits.Find(sessionID); //only removes items if the updates were not done within the last hour if (visitToRemove != null) { ProductVisit productVisit = (from pv in context.ProductVisits where pv.sessionID == sessionID select pv).FirstOrDefault(); DateTime updateTime = ((DateTime)productVisit.updated); DateTime endTime = ((DateTime)productVisit.updated).AddMinutes(59); var productVisitsToRemove = from pv in context.ProductVisits where pv.sessionID == sessionID select pv; foreach (var product_Visit in productVisitsToRemove) { context.ProductVisits.Remove(product_Visit); context.Visits.Remove(visitToRemove); } context.SaveChanges(); } }
// FOR CREATING AND UPDATING SHOPPING CART ITEMS public void AddCartItem(string sessionID, Product product, int?quantity) { // need to update the expiry date of the session aswell? // maybe sliding does this by default MC_ShoppingCartEntities db = new MC_ShoppingCartEntities(); ProductVisit item = db.ProductVisits .Where(pv => pv.productID == product.productID && pv.sessionID == sessionID) .FirstOrDefault(); if (item == null) // if null make one { ProductVisit cartItem = new ProductVisit(); cartItem.sessionID = sessionID; // primary cartItem.productID = product.productID; // primary cartItem.qtyOrdered = quantity; cartItem.updated = DateTime.Now; db.ProductVisits.Add(cartItem); db.SaveChanges(); } else // otherwise update quantity and updated { item.qtyOrdered = quantity; item.updated = DateTime.Now; db.SaveChanges(); } }
public void SaveProductVisit(string sessionID, int productID, string productName, decimal price, int qty) { // reference db ShoppingCartEntities context = new ShoppingCartEntities(); // If item is already in the cart, update qty // othherwise, just toss that item into the cart ProductVisit row = context.ProductVisits.Find(sessionID, productID); if (row == null) { // create and initialize the ProductVisit object ProductVisit pv = new ProductVisit(); pv.sessionID = sessionID; pv.productID = productID; pv.qtyOrdered = qty; pv.updated = DateTime.Now; context.ProductVisits.Add(pv); } else { row.qtyOrdered = qty; row.updated = DateTime.Now; } // commit context.SaveChanges(); }
public void AddOrUpdateProductVisit(ProductVisit formProductVisit) { ShoppingCart743Entities context = new ShoppingCart743Entities(); ProductVisit dbProductVisit = (from pv in context.ProductVisits where formProductVisit.sessionID == pv.sessionID && formProductVisit.productID == pv.productID select pv).FirstOrDefault(); if (dbProductVisit != null) { dbProductVisit.productID = formProductVisit.productID; dbProductVisit.qtyOrdered = formProductVisit.qtyOrdered; dbProductVisit.sessionID = formProductVisit.sessionID; dbProductVisit.updated = formProductVisit.updated; //update the time for all of the session items SessionHelper sessionHelper = new SessionHelper(); var dbProductVisits = (from pv in context.ProductVisits where formProductVisit.sessionID == pv.sessionID select pv); foreach (ProductVisit productVisit in dbProductVisits) { productVisit.updated = sessionHelper.EndTime; } context.SaveChanges(); } else { SessionHelper sessionHelper = new SessionHelper(); Visit visit = new Visit(); visit.started = sessionHelper.StartTime; visit.sessionID = sessionHelper.SessionID; Visit dbVisit = (from pv in context.Visits where formProductVisit.sessionID == pv.sessionID select pv).FirstOrDefault(); if (dbVisit == null) { context.Visits.Add(visit); } context.ProductVisits.Add(formProductVisit); //update the session time for all of the sessions-- if the product, session ID combo doesn't exist var dbProductVisits = (from pv in context.ProductVisits where formProductVisit.sessionID == pv.sessionID select pv); foreach (ProductVisit productVisit in dbProductVisits) { productVisit.updated = sessionHelper.StartTime; } context.SaveChanges(); } }
public ProductVisit GetCartItem(string sessionID, int productID) { MC_ShoppingCartEntities db = new MC_ShoppingCartEntities(); ProductVisit cartItem = db.ProductVisits .Where(pv => pv.sessionID == sessionID && pv.productID == productID) .FirstOrDefault(); return(cartItem); }
public ProductVisit GetCartItem(string sessionID, int productID) { ShoppingCart743Entities context = new ShoppingCart743Entities(); ProductVisit cartItem = (from pv in context.ProductVisits where sessionID == pv.sessionID && pv.productID == productID select pv).FirstOrDefault(); return(cartItem); }
// used when click remove button // need to pass session and product ID // this is causing error when called from session start and there is not any data yet public void RemoveProductVisit(string sessionID, int productID) { MC_ShoppingCartEntities db = new MC_ShoppingCartEntities(); ProductVisit productVisit = db.ProductVisits.Where(pv => pv.sessionID == sessionID && pv.productID == productID).FirstOrDefault(); if (productVisit != null) { db.ProductVisits.Remove(productVisit); db.SaveChanges(); } }
// used for testing public void CreateExpiredProductVisit(string sessionID) { MC_ShoppingCartEntities db = new MC_ShoppingCartEntities(); ProductVisit pv = new ProductVisit(); pv.sessionID = sessionID + "-expired"; // primary pv.productID = 107; // primary pv.updated = DateTime.Now.AddHours(-2); db.ProductVisits.Add(pv); db.SaveChanges(); }
// used for testing public ProductVisit CreateProductVisit(string sessionID) { MC_ShoppingCartEntities db = new MC_ShoppingCartEntities(); ProductVisit pv = new ProductVisit(); pv.sessionID = sessionID; // primary pv.productID = 107; // primary pv.updated = DateTime.Now; db.ProductVisits.Add(pv); db.SaveChanges(); return(pv); }
public int GetProductQty(string sessionID, int productID) { ShoppingCartEntities context = new ShoppingCartEntities(); ProductVisit p = context.ProductVisits.Find(sessionID, productID); if (p == null) { return(1); } else { return((int)p.qtyOrdered); } }
public bool Delete(int id, string sessionid) { shoppingcartfaridehEntities db = new shoppingcartfaridehEntities(); ProductVisit productVisit = db.ProductVisits.Where(pv => pv.productID == id && pv.sessionID == sessionid).FirstOrDefault(); try { db.ProductVisits.Remove(productVisit); db.SaveChanges(); return(true); } catch { return(false); } }
public void AddProductVisit(Visit visit, Product prod, int qty) { ProductVisit prodVisitData = (from pv in db.ProductVisits where pv.sessionID == visit.sessionID && pv.productID == prod.productID select pv).FirstOrDefault(); if (prodVisitData == null) { ProductVisit prodVisit = new ProductVisit(); prodVisit.Product = prod; prodVisit.Visit = visit; prodVisit.qtyOrdered = qty; prodVisit.updated = DateTime.Now; db.ProductVisits.Add(prodVisit); } else { prodVisitData.qtyOrdered = qty; prodVisitData.updated = DateTime.Now; } db.SaveChanges(); }
// GET: Products/Details/5 public async Task <ActionResult> Details(Guid id) { string myUserId = User.Identity.GetUserId(); Product product = await _productDb.Products.FindAsync(id); if (product == null) { return(HttpNotFound()); } //++product.VisitTimes; await _productDb.SaveChangesAsync(); var productVisit = _productDb.ProductVisits.Find(id); if (productVisit == null) { ProductVisit pv = new ProductVisit { ProductId = product.ProductId, VisitTimes = 0 }; _productDb.ProductVisits.Add(pv); await _productDb.SaveChangesAsync(); productVisit = _productDb.ProductVisits.Find(id); } int visit = productVisit.VisitTimes; productVisit.VisitTimes = ++visit; await _productDb.SaveChangesAsync(); ProductViewModel model = new ProductViewModel { Product = product, VisitTime = visit, ifFavorite = _productDb.UserFavoriteAds.Where(x => x.UserId == myUserId).Any(x => x.ProductId == id) }; TempData["Username"] = UserManager.FindById(product.UserId).UserName; return(View(model)); }
public void Add(int id, int Quantity, string sessionid) { shoppingcartfaridehEntities db = new shoppingcartfaridehEntities(); try { ProductVisit productVisit = new ProductVisit(); SessionHelper sessionHelper = new SessionHelper(); sessionid = sessionHelper.SessionID; // Delete(id, sessionid); productVisit.productID = id; productVisit.sessionID = sessionHelper.SessionID; productVisit.updated = DateTime.Now; productVisit.qtyOrdered = Quantity; db.ProductVisits.Add(productVisit); db.SaveChanges(); } catch { } }
//Updates a productVisit entry if it already exists. Creates a new one otherwise public void addProductVisit(string sessionId, int productId, int qty, DateTime lastUpdated) { ShoppingCartEntities db = new ShoppingCartEntities(); int productVisit = db.ProductVisits .Where(prodVisit => prodVisit.sessionID == sessionId && prodVisit.productID == productId).Count(); if (productVisit > 0) { var prodVisit = db.ProductVisits .Where(prVisit => prVisit.sessionID == sessionId && prVisit.productID == productId).First(); db.ProductVisits.Remove(prodVisit); db.SaveChanges(); } ProductVisit pv = new ProductVisit(); pv.sessionID = sessionId; pv.productID = productId; pv.qtyOrdered = qty; pv.updated = lastUpdated; db.ProductVisits.Add(pv); db.SaveChanges(); }
public ActionResult Add(int id) { SessionHelper sessionHelper = new SessionHelper(); ViewBag.SessionID = System.Web.HttpContext.Current.Session.SessionID; ProductVisitRepo productVisitRepo = new ProductVisitRepo(); ProductVisit productVisitFromDB = productVisitRepo.GetCartItem(System.Web.HttpContext.Current.Session.SessionID, id); if (productVisitFromDB != null) { ViewBag.Qty = productVisitFromDB.qtyOrdered; } else { ViewBag.Qty = 1; } ProductRepo productRepo = new ProductRepo(); RedirectToAction("ViewCart"); return(View(productRepo.GetProduct(id))); }
public ActionResult Add(ProductVisit productVisitFromForm) { SessionHelper sessionHelper = new SessionHelper(); productVisitFromForm.sessionID = sessionHelper.SessionID; productVisitFromForm.updated = sessionHelper.StartTime; ProductVisitRepo productVisitRepo = new ProductVisitRepo(); if (productVisitFromForm.productID != 0 && productVisitFromForm.sessionID != null) { productVisitRepo.AddOrUpdateProductVisit(productVisitFromForm); } ProductVisit productVisitFromDB = productVisitRepo.GetCartItem(sessionHelper.SessionID, productVisitFromForm.productID); ViewBag.Qty = 1; if (productVisitFromDB.qtyOrdered != 1) { ViewBag.Qty = productVisitFromDB.qtyOrdered; } return(RedirectToAction("ViewCart", new { id = sessionHelper.SessionID })); }
public void UpdateShoppingCart(string sessionID, IEnumerable <ProductPicked> items) { if (items == null) { return; } // reference db ShoppingCartEntities context = new ShoppingCartEntities(); foreach (ProductPicked item in items) { // If item is already in the cart, update qty // othherwise, just toss that item into the cart ProductVisit row = context.ProductVisits.Find(sessionID, item.ProductID); if (row == null) { // add new item to cart ProductVisit pv = new ProductVisit(); pv.sessionID = sessionID; pv.productID = item.ProductID; pv.qtyOrdered = item.QtyOrdered; pv.updated = DateTime.Now; context.ProductVisits.Add(pv); } // update existing item in cart else { row.qtyOrdered = item.QtyOrdered; row.updated = DateTime.Now; } } // commit context.SaveChanges(); }