2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-09-19 09:20:57 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-05-26 11:00:39 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2019-12-28 20:24:20 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2021-09-23 23:46:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2018-09-19 09:20:57 +02:00
|
|
|
)
|
|
|
|
|
2020-10-01 12:15:43 +02:00
|
|
|
// altCountryNames defines mapping between different names for the same country
|
2019-12-16 20:22:46 +01:00
|
|
|
var altCountryNames = map[string]string{
|
|
|
|
"United States of America": "USA",
|
|
|
|
"United States": "USA",
|
|
|
|
"": "Unknown",
|
|
|
|
}
|
|
|
|
|
2021-09-02 14:45:26 +02:00
|
|
|
// Countries represents a list of countries.
|
|
|
|
type Countries []Country
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Country represents a country location, used for labeling photos.
|
2018-09-19 09:20:57 +02:00
|
|
|
type Country struct {
|
2020-09-13 17:51:43 +02:00
|
|
|
ID string `gorm:"type:VARBINARY(2);primary_key" json:"ID" yaml:"ID"`
|
2021-09-23 23:46:17 +02:00
|
|
|
CountrySlug string `gorm:"type:VARBINARY(160);unique_index;" json:"Slug" yaml:"-"`
|
|
|
|
CountryName string `gorm:"type:VARCHAR(160);" json:"Name" yaml:"Name,omitempty"`
|
2022-03-30 20:36:25 +02:00
|
|
|
CountryDescription string `gorm:"type:VARCHAR(2048);" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
CountryNotes string `gorm:"type:VARCHAR(1024);" json:"Notes,omitempty" yaml:"Notes,omitempty"`
|
2020-05-23 20:58:58 +02:00
|
|
|
CountryPhoto *Photo `json:"-" yaml:"-"`
|
|
|
|
CountryPhotoID uint `json:"-" yaml:"-"`
|
|
|
|
New bool `gorm:"-" json:"-" yaml:"-"`
|
2018-09-19 09:20:57 +02:00
|
|
|
}
|
2018-09-19 11:16:18 +02:00
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (Country) TableName() string {
|
|
|
|
return "countries"
|
|
|
|
}
|
|
|
|
|
2020-04-25 14:22:47 +02:00
|
|
|
// UnknownCountry is defined here to use it as a default
|
|
|
|
var UnknownCountry = Country{
|
2021-08-19 21:12:38 +02:00
|
|
|
ID: UnknownID,
|
|
|
|
CountryName: maps.CountryNames[UnknownID],
|
|
|
|
CountrySlug: UnknownID,
|
2020-04-25 14:22:47 +02:00
|
|
|
}
|
2019-12-28 20:24:20 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// CreateUnknownCountry is used to initialize the database with the default country
|
2020-04-30 20:07:03 +02:00
|
|
|
func CreateUnknownCountry() {
|
2021-11-25 12:48:07 +01:00
|
|
|
UnknownCountry = *FirstOrCreateCountry(&UnknownCountry)
|
2019-12-28 20:24:20 +01:00
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// NewCountry creates a new country, with default country code if not provided
|
2018-09-19 11:16:18 +02:00
|
|
|
func NewCountry(countryCode string, countryName string) *Country {
|
|
|
|
if countryCode == "" {
|
2020-04-25 14:22:47 +02:00
|
|
|
return &UnknownCountry
|
2018-09-19 11:16:18 +02:00
|
|
|
}
|
|
|
|
|
2019-12-16 20:22:46 +01:00
|
|
|
if altName, ok := altCountryNames[countryName]; ok {
|
|
|
|
countryName = altName
|
2018-09-19 11:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
result := &Country{
|
|
|
|
ID: countryCode,
|
2021-09-23 23:46:17 +02:00
|
|
|
CountryName: txt.Clip(countryName, txt.ClipName),
|
|
|
|
CountrySlug: txt.Slug(countryName),
|
2018-09-19 11:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
// Create inserts a new row to the database.
|
|
|
|
func (m *Country) Create() error {
|
|
|
|
return Db().Create(m).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstOrCreateCountry returns the existing row, inserts a new row or nil in case of errors.
|
|
|
|
func FirstOrCreateCountry(m *Country) *Country {
|
2021-02-06 16:30:30 +01:00
|
|
|
if cacheData, ok := countryCache.Get(m.ID); ok {
|
2021-11-27 18:41:10 +01:00
|
|
|
log.Tracef("country: cache hit for %s", m.ID)
|
2021-02-06 16:30:30 +01:00
|
|
|
|
|
|
|
return cacheData.(*Country)
|
|
|
|
}
|
|
|
|
|
2020-05-26 11:00:39 +02:00
|
|
|
result := Country{}
|
|
|
|
|
2020-07-07 16:56:02 +02:00
|
|
|
if findErr := Db().Where("id = ?", m.ID).First(&result).Error; findErr == nil {
|
2021-02-06 16:30:30 +01:00
|
|
|
countryCache.SetDefault(m.ID, &result)
|
2020-05-26 11:00:39 +02:00
|
|
|
return &result
|
2020-07-07 16:56:02 +02:00
|
|
|
} else if createErr := m.Create(); createErr == nil {
|
|
|
|
if !m.Unknown() {
|
|
|
|
event.EntitiesCreated("countries", []*Country{m})
|
2020-05-26 11:00:39 +02:00
|
|
|
|
2020-07-07 16:56:02 +02:00
|
|
|
event.Publish("count.countries", event.Data{
|
|
|
|
"count": 1,
|
|
|
|
})
|
|
|
|
}
|
2021-02-06 16:30:30 +01:00
|
|
|
countryCache.SetDefault(m.ID, m)
|
2020-07-07 16:56:02 +02:00
|
|
|
return m
|
|
|
|
} else if err := Db().Where("id = ?", m.ID).First(&result).Error; err == nil {
|
2021-02-06 16:30:30 +01:00
|
|
|
countryCache.SetDefault(m.ID, &result)
|
2020-07-07 16:56:02 +02:00
|
|
|
return &result
|
|
|
|
} else {
|
2020-12-14 13:31:18 +01:00
|
|
|
log.Errorf("country: %s (find or create %s)", createErr, m.ID)
|
2019-12-19 09:37:10 +01:00
|
|
|
}
|
2018-09-19 11:16:18 +02:00
|
|
|
|
2021-02-06 16:30:30 +01:00
|
|
|
return &UnknownCountry
|
2018-09-19 11:16:18 +02:00
|
|
|
}
|
2019-12-11 04:12:54 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// AfterCreate sets the New column used for database callback
|
2019-12-11 04:12:54 +01:00
|
|
|
func (m *Country) AfterCreate(scope *gorm.Scope) error {
|
2020-05-19 12:30:26 +02:00
|
|
|
m.New = true
|
|
|
|
return nil
|
2019-12-11 04:12:54 +01:00
|
|
|
}
|
2019-12-28 20:24:20 +01:00
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Code returns country code
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Country) Code() string {
|
|
|
|
return m.ID
|
|
|
|
}
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// Name returns country name
|
2019-12-28 20:24:20 +01:00
|
|
|
func (m *Country) Name() string {
|
|
|
|
return m.CountryName
|
|
|
|
}
|
2020-05-29 18:04:30 +02:00
|
|
|
|
|
|
|
// Unknown returns true if the country is not a known country.
|
|
|
|
func (m *Country) Unknown() bool {
|
|
|
|
return m.ID == "" || m.ID == UnknownCountry.ID
|
|
|
|
}
|