11package models
22
33import (
4+ "database/sql/driver"
45 "encoding/json"
56 "fmt"
67 "strings"
@@ -9,33 +10,55 @@ import (
910
1011type DateYMD struct { time.Time } // accepts "2006-01-02"
1112type TimeHM struct { time.Time } // accepts "15:04"
13+ type DBTime struct { time.Time }
14+
15+ func (t * DBTime ) Scan (value interface {}) error {
16+ switch v := value .(type ) {
17+ case time.Time :
18+ t .Time = v
19+ return nil
20+ case string :
21+ parsed , err := time .Parse ("15:04:05" , v )
22+ if err != nil {
23+ return err
24+ }
25+ t .Time = parsed
26+ return nil
27+ default :
28+ return fmt .Errorf ("cannot scan type %T into DBTime" , value )
29+ }
30+ }
31+
32+ func (t DBTime ) Value () (driver.Value , error ) {
33+ return t .Time .Format ("15:04:05" ), nil
34+ }
1235
1336func (d * DateYMD ) UnmarshalJSON (b []byte ) error {
14- var s string
15- if err := json .Unmarshal (b , & s ); err != nil {
16- return fmt .Errorf ("date must be a string YYYY-MM-DD: %w" , err )
17- }
18- s = strings .TrimSpace (s )
19- tt , err := time .ParseInLocation ("2006-01-02" , s , time .Local ) // or a fixed TZ
20- if err != nil {
21- return fmt .Errorf ("date must be YYYY-MM-DD: %w" , err )
22- }
23- d .Time = tt
24- return nil
37+ var s string
38+ if err := json .Unmarshal (b , & s ); err != nil {
39+ return fmt .Errorf ("date must be a string YYYY-MM-DD: %w" , err )
40+ }
41+ s = strings .TrimSpace (s )
42+ tt , err := time .ParseInLocation ("2006-01-02" , s , time .Local ) // or a fixed TZ
43+ if err != nil {
44+ return fmt .Errorf ("date must be YYYY-MM-DD: %w" , err )
45+ }
46+ d .Time = tt
47+ return nil
2548}
2649
2750func (t * TimeHM ) UnmarshalJSON (b []byte ) error {
28- var s string
29- if err := json .Unmarshal (b , & s ); err != nil {
30- return fmt .Errorf ("time must be a string HH:MM (24h): %w" , err )
31- }
32- s = strings .TrimSpace (s )
33- tt , err := time .ParseInLocation ("15:04" , s , time .Local ) // or a fixed TZ
34- if err != nil {
35- return fmt .Errorf ("time must be HH:MM (24h): %w" , err )
36- }
37- t .Time = tt
38- return nil
51+ var s string
52+ if err := json .Unmarshal (b , & s ); err != nil {
53+ return fmt .Errorf ("time must be a string HH:MM (24h): %w" , err )
54+ }
55+ s = strings .TrimSpace (s )
56+ tt , err := time .ParseInLocation ("15:04" , s , time .Local ) // or a fixed TZ
57+ if err != nil {
58+ return fmt .Errorf ("time must be HH:MM (24h): %w" , err )
59+ }
60+ t .Time = tt
61+ return nil
3962}
4063
4164type Event struct {
@@ -45,7 +68,7 @@ type Event struct {
4568 ClubID uint `json:"club_id" gorm:"not null"`
4669 EventType EventType `json:"event_type" gorm:"type:varchar(20)"`
4770 EventDate time.Time `json:"event_date" gorm:"type:date;not null"`
48- EventTime time. Time `json:"event_time" gorm:"type:time;not null"`
71+ EventTime DBTime `json:"event_time" gorm:"type:time;not null"`
4972 Location string `json:"location,omitempty"`
5073 OnlineLink string `json:"online_link,omitempty"`
5174 MaxAttendees * int `json:"max_attendees,omitempty"`
0 commit comments