aboutsummaryrefslogtreecommitdiff
path: root/pkg/model
diff options
context:
space:
mode:
authorleshe4ka46 <alex9102naid1@ya.ru>2025-10-11 16:12:19 +0300
committerleshe4ka46 <alex9102naid1@ya.ru>2025-10-11 16:12:19 +0300
commit2d35000b41e5ccb63a42eee24c4de063efd9071e (patch)
tree63f2fd168942bd336113d29c8991e79382038f0a /pkg/model
parent31b2dce966be10902dd7f75a9e41dd3fd40e6680 (diff)
json tests
Diffstat (limited to 'pkg/model')
-rw-r--r--pkg/model/user.go76
1 files changed, 59 insertions, 17 deletions
diff --git a/pkg/model/user.go b/pkg/model/user.go
index af42e3c..85117f6 100644
--- a/pkg/model/user.go
+++ b/pkg/model/user.go
@@ -1,26 +1,63 @@
package model
-import "time"
+import (
+ "encoding/json"
+ "strings"
+ "time"
+)
+
+const sentinelYear = 1000
+
+func SentinelBirthday() time.Time {
+ return time.Date(sentinelYear, 1, 1, 0, 0, 0, 0, time.UTC)
+}
-type Sex bool
+type Sex uint8
const (
- SexMale Sex = false
- SexFemale Sex = true
+ 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
+}
+
type User struct {
- ID uint64 `gorm:"primaryKey"`
+ ID uint64 `gorm:"primaryKey"`
+
+ Nick string `gorm:"not null;uniqueIndex:uniq_user_nick"`
+
Name string
Surname string
Fathersname string
- Age uint8
-
- Sex Sex
+ Sex Sex `gorm:"type:smallint;check:sex IN (0,1,2)"`
Birthday time.Time
- Cards []Card `gorm:"foreignKey:UserID"`
+ Cards []Card `gorm:"foreignKey:UserID"`
// just for compatibility
Flights []Flight `gorm:"many2many:user_flights;joinForeignKey:UserID;joinReferences:FlightID"`
@@ -31,10 +68,10 @@ func (User) TableName() string { return "users" }
type Card struct {
ID uint64 `gorm:"primaryKey"`
- Prefix string
- Number uint64
+ Prefix string `gorm:"not null;uniqueIndex:uniq_card_identity"`
+ Number uint64 `gorm:"not null;uniqueIndex:uniq_card_identity"`
- Bonusprogramm string
+ 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"`
@@ -48,11 +85,16 @@ func (Card) TableName() string { return "cards" }
type Flight struct {
ID uint64 `gorm:"primaryKey"`
- Number string
- From string
- To string
- Departure time.Time
- Arrival time.Time
+ 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"`
+
+ 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"`
Users []User `gorm:"many2many:user_flights;joinForeignKey:FlightID;joinReferences:UserID"`
Cards []Card `gorm:"many2many:card_flights;joinForeignKey:FlightID;joinReferences:CardID"`