Backend: Improve entity log and error messages

Signed-off-by: Michael Mayer <michael@liquidbytes.net>
This commit is contained in:
Michael Mayer 2020-05-19 12:48:32 +02:00
parent 1cf0893b18
commit 27ea0cb214
3 changed files with 20 additions and 3 deletions

View file

@ -76,6 +76,10 @@ func SavePhotoForm(model Photo, form form.Photo, geoApi string) error {
return err
}
if !model.HasID() {
return errors.New("photo: can't save form, id is empty")
}
model.UpdateYearMonth()
if form.Details.PhotoID == model.ID {
@ -120,8 +124,12 @@ func SavePhotoForm(model Photo, form form.Photo, geoApi string) error {
return nil
}
// Save stored the entity in the database.
// Save the entity in the database.
func (m *Photo) Save() error {
if !m.HasID() {
return errors.New("photo: can't save to database, id is empty")
}
db := Db()
labels := m.ClassifyLabels()
@ -296,6 +304,11 @@ func (m *Photo) PreloadMany() {
m.PreloadAlbums()
}
// HasID checks if the photo has a database id and uuid.
func (m *Photo) HasID() bool {
return m.ID > 0 && m.PhotoUUID != ""
}
// NoLocation checks if the photo has no location
func (m *Photo) NoLocation() bool {
return m.LocationID == ""

View file

@ -70,6 +70,8 @@ func TestSavePhotoForm(t *testing.T) {
}
func TestPhoto_Save(t *testing.T) {
/* Creating new photos with Save() not supported (yet)
t.Run("new photo", func(t *testing.T) {
photo := Photo{
ID: 11111,
@ -105,6 +107,8 @@ func TestPhoto_Save(t *testing.T) {
t.Fatal(err)
}
})
*/
t.Run("existing photo", func(t *testing.T) {
m := PhotoFixtures.Get("19800101_000002_D640C559")
err := m.Save()

View file

@ -58,7 +58,7 @@ func FindPlaceByLabel(id string, label string) *Place {
return nil
}
} else if err := Db().First(place, "id = ? OR loc_label = ?", id, label).Error; err != nil {
log.Debugf("place: %s for id %s or label %s", err.Error(), id, txt.Quote(label))
log.Debugf("place: %s for id %s / label %s", err.Error(), id, txt.Quote(label))
return nil
}
@ -77,7 +77,7 @@ func (m *Place) Find() error {
// FirstOrCreate checks if the place already exists in the database
func (m *Place) FirstOrCreate() *Place {
if err := Db().FirstOrCreate(m, "id = ? OR loc_label = ?", m.ID, m.LocLabel).Error; err != nil {
log.Debugf("place: %s for token %s or label %s", err.Error(), m.ID, txt.Quote(m.LocLabel))
log.Debugf("place: %s for token %s / label %s", err.Error(), m.ID, txt.Quote(m.LocLabel))
}
return m