diff --git a/internal/entity/country.go b/internal/entity/country.go index 7c7dfbfba..76038194b 100644 --- a/internal/entity/country.go +++ b/internal/entity/country.go @@ -68,22 +68,25 @@ func (m *Country) Create() error { func FirstOrCreateCountry(m *Country) *Country { result := Country{} - if err := Db().Where("id = ?", m.ID).First(&result).Error; err == nil { + if findErr := Db().Where("id = ?", m.ID).First(&result).Error; findErr == nil { return &result - } else if err := m.Create(); err != nil { - log.Errorf("country: %s", err) - return nil + } else if createErr := m.Create(); createErr == nil { + if !m.Unknown() { + event.EntitiesCreated("countries", []*Country{m}) + + event.Publish("count.countries", event.Data{ + "count": 1, + }) + } + + return m + } else if err := Db().Where("id = ?", m.ID).First(&result).Error; err == nil { + return &result + } else { + log.Errorf("country: %s (first or create %s)", createErr, m.ID) } - if !m.Unknown() { - event.EntitiesCreated("countries", []*Country{m}) - - event.Publish("count.countries", event.Data{ - "count": 1, - }) - } - - return m + return nil } // AfterCreate sets the New column used for database callback diff --git a/internal/entity/location.go b/internal/entity/location.go index 8d6d6a11a..9706be407 100644 --- a/internal/entity/location.go +++ b/internal/entity/location.go @@ -65,10 +65,10 @@ func (m *Location) Find(api string) error { return err } - if place := FindPlace(l.PrefixedToken(), l.Label()); place != nil { - m.Place = place + if found := FindPlace(l.PrefixedToken(), l.Label()); found != nil { + m.Place = found } else { - place = &Place{ + place := &Place{ ID: l.PrefixedToken(), LocLabel: l.Label(), LocCity: l.City(), @@ -78,10 +78,7 @@ func (m *Location) Find(api string) error { PhotoCount: 1, } - if err := place.Create(); err != nil { - log.Errorf("place: failed adding %s %s", place.ID, err.Error()) - m.Place = &UnknownPlace - } else { + if err := place.Create(); err == nil { event.Publish("count.places", event.Data{ "count": 1, }) @@ -89,6 +86,11 @@ func (m *Location) Find(api string) error { log.Infof("place: added %s [%s]", place.ID, time.Since(start)) m.Place = place + } else if found := FindPlace(l.PrefixedToken(), l.Label()); found != nil { + m.Place = found + } else { + log.Errorf("place: %s (add place %s for location %s)", err, place.ID, l.ID) + m.Place = &UnknownPlace } }