public void removeOldVisits(DateTime now) { ShoppingCartEntities db = new ShoppingCartEntities(); var prodVisits = db.ProductVisits; DateTime oneHourAgo = DateTime.Now.AddHours(-1); List<ProductVisit> oldProdVisits = db.ProductVisits.Where(pv => DateTime.Compare((DateTime)pv.updated, oneHourAgo) < 0 ).ToList(); //Remove from Visit and ProductVisit sessions that are older than 1 hour foreach (var pv in oldProdVisits) { var prodVisit = db.ProductVisits.Where(prodv => prodv.sessionID == pv.sessionID).FirstOrDefault(); if (prodVisit != null) { db.ProductVisits.Remove(prodVisit); var visit = db.Visits.Where(v => v.sessionID == prodVisit.sessionID).FirstOrDefault(); if (visit != null) { db.Visits.Remove(visit); } } } //Remove Visits that are older than 1 hour (sessions that didn't add anything to a cart) var Visits = db.Visits; List<Visit> oldVisits = db.Visits.Where(v => DateTime.Compare((DateTime)v.started, oneHourAgo) < 0).ToList(); if (oldVisits.Count() > 0) { db.Visits.RemoveRange(oldVisits); } db.SaveChanges(); }
public void removeProductVisit(string sessionId) { ShoppingCartEntities db = new ShoppingCartEntities(); var prodVisit = (from pv in db.ProductVisits where pv.sessionID == sessionId select pv); if (prodVisit.Count() > 0) { db.ProductVisits.RemoveRange(prodVisit); db.SaveChanges(); } }
public void removeVisit(string id) { ShoppingCartEntities db = new ShoppingCartEntities(); var visit = (from v in db.Visits where v.sessionID == id select v).FirstOrDefault(); if ((from vis in db.Visits where vis.sessionID == id select vis).Count() > 0) { var productVisits = db.ProductVisits.Where(pv => pv.sessionID == visit.sessionID); db.ProductVisits.RemoveRange(productVisits); db.Visits.Remove(visit); db.SaveChanges(); } }
public void addVisit(string id, DateTime start) { ShoppingCartEntities db = new ShoppingCartEntities(); Visit visit = new Visit(); var prevVisit = db.Visits.SingleOrDefault(v => v.sessionID == id); if (prevVisit == null) { visit.sessionID = id; visit.started = start; db.Visits.Add(visit); db.SaveChanges(); } }
//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(); }