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
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// altCountryNames defines mapping between different names for the same countriy
|
2019-12-16 20:22:46 +01:00
|
|
|
var altCountryNames = map[string]string{
|
|
|
|
"United States of America": "USA",
|
|
|
|
"United States": "USA",
|
|
|
|
"": "Unknown",
|
|
|
|
}
|
|
|
|
|
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-05-23 20:58:58 +02:00
|
|
|
ID string `gorm:"type:varbinary(2);primary_key" json:"ID" yaml:"ID"`
|
|
|
|
CountrySlug string `gorm:"type:varbinary(255);unique_index;" json:"Slug" yaml:"-"`
|
|
|
|
CountryName string `json:"Name" yaml:"Name,omitempty"`
|
|
|
|
CountryDescription string `gorm:"type:text;" json:"Description,omitempty" yaml:"Description,omitempty"`
|
|
|
|
CountryNotes string `gorm:"type:text;" json:"Notes,omitempty" yaml:"Notes,omitempty"`
|
|
|
|
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
|
|
|
|
2020-04-25 14:22:47 +02: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 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() {
|
|
|
|
UnknownCountry.FirstOrCreate()
|
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
|
|
|
}
|
|
|
|
|
|
|
|
countrySlug := slug.MakeLang(countryName, "en")
|
|
|
|
|
|
|
|
result := &Country{
|
|
|
|
ID: countryCode,
|
|
|
|
CountryName: countryName,
|
|
|
|
CountrySlug: countrySlug,
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
// FirstOrCreate checks if the country exist already in the database (using countryCode)
|
|
|
|
func (m *Country) FirstOrCreate() *Country {
|
|
|
|
if err := Db().FirstOrCreate(m, "id = ?", m.ID).Error; err != nil {
|
2019-12-19 09:37:10 +01:00
|
|
|
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
|
|
|
|
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
|
|
|
|
}
|