2019-12-28 20:24:20 +01:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2019-12-28 20:24:20 +01:00
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Place used to associate photos to places
|
2019-12-28 20:24:20 +01:00
|
|
|
type Place struct {
|
2019-12-31 01:34:27 +01:00
|
|
|
ID string `gorm:"type:varbinary(16);primary_key;auto_increment:false;"`
|
2020-04-28 19:41:06 +02:00
|
|
|
LocLabel string `gorm:"type:varbinary(768);unique_index;"`
|
|
|
|
LocCity string `gorm:"type:varchar(255);"`
|
|
|
|
LocState string `gorm:"type:varchar(255);"`
|
2020-01-22 13:43:07 +01:00
|
|
|
LocCountry string `gorm:"type:varbinary(2);"`
|
2020-04-28 19:41:06 +02:00
|
|
|
LocKeywords string `gorm:"type:varchar(255);"`
|
2019-12-28 20:24:20 +01:00
|
|
|
LocNotes string `gorm:"type:text;"`
|
|
|
|
LocFavorite bool
|
2020-05-08 15:41:01 +02:00
|
|
|
PhotoCount int
|
2019-12-28 20:24:20 +01:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
New bool `gorm:"-"`
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
// UnknownPlace is defined here to use it as a default
|
|
|
|
var UnknownPlace = Place{
|
2020-04-28 19:41:06 +02:00
|
|
|
ID: "zz",
|
|
|
|
LocLabel: "Unknown",
|
|
|
|
LocCity: "Unknown",
|
|
|
|
LocState: "Unknown",
|
|
|
|
LocCountry: "zz",
|
|
|
|
LocKeywords: "",
|
|
|
|
LocNotes: "",
|
|
|
|
LocFavorite: false,
|
2020-05-08 15:41:01 +02:00
|
|
|
PhotoCount: -1,
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
2019-12-28 20:24:20 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CreateUnknownPlace initializes default place in the database
|
2020-04-30 20:07:03 +02:00
|
|
|
func CreateUnknownPlace() {
|
|
|
|
UnknownPlace.FirstOrCreate()
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// AfterCreate sets the New column used for database callback
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Place) AfterCreate(scope *gorm.Scope) error {
|
|
|
|
return scope.SetColumn("New", true)
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// FindPlaceByLabel returns a place from an id or a label
|
2020-04-30 20:07:03 +02:00
|
|
|
func FindPlaceByLabel(id string, label string) *Place {
|
2019-12-28 20:24:20 +01:00
|
|
|
place := &Place{}
|
|
|
|
|
2020-05-15 13:16:03 +02:00
|
|
|
if label == "" {
|
|
|
|
if err := Db().First(place, "id = ?", id).Error; err != nil {
|
|
|
|
log.Debugf("place: %s for id %s", err.Error(), id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else if err := Db().First(place, "id = ? OR loc_label = ?", id, label).Error; err != nil {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Debugf("place: %s for id %s or label %s", err.Error(), id, txt.Quote(label))
|
2020-04-25 16:17:59 +02:00
|
|
|
return nil
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return place
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Find returns db record of place
|
2020-04-30 20:07:03 +02:00
|
|
|
func (m *Place) Find() error {
|
|
|
|
if err := Db().First(m, "id = ?", m.ID).Error; err != nil {
|
2019-12-28 20:24:20 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
// 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 {
|
2020-05-03 18:00:50 +02:00
|
|
|
log.Debugf("place: %s for token %s or label %s", err.Error(), m.ID, txt.Quote(m.LocLabel))
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-04-25 16:17:59 +02:00
|
|
|
// Unknown returns true if this is an unknown place
|
|
|
|
func (m Place) Unknown() bool {
|
|
|
|
return m.ID == UnknownPlace.ID
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Label returns place label
|
2020-04-16 20:57:00 +02:00
|
|
|
func (m Place) Label() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.LocLabel
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// City returns place City
|
2020-04-16 20:57:00 +02:00
|
|
|
func (m Place) City() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.LocCity
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// State returns place State
|
2020-04-16 20:57:00 +02:00
|
|
|
func (m Place) State() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.LocState
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CountryCode returns place CountryCode
|
2020-04-16 20:57:00 +02:00
|
|
|
func (m Place) CountryCode() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.LocCountry
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CountryName returns place CountryName
|
2020-04-16 20:57:00 +02:00
|
|
|
func (m Place) CountryName() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return maps.CountryNames[m.LocCountry]
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Notes returns place Notes
|
2020-04-16 20:57:00 +02:00
|
|
|
func (m Place) Notes() string {
|
2019-12-28 20:24:20 +01:00
|
|
|
return m.LocNotes
|
|
|
|
}
|