diff options
Diffstat (limited to 'pkg/model/user.go')
| -rw-r--r-- | pkg/model/user.go | 66 |
1 files changed, 19 insertions, 47 deletions
diff --git a/pkg/model/user.go b/pkg/model/user.go index bfa3694..213934a 100644 --- a/pkg/model/user.go +++ b/pkg/model/user.go @@ -1,48 +1,11 @@ package model import ( - "encoding/json" - "strings" "time" ) -const sentinelYear = 1 - func SentinelBirthday() time.Time { - return time.Date(sentinelYear, 1, 1, 0, 0, 0, 0, time.UTC) -} - -type Sex uint8 - -const ( - SexUnknown Sex = 0 - SexMale Sex = 1 - SexFemale Sex = 2 -) - -func (s *Sex) UnmarshalJSON(b []byte) error { - var raw string - if err := json.Unmarshal(b, &raw); err == nil { - switch strings.ToLower(strings.TrimSpace(raw)) { - case "male": - *s = SexMale - return nil - case "female": - *s = SexFemale - return nil - case "", "unknown", "null": - *s = SexUnknown - return nil - } - } - // also accept numbers in JSON - var n int - if err := json.Unmarshal(b, &n); err == nil { - *s = Sex(n) - return nil - } - *s = SexUnknown - return nil + return time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC) } type User struct { @@ -58,9 +21,11 @@ type User struct { Birthday time.Time Cards []Card `gorm:"foreignKey:UserID"` + CardsIDs []uint64 `gorm:"-"` // just for compatibility Flights []Flight `gorm:"many2many:user_flights;joinForeignKey:UserID;joinReferences:FlightID"` + FlightsIDs []uint64 `gorm:"-"` } func (User) TableName() string { return "users" } @@ -73,8 +38,8 @@ type Card struct { Bonusprogramm string `gorm:"not null;uniqueIndex:uniq_card_identity"` - // User has multiple cards -> each card has registered flights to it Flights []Flight `gorm:"many2many:card_flights;joinForeignKey:CardID;joinReferences:FlightID"` + FlightsIDs []uint64 `gorm:"-"` UserID uint64 User User `gorm:"foreignKey:UserID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"` @@ -82,22 +47,29 @@ type Card struct { func (Card) TableName() string { return "cards" } +type LatLong struct { + Lat, Long float64 +} + type Flight struct { ID uint64 `gorm:"primaryKey"` - Number string `gorm:"not null;uniqueIndex:uniq_flight_identity"` - From string `gorm:"not null;uniqueIndex:uniq_flight_identity"` - FromCity string `gorm:"not null;uniqueIndex:uniq_flight_identity"` - FromCountry string `gorm:"not null;uniqueIndex:uniq_flight_identity"` + Number string `gorm:"not null;uniqueIndex:uniq_flight_identity"` + From string `gorm:"not null;uniqueIndex:uniq_flight_identity"` + FromCoords LatLong `gorm:"-"` + + To string `gorm:"not null;uniqueIndex:uniq_flight_identity"` + ToCoords LatLong `gorm:"-"` - To string `gorm:"not null;uniqueIndex:uniq_flight_identity"` - ToCity string `gorm:"not null;uniqueIndex:uniq_flight_identity"` - ToCountry string `gorm:"not null;uniqueIndex:uniq_flight_identity"` + Date time.Time `gorm:"not null;uniqueIndex:uniq_flight_identity"` + HasTime bool - Date time.Time `gorm:"not null;uniqueIndex:uniq_flight_identity"` + Code string `gorm:"-"` Users []User `gorm:"many2many:user_flights;joinForeignKey:FlightID;joinReferences:UserID"` Cards []Card `gorm:"many2many:card_flights;joinForeignKey:FlightID;joinReferences:CardID"` + UserID uint64 `gorm:"-"` + CardID uint64 `gorm:"-"` } func (Flight) TableName() string { return "flights" } |
