2019-12-28 20:24:20 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2020-01-08 19:51:21 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2019-12-28 20:24:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Photo place
|
|
|
|
type Place struct {
|
2019-12-31 01:34:27 +01:00
|
|
|
ID string `gorm:"type:varbinary(16);primary_key;auto_increment:false;"`
|
2019-12-28 20:24:20 +01:00
|
|
|
LocLabel string `gorm:"type:varbinary(500);unique_index;"`
|
|
|
|
LocCity string `gorm:"type:varchar(100);"`
|
|
|
|
LocState string `gorm:"type:varchar(100);"`
|
|
|
|
LocCountry string `gorm:"type:binary(2);"`
|
|
|
|
LocNotes string `gorm:"type:text;"`
|
|
|
|
LocFavorite bool
|
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
New bool `gorm:"-"`
|
|
|
|
}
|
|
|
|
|
2019-12-31 01:34:27 +01:00
|
|
|
var UnknownPlace = NewPlace("-", "Unknown", "Unknown", "Unknown", "zz")
|
2019-12-28 20:24:20 +01:00
|
|
|
|
|
|
|
func CreateUnknownPlace(db *gorm.DB) {
|
|
|
|
UnknownPlace.FirstOrCreate(db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) AfterCreate(scope *gorm.Scope) error {
|
|
|
|
return scope.SetColumn("New", true)
|
|
|
|
}
|
|
|
|
|
2019-12-31 01:34:27 +01:00
|
|
|
func FindPlace(token string, db *gorm.DB) *Place {
|
2019-12-28 20:24:20 +01:00
|
|
|
place := &Place{}
|
|
|
|
|
2019-12-31 01:34:27 +01:00
|
|
|
if err := db.First(place, "id = ?", token).Error; err != nil {
|
|
|
|
log.Debugf("place: %s for token %s", err.Error(), token)
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return place
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:38:21 +01:00
|
|
|
func FindPlaceByLabel(id string, label string, db *gorm.DB) *Place {
|
2019-12-28 20:24:20 +01:00
|
|
|
place := &Place{}
|
|
|
|
|
2020-01-02 05:38:21 +01:00
|
|
|
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, label)
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return place
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:38:21 +01:00
|
|
|
func NewPlace(id, label, city, state, countryCode string) *Place {
|
2019-12-28 20:24:20 +01:00
|
|
|
result := &Place{
|
2020-01-02 05:38:21 +01:00
|
|
|
ID: id,
|
2019-12-28 20:24:20 +01:00
|
|
|
LocLabel: label,
|
|
|
|
LocCity: city,
|
|
|
|
LocState: state,
|
|
|
|
LocCountry: countryCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) Find(db *gorm.DB) error {
|
|
|
|
if err := db.First(m, "id = ?", m.ID).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) FirstOrCreate(db *gorm.DB) *Place {
|
2020-01-08 19:51:21 +01:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
if err := db.FirstOrCreate(m, "id = ? OR loc_label = ?", m.ID, m.LocLabel).Error; err != nil {
|
2019-12-31 01:34:27 +01:00
|
|
|
log.Debugf("place: %s for token %s or label \"%s\"", err.Error(), m.ID, m.LocLabel)
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) NoID() bool {
|
2019-12-31 01:34:27 +01:00
|
|
|
return m.ID == ""
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) Label() string {
|
|
|
|
return m.LocLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) City() string {
|
|
|
|
return m.LocCity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) State() string {
|
|
|
|
return m.LocState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) CountryCode() string {
|
|
|
|
return m.LocCountry
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) CountryName() string {
|
|
|
|
return maps.CountryNames[m.LocCountry]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Place) Notes() string {
|
|
|
|
return m.LocNotes
|
|
|
|
}
|