Skip to content

Commit 5bd792b

Browse files
fix: event_time scanner error
1 parent 8465366 commit 5bd792b

File tree

2 files changed

+51
-26
lines changed

2 files changed

+51
-26
lines changed

internal/database/migrations/000003_create_events_table.up.sql

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ CREATE TABLE events (
44
description TEXT,
55
club_id INTEGER NOT NULL REFERENCES clubs(id) ON DELETE CASCADE,
66
event_type VARCHAR(20) NOT NULL CHECK (event_type IN ('in_person', 'online')),
7-
start_time TIMESTAMP NOT NULL,
8-
end_time TIMESTAMP NOT NULL,
7+
event_date DATE NOT NULL,
8+
event_time TIME NOT NULL,
99
location VARCHAR(255),
1010
online_link TEXT,
1111
max_attendees INTEGER,
12+
is_public BOOLEAN DEFAULT FALSE,
1213
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1314
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
1415
deleted_at TIMESTAMP
1516
);
1617

1718
CREATE INDEX idx_events_club_id ON events(club_id);
18-
CREATE INDEX idx_events_start_time ON events(start_time);
19+
CREATE INDEX idx_events_event_date ON events(event_date);
20+
CREATE INDEX idx_events_event_time ON events(event_time);
1921
CREATE INDEX idx_events_deleted_at ON events(deleted_at);
2022

2123
-- Create event_rsvps table

internal/models/event.go

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"database/sql/driver"
45
"encoding/json"
56
"fmt"
67
"strings"
@@ -9,33 +10,55 @@ import (
910

1011
type DateYMD struct{ time.Time } // accepts "2006-01-02"
1112
type 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

1336
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
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

2750
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
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

4164
type 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

Comments
 (0)