/// <summary> /// Update the user's recently viewed product in the cookie and database /// </summary> /// <param name="s_id">shop id</param> /// <param name="p_id">product id</param> /// <param name="max">max amount of product views</param> public static HttpCookie updateViewCookieHistory(int userId, HttpCookie cookie, int s_id, int p_id = 0, int max = 10) { HttpCookie returnCookie = new HttpCookie(cookie.Name); returnCookie.Value = ""; SIEBUEntities db = new SIEBUEntities(); Store store = db.Stores.FirstOrDefault(s => s.s_id == s_id); Product product = db.Products.FirstOrDefault(p => p.p_id == p_id); User_SearchHistory record = db.User_SearchHistory.FirstOrDefault(search => search.user_id == userId && search.product_id == product.p_id); if (record != null) //modify existing record in the database or add it { record.dateaccessed = DateTime.Now; } else //add new record in the database { record = new User_SearchHistory(); record.product_id = product.p_id; record.store_id = store.s_id; record.user_id = userId; record.dateaccessed = DateTime.Now; db.User_SearchHistory.Add(record); } db.SaveChanges(); //Set the cookie history //var cookie = Request.Cookies["item_hist"]; String itemHist = ""; HttpCookie productCookie = null; if (cookie == null) //cookie doesn't exist { productCookie = recentStoreViewedCookie(userId); itemHist = productCookie.Value; //Response.Cookies.Add(productCookie); } else //get the value { itemHist = cookie.Value; } if (itemHist != "") { String[] cookieRecords = itemHist.Split('|'); Boolean contains = false; int index = -1; try { for (int i = 0; i < cookieRecords.Length; i++) // Check and find the index of the item if it is in the cookie { String[] cookieRecord = cookieRecords[i].Split('&'); int storeId = int.Parse(cookieRecord[0].ToString()); int productId = int.Parse(cookieRecord[2].ToString()); if (storeId == store.s_id && product.p_id == productId) { contains = true; index = i; break; } } if (!contains) // new record add it in the cookie { String cookieValue = ""; if (cookieRecords.Length >= max) { for (int i = 0; i < cookieRecords.Length - 1; i++) //Take the first items in the record { cookieValue = cookieValue + "|" + cookieRecords[i]; } //add element 0 before cookieValue; returnCookie.Value = getItemCookieValue(store.s_id, store.name_space, product.p_id, product.name, (product.Product_Image.Count > 0 ? product.Product_Image.First().url : "/content/no-image"), store.name, (store.logo != null) ? store.logo : "/content/no-image") + cookieValue; } else { returnCookie.Value = getItemCookieValue(store.s_id, store.name_space, product.p_id, product.name, (product.Product_Image.Count > 0 ? product.Product_Image.First().url : "/content/no-image"), store.name, (store.logo != null) ? store.logo : "/content/no-image") + "|" + cookieValue; } } else //Modify existing record in the cookie { String cookieValue = ""; if (index != 0) //first element is not the product { if (index == cookieRecords.Length - 1) //the record is the last element in the cookie. { cookieValue = cookieRecords[cookieRecords.Length - 1]; for (int i = 0; i != cookieRecords.Length - 1; i++) { cookieValue = cookieValue + "|" + cookieRecords[i]; } returnCookie.Value = cookieValue; } else //record in the middle { String before = cookieRecords[0]; // for sure not the first element since we checked earlier for (int i = 1; i < index; i++) { before = before + "|" + cookieRecords[i]; } String after = cookieRecords[index + 1]; for (int i = index + 2; i < cookieRecords.Length; i++) { after = after + "|" + cookieRecords[i]; } returnCookie.Value = cookieRecords[index] + "|" + before + "|" + after; } } else //first element is the first product. Just return the same thing { returnCookie.Value = itemHist; //just return the same cookie. } } } catch (Exception e) { //Debug.WriteLine("Exception has occured with modifying the recently viewed store cookie: " + e.ToString()); //Response.Cookies.Add(NavigationModel.recentProductsViewedCookie(WebSecurity.CurrentUserId)); returnCookie.Value = itemHist; } } else // First record on the list { returnCookie.Value = NavigationModel.getItemCookieValue(store.s_id, store.name_space, product.p_id, product.name, (product.Product_Image.Count > 0 ? product.Product_Image.First().url : "/content/no-image"), store.name, (store.logo != null) ? store.logo : "/content/no-image"); } return returnCookie; }
/// <summary> /// Update the recent stores viewed in the cookie and add in the database /// </summary> /// <param name="userId">user's id</param> /// <param name="store">store viewed</param> /// <param name="max">max items in a cookie</param> /// <returns></returns> public static HttpCookie updateStoreViewHistory(int userId, HttpCookie cookie, Store store, int max = 10) { HttpCookie returnCookie = new HttpCookie(cookie.Name); returnCookie.Value = ""; //add the new store in the database SIEBUEntities db = new SIEBUEntities(); User_SearchHistory record = db.User_SearchHistory.FirstOrDefault(search => search.store_id == store.s_id && search.product_id == null); if (record != null) //modify existing record in the database or add it { record.dateaccessed = DateTime.Now; } else //add new record in the database { record = new User_SearchHistory(); record.store_id = store.s_id; record.user_id = userId; record.dateaccessed = DateTime.Now; db.User_SearchHistory.Add(record); } db.SaveChanges(); String storeHist = ""; HttpCookie storeCookie = null; if (cookie == null) //cookie doesn't exist { storeCookie = recentStoreViewedCookie(userId); storeHist = storeCookie.Value; } else //get the value { storeHist = cookie.Value; } if (storeHist != "") { String[] cookieRecords = storeHist.Split('|'); Boolean contains = false; int index = -1; try { for (int i = 0; i < cookieRecords.Length; i++) // Check and find the index of the item if it is in the cookie { String[] cookieRecord = cookieRecords[i].Split('&'); int storeId = int.Parse(cookieRecord[0].ToString()); if (storeId == store.s_id) { contains = true; index = i; break; } } String cookieValue = ""; if (!contains) //new record { if (cookieRecords.Length >= max) { for (int i = 0; i < cookieRecords.Length - 1; i++) //Take the first items in the record minus the last element { cookieValue = cookieValue + "|" + cookieRecords[i]; } //add element 0 before cookieValue; returnCookie.Value = getStoreCookieValue(store.s_id, store.name, store.name_space, (store.logo != null) ? store.logo : "/content/no-image") + cookieValue; } else { returnCookie.Value = getStoreCookieValue(store.s_id, store.name, store.name_space, (store.logo != null) ? store.logo : "/content/no-image") + "|" + storeHist; } } else //modify existing cookie { if (index != 0) // if index == 0 then it's just the first element and we don't need to do anything { if (index == cookieRecords.Length - 1) //the record is the last element in the cookie. { cookieValue = cookieRecords[cookieRecords.Length - 1]; for (int i = 0; i != cookieRecords.Length - 1; i++) { cookieValue = cookieValue + "|" + cookieRecords[i]; } returnCookie.Value = cookieValue; } else //record is in the middle of the cookie { String before = cookieRecords[0]; for (int i = 1; i < index; i++) { before = before + "|" + cookieRecords[i]; } String after = cookieRecords[index + 1]; for (int i = index + 2; i < cookieRecords.Length; i++) { after = after + "|" + cookieRecords[i]; } returnCookie.Value = cookieRecords[index] + "|" + before + "|" + after; } } else { returnCookie.Value = storeHist; //just return the same cookie. } } } catch (Exception e) { //Debug.WriteLine("Exception has occured with modifying the recently viewed store cookie: " + e.ToString()); returnCookie.Value = storeHist; //Response.Cookies.Add(recentStoreViewedCookie(WebSecurity.CurrentUserId)); } } else //first shop viewed. { returnCookie.Value = getStoreCookieValue(store.s_id, store.name, store.name_space, store.logo); } return returnCookie; }