/
place.go
160 lines (130 loc) Β· 4.22 KB
/
place.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package entity
import (
"strings"
"sync"
"time"
"github.com/photoprism/photoprism/internal/maps"
"github.com/photoprism/photoprism/pkg/clean"
)
var placeMutex = sync.Mutex{}
// Place represents a distinct region identified by city, district, state, and country.
type Place struct {
ID string `gorm:"type:VARBINARY(42);primary_key;auto_increment:false;" json:"PlaceID" yaml:"PlaceID"`
PlaceLabel string `gorm:"type:VARCHAR(400);" json:"Label" yaml:"Label"`
PlaceDistrict string `gorm:"type:VARCHAR(100);index;" json:"District" yaml:"District,omitempty"`
PlaceCity string `gorm:"type:VARCHAR(100);index;" json:"City" yaml:"City,omitempty"`
PlaceState string `gorm:"type:VARCHAR(100);index;" json:"State" yaml:"State,omitempty"`
PlaceCountry string `gorm:"type:VARBINARY(2);" json:"Country" yaml:"Country,omitempty"`
PlaceKeywords string `gorm:"type:VARCHAR(300);" json:"Keywords" yaml:"Keywords,omitempty"`
PlaceFavorite bool `json:"Favorite" yaml:"Favorite,omitempty"`
PhotoCount int `gorm:"default:1" json:"PhotoCount" yaml:"-"`
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
}
// TableName returns the entity table name.
func (Place) TableName() string {
return "places"
}
// UnknownPlace is PhotoPrism's default place.
var UnknownPlace = Place{
ID: UnknownID,
PlaceLabel: "Unknown",
PlaceDistrict: "Unknown",
PlaceCity: "Unknown",
PlaceState: "Unknown",
PlaceCountry: UnknownID,
PlaceKeywords: "",
PlaceFavorite: false,
PhotoCount: -1,
}
// CreateUnknownPlace creates the default place if not exists.
func CreateUnknownPlace() {
UnknownPlace = *FirstOrCreatePlace(&UnknownPlace)
}
// FindPlace finds a matching place or returns nil.
func FindPlace(id string) *Place {
m := Place{}
if Db().First(&m, "id = ?", id).Error != nil {
log.Debugf("place: %s not found", clean.Log(id))
return nil
}
return &m
}
// Create inserts a new row to the database.
func (m *Place) Create() error {
placeMutex.Lock()
defer placeMutex.Unlock()
return Db().Create(m).Error
}
// Save updates the record in the database or inserts a new record if it does not already exist.
func (m *Place) Save() error {
placeMutex.Lock()
defer placeMutex.Unlock()
return Db().Save(m).Error
}
// Delete removes the entity from the index.
func (m *Place) Delete() (err error) {
return UnscopedDb().Delete(m).Error
}
// FirstOrCreatePlace fetches an existing row, inserts a new row or nil in case of errors.
func FirstOrCreatePlace(m *Place) *Place {
if m.ID == "" {
log.Errorf("place: id must not be empty (find or create)")
return nil
}
if m.PlaceLabel == "" {
log.Errorf("place: label must not be empty (find or create place %s)", m.ID)
return nil
}
result := Place{}
if findErr := Db().Where("id = ?", m.ID).First(&result).Error; findErr == nil {
return &result
} else if createErr := m.Create(); createErr == nil {
return m
} else if err := Db().Where("id = ?", m.ID).First(&result).Error; err == nil {
return &result
} else {
log.Errorf("place: %s (create %s)", createErr, m.ID)
}
return nil
}
// Unknown returns true if this is an unknown place
func (m Place) Unknown() bool {
return m.ID == "" || m.ID == UnknownPlace.ID
}
// Label returns place label
func (m Place) Label() string {
return m.PlaceLabel
}
// District returns the place district name if any.
func (m Place) District() string {
return m.PlaceDistrict
}
// City returns place city if any.
func (m Place) City() string {
return m.PlaceCity
}
// LongCity checks if the city name is more than 16 char.
func (m Place) LongCity() bool {
return len(m.PlaceCity) > 16
}
// NoCity checks if the location has no city
func (m Place) NoCity() bool {
return m.PlaceCity == ""
}
// CityContains checks if the location city contains the text string
func (m Place) CityContains(text string) bool {
return strings.Contains(text, m.PlaceCity)
}
// State returns place State
func (m Place) State() string {
return m.PlaceState
}
// CountryCode returns place CountryCode
func (m Place) CountryCode() string {
return m.PlaceCountry
}
// CountryName returns place CountryName
func (m Place) CountryName() string {
return maps.CountryNames[m.PlaceCountry]
}