Пример #1
0
 public static LocationComponentDto ToLocationComponentDto(this GoogleAddressComponent component)
 {
     return(new LocationComponentDto
     {
         Name = component.LongName
     });
 }
Пример #2
0
        public ContentResult SetLocation(PostNewLocationRequest postNewLocationRequest)
        {
            try
            {
                var thumbnail = Context.Thumbnails.Include(t => t.Location).FirstOrDefault(t => t.Id == postNewLocationRequest.ThumbnailId);
                if (thumbnail == null)
                {
                    return(AjaxFailedResult("Image not found."));
                }

                var location = Context.Locations.Include(l => l.LocationComponents).ThenInclude(lc => lc.Component).FirstOrDefault(l => l.PlaceId == postNewLocationRequest.PlaceId);
                if (string.Equals(postNewLocationRequest.PlaceId, location?.PlaceId, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(AjaxResult(location.ToLocationDto(), $"Set location for { thumbnail.FileName } to { location.ToLocationDto().Formatted }"));
                }

                if (location == null)
                {
                    location = new Location
                    {
                        Name      = postNewLocationRequest.Name,
                        PlaceId   = postNewLocationRequest.PlaceId,
                        Latitude  = postNewLocationRequest.Lat,
                        Longitude = postNewLocationRequest.Lng
                    };

                    foreach (var component in postNewLocationRequest.Components)
                    {
                        var types       = $"|{string.Join("|", component.types.Select(t => (int)ConvertJsGoogleAddressType(t)).OrderBy(t => t))}|";
                        var dbComponent = Context.Components.FirstOrDefault(c =>
                                                                            c.LongName == component.LongName && c.ShortName == component.ShortName &&
                                                                            c.TypesString == types) ??
                                          Context.Components.Local.FirstOrDefault(c =>
                                                                                  c.LongName == component.LongName && c.ShortName == component.ShortName &&
                                                                                  c.TypesString == types);
                        if (dbComponent == null)
                        {
                            dbComponent = new GoogleAddressComponent
                            {
                                LongName    = component.LongName,
                                ShortName   = component.ShortName,
                                TypesString = types
                            };
                            Context.Components.Add(dbComponent);
                        }
                        var lc = new LocationComponent
                        {
                            Component   = dbComponent,
                            ComponentId = dbComponent.Id,
                            Location    = location,
                            LocationId  = location.Id
                        };
                        Context.LocationComponent.Add(lc);
                    }
                    Context.Locations.Add(location);
                }
                else if (!location.Latitude.HasValue || !location.Longitude.HasValue)
                {
                    location.Latitude  = postNewLocationRequest.Lat;
                    location.Longitude = postNewLocationRequest.Lng;
                }

                thumbnail.Location = location;
                if (!thumbnail.LatLongFromImage)
                {
                    thumbnail.Latitude  = location.Latitude;
                    thumbnail.Longitude = location.Longitude;
                }
                Context.Entry(thumbnail).State = EntityState.Modified;
                Context.SaveChanges();
                return(AjaxResult(location.ToLocationDto(), $"Set location for { thumbnail.FileName } to { location.ToLocationDto().Formatted }"));
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Failed to set location for {postNewLocationRequest.ThumbnailId} to {postNewLocationRequest.PlaceId}. {ex.Message}: {ex}");
                return(AjaxFailedResult($"Failed to set location for {postNewLocationRequest.ThumbnailId} to {postNewLocationRequest.PlaceId} : {ex.Message}"));
            }
        }
Пример #3
0
 string ProcessAddressComponent(GoogleAddressComponent c)
 {
     return((c == null) ? "" : c.LongName + ", ");
 }