public ActionResult DownloadInfo(BusinessCategoryViewModel item) { var userId = User.Identity.GetUserId(); if (item != null) { if (item.IsChoose && (item.TotalDownloaded < item.Availble || (item.Availble == 0 && item.TotalDownloaded == 0))) { var query = new Query(); query.And( query.Field("locality").Equal(item.City), query.Field("region").Equal(item.State), query.Field("postcode").Equal(item.Zipcode), query.Field("category_ids").Includes(item.CategoryInclude), query.IncludeRowCount(true) ); try { string re = Factual.Fetch("restaurants-us", query); if (!String.IsNullOrEmpty(re)) { Category_ZipCode cate = category_ZipCodeService.Get().FirstOrDefault(t => t.ZipcodeName.Equals(item.Zipcode)); if (cate != null) { dynamic jsonResponse = JsonConvert.DeserializeObject(re); //import to business and update Downloaded if (jsonResponse.response != null && jsonResponse.status == "ok") { dynamic data = jsonResponse.response.data; dynamic total_row_count = jsonResponse.response.total_row_count; dynamic included_rows = jsonResponse.response.included_rows; if (data != null) { JArray listData = data as JArray; if (listData != null) { for (int i = 0; i < listData.Count; i++) { dynamic biz = listData[i]; //insert to business Business bus = new Business() { Id = Guid.NewGuid(), Address = biz.address, Phone = biz.tel, Latitude = biz.latitude ?? 0, Longtitude = biz.longitude ?? 0, Locality = biz.locality, Active = true, BusinessCategoryId = cate.BusinessCategoryId, Country = biz.country, CreateBy = HttpContext.User.Identity.Name, CreateDate = DateTime.Now, Name = biz.name, Region = biz.region, Status = Infrastructure.Domain.Status.Approved, //reference UserId = userId, Zipcode = item.Zipcode }; businessService.Create(bus); } } } cate.TotalRecord = total_row_count == null ? 0 : total_row_count; cate.Downloaded += cate.TotalRecord; category_ZipCodeService.Update(cate); item.TotalDownloaded = cate.Downloaded; item.Availble = cate.TotalRecord; } } } } catch (FactualApiException ce) { } } } return Json(item, JsonRequestBehavior.AllowGet); }
public ActionResult GetInfo(FactualDownloadParameter parameter) { List<BusinessCategoryViewModel> listReturn = new List<BusinessCategoryViewModel>(); if (parameter != null) { //get zip code of county var zipcodes = zipcodeService.Get() .Where(c => c.City.Equals(parameter.City) && c.Country.Equals(parameter.Country) && c.State.Equals(parameter.State)) .ToList(); //get categoryL1 BusinessCategory CategoryL1; BusinessCategory CategoryL2; BusinessCategory CategoryL3; BusinessCategory CategoryL4; BusinessCategory MainCategory = new BusinessCategory(); ArrayList category_ids = new ArrayList(); if (parameter.CategoryL1 != null) { CategoryL1 = businessCategoryService.Get(parameter.CategoryL1); MainCategory = CategoryL1; if (MainCategory != null) { category_ids.Add(MainCategory.CategoryId); } } if (parameter.CategoryL2 != null && parameter.CategoryL2 != Guid.Empty) { CategoryL2 = businessCategoryService.Get(parameter.CategoryL2); MainCategory = CategoryL2; if (MainCategory != null) { category_ids.Add(MainCategory.CategoryId); } } if (parameter.CategoryL3 != null && parameter.CategoryL3 != Guid.Empty) { CategoryL3 = businessCategoryService.Get(parameter.CategoryL3); MainCategory = CategoryL3; if (MainCategory != null) { category_ids.Add(MainCategory.CategoryId); } } if (parameter.CategoryL4 != null && parameter.CategoryL4 != Guid.Empty) { CategoryL4 = businessCategoryService.Get(parameter.CategoryL4); MainCategory = CategoryL4; if (MainCategory != null) { category_ids.Add(MainCategory.CategoryId); } } string categoryIDs = string.Join(",", category_ids.ToArray()); if (MainCategory != null && MainCategory.Id != Guid.Empty) { foreach (var zip in zipcodes) { //insert new Category_ZipCode cate = new Category_ZipCode(); cate.Id = Guid.NewGuid(); cate.BusinessCategoryId = MainCategory.Id; cate.ZipcodeId = zip.Id; cate.ZipcodeName = zip.Zip; cate.TotalRecord = 0; cate.Downloaded = 0; cate.CategoryName = MainCategory.Name; cate.CategoryId = MainCategory.CategoryId; var cate_zip = category_ZipCodeService.Get().FirstOrDefault(c => c.BusinessCategoryId == MainCategory.Id && c.ZipcodeName == zip.Zip); if (cate_zip == null) { category_ZipCodeService.Add(cate); } //convert to businesCategory View model BusinessCategoryViewModel model = new BusinessCategoryViewModel(); model.Category_ZipId = cate.Id; model.Zipcode = zip.Zip; model.TotalDownloaded = cate_zip == null ? 0 : cate_zip.Downloaded; model.State = zip.State; model.City = zip.City; model.Availble = cate_zip == null ? 0 : cate_zip.TotalRecord; model.IsChoose = false; model.CategoryInclude = categoryIDs; listReturn.Add(model); } } } return Json(listReturn, JsonRequestBehavior.AllowGet); }
public List<BusinessCategoryViewModel> GetDownloadInfo(string country, string state, string city, int categoryId) { List<BusinessCategoryViewModel> result = new List<BusinessCategoryViewModel>(); var zipcodes = zipcodeService.Get() .Where(c => c.City.Equals(city) && c.Country.Equals(country) && c.State.Equals(state)) .ToList(); var businessCategory = businessCategoryService.GetByFactualCategoryId(categoryId); if (businessCategory != null) { foreach (var zip in zipcodes) { //insert new var businessCategoryId = businessCategory.Id; Category_ZipCode cate = new Category_ZipCode(); cate.Id = Guid.NewGuid(); cate.BusinessCategoryId = businessCategoryId; cate.ZipcodeId = zip.Id; cate.ZipcodeName = zip.Zip; cate.TotalRecord = 0; cate.Downloaded = 0; cate.CategoryName = businessCategory.Name; cate.CategoryId = categoryId; var cate_zip = category_ZipCodeService.Get().FirstOrDefault(c => c.BusinessCategoryId == businessCategoryId && c.ZipcodeName == zip.Zip); if (cate_zip == null) { category_ZipCodeService.Add(cate); } //convert to businesCategory View model BusinessCategoryViewModel model = new BusinessCategoryViewModel(); model.Category_ZipId = cate.Id; model.Zipcode = zip.Zip; model.TotalDownloaded = cate_zip == null ? 0 : cate_zip.Downloaded; model.State = zip.State; model.City = zip.City; model.Availble = cate_zip == null ? 0 : cate_zip.TotalRecord; result.Add(model); } } return result; }