Beispiel #1
0
        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);
        }
Beispiel #2
0
        public void Download(string city, string state, string zipcode, string categories, out int total, out int downloaded)
        {
            int totalDownload = 0;
            int totalDownloaded = 0;

            var query = new Query();
            query.And(
            query.Field("locality").Equal(city),
            query.Field("region").Equal(state),
            query.Field("postcode").Equal(zipcode),
            query.Field("category_ids").Includes(categories),
            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(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 = "Download Tool",
                                            CreateDate = DateTime.Now,
                                            Name = biz.name,
                                            Region = biz.region,
                                            Status = Infrastructure.Domain.Status.Approved,
                                            //reference
                                            UserId = userId,
                                            Zipcode = zipcode
                                        };

                                        businessService.Create(bus);
                                    }
                                }
                            }

                            totalDownload = cate.TotalRecord = total_row_count == null ? 0 : total_row_count;
                            cate.Downloaded += cate.TotalRecord;
                            totalDownloaded = cate.Downloaded;
                            category_ZipCodeService.Update(cate);

                        }
                    }
                }
            }
            catch (FactualApiException ce)
            {
            }
            total = totalDownload;
            downloaded = totalDownloaded;
        }