Skip to content

Commit 7b0ad9a

Browse files
add public event
1 parent 4acaba5 commit 7b0ad9a

File tree

6 files changed

+46
-0
lines changed

6 files changed

+46
-0
lines changed

internal/handlers/event.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,14 @@ func (h *EventHandler) GetEventAttendees(c *gin.Context) {
289289
}
290290

291291
c.JSON(http.StatusOK, gin.H{"attendees": attendees})
292+
}
293+
294+
func (h *EventHandler) GetPublicEvents(c *gin.Context) {
295+
events, err := h.eventService.GetPublicEvents()
296+
if err != nil {
297+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
298+
return
299+
}
300+
301+
c.JSON(http.StatusOK, gin.H{"events": events})
292302
}

internal/handlers/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ func (s *Server) setupRoutes() {
134134
api.GET("/users/:id/comments", commentHandler.ListCommentsByUserID)
135135
api.GET("/comments/:id", commentHandler.GetCommentByID)
136136
api.GET("/comments/:id/likes", commentHandler.ListLikesByCommentID)
137+
138+
api.GET("/events/public", eventHandler.GetPublicEvents)
137139
}
138140

139141
protected := api.Group("/")

internal/models/event.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Event struct {
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"`
1617
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
1718
Club Club `json:"club" gorm:"foreignKey:ClubID"`
1819
RSVPs []EventRSVP `json:"rsvps,omitempty"`
@@ -52,6 +53,7 @@ type CreateEventRequest struct {
5253
Location string `json:"location,omitempty"`
5354
OnlineLink string `json:"online_link,omitempty"`
5455
MaxAttendees *int `json:"max_attendees,omitempty"`
56+
IsPublic bool `json:"is_public" gorm:"default:false"`
5557
}
5658

5759
type UpdateEventRequest struct {
@@ -63,6 +65,7 @@ type UpdateEventRequest struct {
6365
Location *string `json:"location,omitempty"`
6466
OnlineLink *string `json:"online_link,omitempty"`
6567
MaxAttendees *int `json:"max_attendees,omitempty"`
68+
IsPublic *bool `json:"is_public,omitempty"`
6669
}
6770

6871
type RSVPRequest struct {
@@ -82,6 +85,7 @@ type EventResponse struct {
8285
MaxAttendees *int `json:"max_attendees,omitempty"`
8386
CreatedAt time.Time `json:"created_at"`
8487
RSVPs []EventRSVP `json:"rsvps,omitempty"`
88+
IsPublic *bool `json:"is_public,omitempty"`
8589
}
8690

8791
func (e *Event) ToResponse() EventResponse {
@@ -98,5 +102,6 @@ func (e *Event) ToResponse() EventResponse {
98102
MaxAttendees: e.MaxAttendees,
99103
CreatedAt: e.CreatedAt,
100104
RSVPs: e.RSVPs,
105+
IsPublic: &e.IsPublic,
101106
}
102107
}

internal/repository/event.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,16 @@ func (r *eventRepository) GetEventAttendees(eventID uint) ([]models.EventRSVP, e
7878
}
7979
return rsvps, nil
8080
}
81+
82+
func (r *eventRepository) GetPublicEvents() ([]models.Event, error) {
83+
var events []models.Event
84+
err := r.db.
85+
Where("is_public = ?", true).
86+
Preload("RSVPs").
87+
Find(&events).Error
88+
if err != nil {
89+
return nil, err
90+
}
91+
92+
return events, nil
93+
}

internal/repository/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type EventRepository interface {
4545
Delete(id uint) error
4646
RSVP(eventID uint, rsvp *models.EventRSVP) error
4747
GetEventAttendees(eventID uint) ([]models.EventRSVP, error)
48+
GetPublicEvents() ([]models.Event, error)
4849
}
4950

5051
type BookRepository interface {

internal/services/event.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,19 @@ func (s *EventService) refreshClubNextMeeting(clubID uint) error {
229229

230230
func (s *EventService) ClubRepo() repository.ClubRepository {
231231
return s.clubRepo
232+
}
233+
234+
func (s *EventService) GetPublicEvents() ([]models.EventResponse, error) {
235+
events, err := s.eventRepo.GetPublicEvents()
236+
if err != nil {
237+
return nil, err
238+
}
239+
240+
var responses []models.EventResponse
241+
for _, event := range events {
242+
response := event.ToResponse()
243+
responses = append(responses, response)
244+
}
245+
246+
return responses, nil
232247
}

0 commit comments

Comments
 (0)