11package models
22
3- import "time"
3+ import (
4+ "encoding/json"
5+ "fmt"
6+ "strings"
7+ "time"
8+ )
9+
10+ type DateYMD struct { time.Time } // accepts "2006-01-02"
11+ type TimeHM struct { time.Time } // accepts "15:04"
12+
13+ func (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
25+ }
26+
27+ func (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
39+ }
440
541type Event struct {
642 ID uint `json:"id" gorm:"primaryKey"`
@@ -48,8 +84,8 @@ type CreateEventRequest struct {
4884 Title string `json:"title" binding:"required"`
4985 Description string `json:"description"`
5086 EventType EventType `json:"event_type" binding:"required,oneof=in_person online"`
51- EventDate time. Time `json:"event_date" binding:"required" time_format:"2006-01-02 "`
52- EventTime time. Time `json:"event_time" binding:"required" time_format:"15:04 "`
87+ EventDate DateYMD `json:"event_date" binding:"required"`
88+ EventTime TimeHM `json:"event_time" binding:"required"`
5389 Location string `json:"location,omitempty"`
5490 OnlineLink string `json:"online_link,omitempty"`
5591 MaxAttendees * int `json:"max_attendees,omitempty"`
@@ -60,8 +96,8 @@ type UpdateEventRequest struct {
6096 Title * string `json:"title,omitempty"`
6197 Description * string `json:"description,omitempty"`
6298 EventType * EventType `json:"event_type,omitempty" binding:"omitempty,oneof=in_person online"`
63- EventDate * time. Time `json:"event_date,omitempty" binding:"omitempty" time_format:"2006-01-02 "`
64- EventTime * time. Time `json:"event_time,omitempty" binding:"omitempty" time_format:"15:04 "`
99+ EventDate * DateYMD `json:"event_date,omitempty" binding:"omitempty"`
100+ EventTime * TimeHM `json:"event_time,omitempty" binding:"omitempty"`
65101 Location * string `json:"location,omitempty"`
66102 OnlineLink * string `json:"online_link,omitempty"`
67103 MaxAttendees * int `json:"max_attendees,omitempty"`
0 commit comments