Skip to content

Commit 446b085

Browse files
update event model
1 parent 7a57aee commit 446b085

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

internal/models/event.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ type Event struct {
88
Description string `json:"description"`
99
ClubID uint `json:"club_id" gorm:"not null"`
1010
EventType EventType `json:"event_type" gorm:"type:varchar(20)"`
11-
EventDate time.Time `json:"event_date" gorm:"not null"`
12-
EventTime time.Time `json:"event_time" gorm:"not null"`
11+
EventDate time.Time `json:"event_date" gorm:"type:date;not null"`
12+
EventTime time.Time `json:"event_time" gorm:"type:time;not null"`
1313
Location string `json:"location,omitempty"`
1414
OnlineLink string `json:"online_link,omitempty"`
1515
MaxAttendees *int `json:"max_attendees,omitempty"`
16-
IsPublic bool `json:"is_public" gorm:"default:false"`
16+
IsPublic bool `json:"is_public" gorm:"default:false"`
1717
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
1818
Club Club `json:"club" gorm:"foreignKey:ClubID"`
1919
RSVPs []EventRSVP `json:"rsvps,omitempty"`
@@ -48,24 +48,24 @@ type CreateEventRequest struct {
4848
Title string `json:"title" binding:"required"`
4949
Description string `json:"description"`
5050
EventType EventType `json:"event_type" binding:"required,oneof=in_person online"`
51-
EventDate time.Time `json:"event_date" binding:"required"`
52-
EventTime time.Time `json:"event_time" binding:"required"`
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"`
5353
Location string `json:"location,omitempty"`
5454
OnlineLink string `json:"online_link,omitempty"`
5555
MaxAttendees *int `json:"max_attendees,omitempty"`
56-
IsPublic bool `json:"is_public" gorm:"default:false"`
56+
IsPublic bool `json:"is_public" gorm:"default:false"`
5757
}
5858

5959
type UpdateEventRequest struct {
6060
Title *string `json:"title,omitempty"`
6161
Description *string `json:"description,omitempty"`
6262
EventType *EventType `json:"event_type,omitempty" binding:"omitempty,oneof=in_person online"`
63-
EventDate *time.Time `json:"event_date,omitempty" binding:"omitempty"`
64-
EventTime *time.Time `json:"event_time,omitempty" binding:"omitempty"`
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"`
6565
Location *string `json:"location,omitempty"`
6666
OnlineLink *string `json:"online_link,omitempty"`
6767
MaxAttendees *int `json:"max_attendees,omitempty"`
68-
IsPublic *bool `json:"is_public,omitempty"`
68+
IsPublic *bool `json:"is_public,omitempty"`
6969
}
7070

7171
type RSVPRequest struct {
@@ -95,8 +95,8 @@ func (e *Event) ToResponse() EventResponse {
9595
Description: e.Description,
9696
ClubID: e.ClubID,
9797
EventType: e.EventType,
98-
EventDate: e.EventDate.Format(time.RFC3339),
99-
EventTime: e.EventTime.Format(time.RFC3339),
98+
EventDate: e.EventDate.Format("2006-01-02"),
99+
EventTime: e.EventTime.Format("15:04"),
100100
Location: e.Location,
101101
OnlineLink: e.OnlineLink,
102102
MaxAttendees: e.MaxAttendees,

internal/services/event.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,34 @@ func (s *EventService) refreshClubNextMeeting(clubID uint) error {
183183
return err
184184
}
185185

186+
combine := func(d, t time.Time) time.Time {
187+
loc := d.Location()
188+
if loc == time.UTC && t.Location() != time.UTC {
189+
loc = t.Location()
190+
}
191+
return time.Date(
192+
d.Year(), d.Month(), d.Day(),
193+
t.Hour(), t.Minute(), t.Second(), t.Nanosecond(),
194+
loc,
195+
)
196+
}
197+
186198
now := time.Now()
187-
var next *models.Event
199+
var (
200+
nextEvent *models.Event
201+
nextStart time.Time
202+
)
203+
188204
for i := range events {
189-
e := events[i]
190-
if e.EventDate.Before(now) {
205+
event := &events[i]
206+
eventStart := combine(event.EventDate, event.EventTime)
207+
208+
if eventStart.Before(now) {
191209
continue
192210
}
193-
if next == nil || e.EventDate.Before(next.EventDate) {
194-
next = &e
211+
if nextEvent == nil || eventStart.Before(nextStart) {
212+
nextEvent = event
213+
nextStart = eventStart
195214
}
196215
}
197216

@@ -200,30 +219,30 @@ func (s *EventService) refreshClubNextMeeting(clubID uint) error {
200219
return err
201220
}
202221

203-
if next == nil {
222+
if nextEvent == nil {
204223
club.NextMeeting = nil
205224
return s.clubRepo.Update(club)
206225
}
207226

208227
var loc *string
209-
switch next.EventType {
228+
switch nextEvent.EventType {
210229
case models.EventOnline:
211-
if next.OnlineLink != "" {
212-
l := next.OnlineLink
230+
if nextEvent.OnlineLink != "" {
231+
l := nextEvent.OnlineLink
213232
loc = &l
214233
}
215234
default:
216-
if next.Location != "" {
217-
l := next.Location
235+
if nextEvent.Location != "" {
236+
l := nextEvent.Location
218237
loc = &l
219-
}
238+
}
220239
}
221240

222-
topic := next.Title
241+
topic := nextEvent.Title
223242
nm := models.NextMeeting{
224-
Date: &next.EventDate,
243+
Date: &nextEvent.EventDate,
225244
Location: loc,
226-
Topic: &topic,
245+
Topic: &topic,
227246
}
228247
if b, merr := json.Marshal(&nm); merr == nil {
229248
club.NextMeeting = b
@@ -233,7 +252,7 @@ func (s *EventService) refreshClubNextMeeting(clubID uint) error {
233252
}
234253

235254
func (s *EventService) ClubRepo() repository.ClubRepository {
236-
return s.clubRepo
255+
return s.clubRepo
237256
}
238257

239258
func (s *EventService) GetPublicEvents() ([]models.EventResponse, error) {
@@ -249,4 +268,4 @@ func (s *EventService) GetPublicEvents() ([]models.EventResponse, error) {
249268
}
250269

251270
return responses, nil
252-
}
271+
}

0 commit comments

Comments
 (0)