Пример #1
0
        private async Task <LocateModel> Locate(LocateModel model)
        {
            model.Country = await model.Country.CountryNameCheck(model.City);

            if (string.IsNullOrEmpty(model.Country))
            {
                return(new LocateModel());
            }
            var addressUri = new Uri($"https://maps.googleapis.com/maps/api/geocode/json?latlng={model.Lat},{model.Lng}{ConstValues.GoogleLocateKey}");

            using (var client = new HttpClient())
            {
                var resp = await client.GetAsync(addressUri);

                var json = await resp.Content.ReadAsStringAsync();

                var token = JToken.Parse(json);
                var array = token.SelectToken("results");
                if (!array.HasValues || !array.Any())
                {
                    return(model);
                }
                var deserialized = JsonConvert.DeserializeObject <List <UserLocationParsingBase> >(array.ToString());
                deserialized.RemoveAll(x => x.Geometry.LocationType != "GEOMETRIC_CENTER");
                var item = deserialized.FirstOrDefault();
                if (item == null)
                {
                    return(model);
                }
                var addressItem = item.AddressComponents.FirstOrDefault(x => x.Types.Contains("route"))?.Name;
                model.Address = addressItem;
            }
            model.Address = model.Address?.Replace(InvalidChars, string.Empty);
            return(model);
        }
 public ActionResult Edit(LocateModel model)
 {
     if (ModelState.IsValid)
     {
         locateService.Update(model);
         ModelState.Clear();
         return(RedirectToAction("Index"));
     }
     return(PartialView("_Edit", model));
 }
 public ActionResult Create(LocateModel model)
 {
     if (ModelState.IsValid)
     {
         locateService.Insert(model);
         ModelState.Clear();
         return(RedirectToAction("Index"));
     }
     return(PartialView("_Create", model));
 }
Пример #4
0
        public void Insert(LocateModel model)
        {
            tbl_Locates details = new tbl_Locates();

            details.CreatedDate = DateTime.Now;
            details.Description = model.Description;
            details.IsPublic    = model.IsPublic;
            details.Name        = model.Name;
            LocateRepository.Create(details);
        }
Пример #5
0
        public void Delete(LocateModel model)
        {
            tbl_Locates details = new tbl_Locates();

            details.Id = model.Id;

            details.Description = model.Description;
            details.IsPublic    = model.IsPublic;
            details.Name        = model.Name;
            LocateRepository.Delete(details);
        }
Пример #6
0
        public LocateModel GetbyID(int ID)
        {
            tbl_Locates temp    = LocateRepository.GetbyID(ID);
            LocateModel details = new LocateModel();

            details.Id          = temp.Id;
            details.CreatedDate = temp.CreatedDate;
            details.Description = temp.Description;
            details.IsPublic    = temp.IsPublic;
            details.Name        = temp.Name;
            return(details);
        }
Пример #7
0
        protected (decimal, decimal) GetRequesterLatLng()
        {
            string ip     = HttpContext.Connection.RemoteIpAddress.ToString();
            var    result = new LocateModel();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(ConstValues.IpAddressUrl);
                var response   = client.GetAsync(ip).Result;
                var serialized = response.Content.ReadAsStringAsync().Result;
                var token      = JToken.Parse(serialized);
                var resultJson = token.SelectToken("data").ToString();
                result = JsonConvert.DeserializeObject <LocateModel>(resultJson);
            }
            return(result.Lat, result.Lng);
        }
Пример #8
0
        public async Task Locate(string userName, string ip, string verifiedBy)
        {
            var result = new LocateModel();

            if (string.IsNullOrEmpty(ip))
            {
                return;
            }
            Enum.TryParse(verifiedBy, out VerifiedBy verified);
            var caller = await _repository.Filter <User>(x => (x.Email == userName ||
                                                               (x.PhoneCode + x.Phone) == userName) && x.VerifiedBy == verified).FirstOrDefaultAsync();

            if (caller == null)
            {
                throw new SmartException("No user found");
            }
            dynamic property = new ExpandoObject();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(ConstValues.IpAddressUrl);
                var response = await client.GetAsync(ip);

                if (!response.IsSuccessStatusCode)
                {
                    return;
                }
                var serialized = await response.Content.ReadAsStringAsync();

                var token      = JToken.Parse(serialized);
                var resultJson = token.SelectToken("data").ToString();
                result = JsonConvert.DeserializeObject <LocateModel>(resultJson);
            }
            if (result.Lat != 0.0m && result.Lng != 0.0m && Utilities.IsNullOrEmpty(result.City, result.Country))
            {
                if (string.IsNullOrEmpty(result.Address))
                {
                    result.Address = "Unnamed Road";
                }
                var userLocation = await _repository.Filter <UserLocation>(x => x.UserId == caller.Id).FirstOrDefaultAsync();

                lock (this)
                {
                    if (userLocation == null)
                    {
                        _repository.Create(new UserLocation
                        {
                            UserId    = caller.Id,
                            Address   = result.Address,
                            City      = result.City,
                            Lat       = result.Lat,
                            Long      = result.Lng,
                            Country   = result.Country,
                            IpAddress = result.Ip
                        });
                        _repository.SaveChanges();
                    }
                    else
                    {
                        userLocation.Address   = result.Address;
                        userLocation.City      = result.City;
                        userLocation.Lat       = result.Lat;
                        userLocation.Long      = result.Lng;
                        userLocation.Country   = result.Country;
                        userLocation.IpAddress = result.Ip;
                        _repository.Update(userLocation);
                        _repository.SaveChanges();
                    }
                }
            }
        }