Try fetching country again if create fails #357

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-07-07 16:56:02 +02:00
parent 14420b02a9
commit 4b7551fb99
2 changed files with 25 additions and 20 deletions

View file

@ -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

View file

@ -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
}
}