2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2018-09-19 09:20:57 +02:00
|
|
|
|
|
|
|
import (
|
2018-09-19 11:16:18 +02:00
|
|
|
"github.com/gosimple/slug"
|
2018-09-19 09:20:57 +02:00
|
|
|
"github.com/jinzhu/gorm"
|
2019-12-28 20:24:20 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/maps"
|
2018-09-19 09:20:57 +02:00
|
|
|
)
|
|
|
|
|
2019-12-16 20:22:46 +01:00
|
|
|
var altCountryNames = map[string]string{
|
|
|
|
"United States of America": "USA",
|
|
|
|
"United States": "USA",
|
|
|
|
"": "Unknown",
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:20:57 +02:00
|
|
|
type Country struct {
|
2018-09-19 11:16:18 +02:00
|
|
|
ID string `gorm:"primary_key"`
|
|
|
|
CountrySlug string
|
2018-09-19 09:20:57 +02:00
|
|
|
CountryName string
|
|
|
|
CountryDescription string `gorm:"type:text;"`
|
|
|
|
CountryNotes string `gorm:"type:text;"`
|
|
|
|
CountryPhoto *Photo
|
|
|
|
CountryPhotoID uint
|
2019-12-11 04:12:54 +01:00
|
|
|
New bool `gorm:"-"`
|
2018-09-19 09:20:57 +02:00
|
|
|
}
|
2018-09-19 11:16:18 +02:00
|
|
|
|
2019-12-28 20:24:20 +01:00
|
|
|
var UnknownCountry = NewCountry("zz", maps.CountryNames["zz"])
|
|
|
|
|
|
|
|
func CreateUnknownCountry(db *gorm.DB) {
|
|
|
|
UnknownCountry.FirstOrCreate(db)
|
|
|
|
}
|
|
|
|
|
2018-11-06 19:02:03 +01:00
|
|
|
// Create a new country
|
2018-09-19 11:16:18 +02:00
|
|
|
func NewCountry(countryCode string, countryName string) *Country {
|
|
|
|
if countryCode == "" {
|
|
|
|
countryCode = "zz"
|
|
|
|
}
|
|
|
|
|
2019-12-16 20:22:46 +01:00
|
|
|
if altName, ok := altCountryNames[countryName]; ok {
|
|
|
|
countryName = altName
|
2018-09-19 11:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
countrySlug := slug.MakeLang(countryName, "en")
|
|
|
|
|
|
|
|
result := &Country{
|
|
|
|
ID: countryCode,
|
|
|
|
CountryName: countryName,
|
|
|
|
CountrySlug: countrySlug,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
func (m *Country) FirstOrCreate(db *gorm.DB) *Country {
|
2019-12-19 09:37:10 +01:00
|
|
|
if err := db.FirstOrCreate(m, "id = ?", m.ID).Error; err != nil {
|
|
|
|
log.Errorf("country: %s", err)
|
|
|
|
}
|
2018-09-19 11:16:18 +02:00
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
return m
|
2018-09-19 11:16:18 +02:00
|
|
|
}
|
2019-12-11 04:12:54 +01:00
|
|
|
|
|
|
|
func (m *Country) AfterCreate(scope *gorm.Scope) error {
|
|
|
|
return scope.SetColumn("New", true)
|
|
|
|
}
|
2019-12-28 20:24:20 +01:00
|
|
|
|
|
|
|
func (m *Country) Code() string {
|
|
|
|
return m.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Country) Name() string {
|
|
|
|
return m.CountryName
|
|
|
|
}
|