public static string BuildHttp(AddEditSearchVM svm, string category, string city) { // http://stackoverflow.com/questions/20164298/net-how-to-build-a-url // Building search query. var query = HttpUtility.ParseQueryString(string.Empty); if (svm.MinPrice.HasValue) query["min_price"] = svm.MinPrice.ToString(); if (svm.MaxPrice.HasValue) query["max_price"] = svm.MaxPrice.ToString(); if (svm.SearchText != null) query["query"] = svm.SearchText; // TODO: Add more query options. Make them all nullable. query["format"] = "rss"; // Building the full url here. UriBuilder url = new UriBuilder(); url.Scheme = "https:"; url.Host = city + ".craigslist.org"; // When there is no category specified, we search all. // Category all = "sss" url.Path = (category != null ? "search/" + category : "search/sss"); url.Query = query.ToString(); return url.ToString(); }
public ActionResult AddSearch(AddEditSearchVM vm) { profileData = AuthCookies.DeserializeCookie<UserProfileSessionData>(HttpContext.Request.Cookies["authenticationToken"]); try { if (ModelState.IsValid) { SearchCriteria sc = new SearchCriteria { ProfileId = profileData.ProfileId, SearchText = vm.SearchText, MinPrice = vm.MinPrice, MaxPrice = vm.MaxPrice, }; db.AddEntry(sc); List<CategorySearch> catSearch = new List<CategorySearch>(); foreach (CheckBoxCategoryVM c in vm.Categories.Where(w => w.IsChecked)) { string http = RssPages.BuildHttp(vm, c.Code, profileData.City); catSearch.Add(new CategorySearch { SearchId = sc.SearchId, Category = c.Code, SearchLink = http }); } db.AddEntries(catSearch); } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Please try again. If the same error keeps occurring, try again another time."); } return RedirectToAction("Index"); }
public ActionResult AddSearch() { ViewBag.AddEdit = "Add"; AddEditSearchVM vm = new AddEditSearchVM(); vm.Categories = GetCategories(); return View("AddEditSearch", vm); }
public static List<AddEditSearchVM> GetAddEditSearchVMByProfileId(int profileId) { List<AddEditSearchVM> addEditSearchVMs = new List<AddEditSearchVM>(); List<int> uniqueSearches = new List<int>(); using (var data = new DataAccess()) { data.ProcedureName = "uspGetAddEditSearchVMByProfileId"; data.AddParm("@ProfileId", SqlDbType.Int, profileId); DataTable results = data.ExecReturnDataTable(); CheckBoxCategoryVM category = new CheckBoxCategoryVM(); foreach (DataRow row in results.Rows) { int searchId = Convert.ToInt32(row["SearchId"]); if (!uniqueSearches.Contains(searchId)) { uniqueSearches.Add(searchId); AddEditSearchVM search = new AddEditSearchVM { SearchId = Convert.ToInt32(row["SearchId"]), SearchText = row["SearchText"].ToString(), MinPrice = (!(row["MinPrice"] is DBNull) ? Convert.ToDecimal(row["MinPrice"]) : 0m), MaxPrice = (!(row["MaxPrice"] is DBNull) ? Convert.ToDecimal(row["MaxPrice"]) : 0m) }; category = new CheckBoxCategoryVM() { Code = row["Category"].ToString(), Name = DictCategory.GetCategoryName(row["Category"].ToString()), IsChecked = true }; search.Categories.Add(category); addEditSearchVMs.Add(search); } else { addEditSearchVMs.Find(x => x.SearchId == searchId).Categories.Add(new CheckBoxCategoryVM() { Code = row["Category"].ToString(), Name = DictCategory.GetCategoryName(row["Category"].ToString()), IsChecked = true }); } } } return addEditSearchVMs; }
public static AddEditSearchVM GetAddEditSearchVMBySearchId(int? searchId) { AddEditSearchVM searchVM = new AddEditSearchVM(); using (var data = new DataAccess()) { data.ProcedureName = "uspGetAddEditSearchVMBySearchId"; data.AddParm("@searchId", SqlDbType.Int, searchId); DataTable results = data.ExecReturnDataTable(); DataRow firstRow = results.Rows[0]; searchVM.SearchId = searchId ?? default(int); searchVM.SearchText = firstRow["SearchText"].ToString(); searchVM.MinPrice = (!(firstRow["MinPrice"] is DBNull) ? Convert.ToDecimal(firstRow["MinPrice"]) : 0m); searchVM.MaxPrice = (!(firstRow["MaxPrice"] is DBNull) ? Convert.ToDecimal(firstRow["MaxPrice"]) : 0m); CheckBoxCategoryVM category = new CheckBoxCategoryVM(); foreach (DataRow row in results.Rows) { category = new CheckBoxCategoryVM() { Code = row["Category"].ToString(), Name = DictCategory.GetCategoryName(row["Category"].ToString()), IsChecked = true }; searchVM.Categories.Add(category); } } return searchVM; }
public ActionResult EditSearch(AddEditSearchVM vm, AddEditSearchVM oldState, int id) { try { if (ModelState.IsValid) { profileData = AuthCookies.DeserializeCookie<UserProfileSessionData>(HttpContext.Request.Cookies["authenticationToken"]); AddEditSearchVM previousState = Procedures.GetAddEditSearchVMBySearchId(id); List<UpdateCategories> updateCategories = new List<UpdateCategories>(); foreach (CheckBoxCategoryVM cat in vm.Categories) { CheckBoxCategoryVM previousCat = new CheckBoxCategoryVM(); previousCat = previousState.Categories.FirstOrDefault(s => s.Code == cat.Code) ?? null; if (cat.IsChecked && previousCat == null) { string http = RssPages.BuildHttp(vm, cat.Code, profileData.City); updateCategories.Add(new UpdateCategories() { Category = cat.Code, InsertOrDelete = cat.IsChecked, SearchLink = http }); } if (previousCat != null && previousCat.IsChecked && !cat.IsChecked) { updateCategories.Add(new UpdateCategories() { Category = previousCat.Code, InsertOrDelete = false }); } } Procedures.UpdateSearchCriteria(id, vm.SearchText, vm.MinPrice ?? default(decimal), vm.MaxPrice ?? default(decimal), updateCategories); } } catch (RetryLimitExceededException) { ModelState.AddModelError("", "Unable to save changes. Please try again. If the same error keeps occurring, try again another time."); } return RedirectToAction("Index"); }