2019-12-11 15:55:18 +00:00
|
|
|
package entity
|
2018-09-19 07:20:57 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-19 09:16:18 +00:00
|
|
|
"github.com/gosimple/slug"
|
2018-09-19 07:20:57 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2019-12-28 19:24:20 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2020-01-08 18:51:21 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
2018-09-19 07:20:57 +00:00
|
|
|
)
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// altCountryNames defines mapping between different names for the same countriy
|
2019-12-16 19:22:46 +00:00
|
|
|
var altCountryNames = map[string]string{
|
|
|
|
"United States of America": "USA",
|
|
|
|
"United States": "USA",
|
|
|
|
"": "Unknown",
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// Country represents a country location, used for labeling photos.
|
2018-09-19 07:20:57 +00:00
|
|
|
type Country struct {
|
2018-09-19 09:16:18 +00:00
|
|
|
ID string `gorm:"primary_key"`
|
2020-01-06 01:14:17 +00:00
|
|
|
CountrySlug string `gorm:"type:varbinary(128);unique_index;"`
|
2018-09-19 07:20:57 +00:00
|
|
|
CountryName string
|
|
|
|
CountryDescription string `gorm:"type:text;"`
|
|
|
|
CountryNotes string `gorm:"type:text;"`
|
|
|
|
CountryPhoto *Photo
|
|
|
|
CountryPhotoID uint
|
2019-12-11 03:12:54 +00:00
|
|
|
New bool `gorm:"-"`
|
2018-09-19 07:20:57 +00:00
|
|
|
}
|
2018-09-19 09:16:18 +00:00
|
|
|
|
2020-04-25 12:22:47 +00:00
|
|
|
// UnknownCountry is defined here to use it as a default
|
|
|
|
var UnknownCountry = Country{
|
|
|
|
ID: "zz",
|
|
|
|
CountryName: maps.CountryNames["zz"],
|
|
|
|
CountrySlug: "zz",
|
|
|
|
}
|
2019-12-28 19:24:20 +00:00
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// CreateUnknownCountry is used to initialize the database with the default country
|
2019-12-28 19:24:20 +00:00
|
|
|
func CreateUnknownCountry(db *gorm.DB) {
|
|
|
|
UnknownCountry.FirstOrCreate(db)
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// NewCountry creates a new country, with default country code if not provided
|
2018-09-19 09:16:18 +00:00
|
|
|
func NewCountry(countryCode string, countryName string) *Country {
|
|
|
|
if countryCode == "" {
|
2020-04-25 12:22:47 +00:00
|
|
|
return &UnknownCountry
|
2018-09-19 09:16:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:22:46 +00:00
|
|
|
if altName, ok := altCountryNames[countryName]; ok {
|
|
|
|
countryName = altName
|
2018-09-19 09:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
countrySlug := slug.MakeLang(countryName, "en")
|
|
|
|
|
|
|
|
result := &Country{
|
|
|
|
ID: countryCode,
|
|
|
|
CountryName: countryName,
|
|
|
|
CountrySlug: countrySlug,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// FirstOrCreate checks wether the country exist already in the database (using countryCode)
|
2019-06-04 16:26:35 +00:00
|
|
|
func (m *Country) FirstOrCreate(db *gorm.DB) *Country {
|
2020-01-08 18:51:21 +00:00
|
|
|
mutex.Db.Lock()
|
|
|
|
defer mutex.Db.Unlock()
|
|
|
|
|
2019-12-19 08:37:10 +00:00
|
|
|
if err := db.FirstOrCreate(m, "id = ?", m.ID).Error; err != nil {
|
|
|
|
log.Errorf("country: %s", err)
|
|
|
|
}
|
2018-09-19 09:16:18 +00:00
|
|
|
|
2019-06-04 16:26:35 +00:00
|
|
|
return m
|
2018-09-19 09:16:18 +00:00
|
|
|
}
|
2019-12-11 03:12:54 +00:00
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// AfterCreate sets the New column used for database callback
|
2019-12-11 03:12:54 +00:00
|
|
|
func (m *Country) AfterCreate(scope *gorm.Scope) error {
|
|
|
|
return scope.SetColumn("New", true)
|
|
|
|
}
|
2019-12-28 19:24:20 +00:00
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// Code returns country code
|
2019-12-28 19:24:20 +00:00
|
|
|
func (m *Country) Code() string {
|
|
|
|
return m.ID
|
|
|
|
}
|
|
|
|
|
2020-02-21 00:14:45 +00:00
|
|
|
// Name returns country name
|
2019-12-28 19:24:20 +00:00
|
|
|
func (m *Country) Name() string {
|
|
|
|
return m.CountryName
|
|
|
|
}
|