Пример #1
0
        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;
        }
Пример #2
0
        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;
        }
Пример #3
0
        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");
        }