diff --git a/backend/entities/events/base/models.go b/backend/entities/events/base/models.go index 5f7b76e5..d57e3219 100644 --- a/backend/entities/events/base/models.go +++ b/backend/entities/events/base/models.go @@ -7,7 +7,7 @@ import ( "github.com/google/uuid" ) -type EventPreview struct { +type EventStub struct { ID uuid.UUID `json:"id"` Title string `json:"title"` @@ -18,8 +18,43 @@ type EventPreview struct { StartTime time.Time `json:"start_time"` EndTime time.Time `json:"end_time"` + + Host uuid.UUID `json:"host"` } -func (ep EventPreview) TableName() string { +func (es *EventStub) Into(tags []models.Tag, hostName string, hostLogo string) *EventPreview { + return &EventPreview{ + ID: es.ID, + Title: es.Title, + EventType: es.EventType, + Location: es.Location, + Link: es.Link, + StartTime: es.StartTime, + EndTime: es.EndTime, + Tags: tags, + HostName: hostName, + HostLogo: hostLogo, + } +} + +func (es EventStub) TableName() string { return "events" } + +type EventPreview struct { + ID uuid.UUID `json:"id"` + + Title string `json:"title"` + + EventType models.EventType `json:"event_type"` + Location string `json:"location"` + Link string `json:"link"` + + StartTime time.Time `json:"start_time"` + EndTime time.Time `json:"end_time"` + + Tags []models.Tag `json:"tags"` + + HostName string `json:"host_name"` + HostLogo string `json:"host_logo"` +} diff --git a/backend/entities/events/base/transactions.go b/backend/entities/events/base/transactions.go index 9fd3aae2..036bdbe9 100644 --- a/backend/entities/events/base/transactions.go +++ b/backend/entities/events/base/transactions.go @@ -6,7 +6,9 @@ import ( "time" "github.com/GenerateNU/sac/backend/constants" + "github.com/GenerateNU/sac/backend/entities/clubs" "github.com/GenerateNU/sac/backend/entities/events" + "github.com/GenerateNU/sac/backend/entities/events/tags" "github.com/GenerateNU/sac/backend/entities/models" "github.com/garrettladley/fiberpaginate" @@ -23,7 +25,29 @@ func GetEvents(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Event, er } func GetEventsPreview(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]EventPreview, error) { - return transactionsGetEvents[EventPreview](db, &pageInfo, nil, nil) + events, err := transactionsGetEvents[EventStub](db, &pageInfo, nil, nil) + if err != nil { + return nil, err + } + + eventPreviews := make([]EventPreview, len(events)) + for index, event := range events { + tags, err := tags.GetEventTags(db, event.ID) + if err != nil { + return nil, err + } + + eventPreviews[index].Tags = tags + + host, err := clubs.GetClub(db, event.Host) + if err != nil { + return nil, err + } + + eventPreviews[index] = *event.Into(tags, host.Name, host.Logo) + } + + return eventPreviews, nil } func GetEventsByTime(db *gorm.DB, pageInfo fiberpaginate.PageInfo, startTime time.Time, endTime time.Time) ([]models.Event, error) { @@ -31,7 +55,29 @@ func GetEventsByTime(db *gorm.DB, pageInfo fiberpaginate.PageInfo, startTime tim } func GetEventsPreviewByTime(db *gorm.DB, pageInfo fiberpaginate.PageInfo, startTime time.Time, endTime time.Time) ([]EventPreview, error) { - return transactionsGetEvents[EventPreview](db, &pageInfo, &startTime, &endTime) + events, err := transactionsGetEvents[EventStub](db, &pageInfo, &startTime, &endTime) + if err != nil { + return nil, err + } + + eventPreviews := make([]EventPreview, len(events)) + for index, event := range events { + tags, err := tags.GetEventTags(db, event.ID) + if err != nil { + return nil, err + } + + eventPreviews[index].Tags = tags + + host, err := clubs.GetClub(db, event.Host) + if err != nil { + return nil, err + } + + eventPreviews[index] = *event.Into(tags, host.Name, host.Logo) + } + + return eventPreviews, nil } func CreateEvent(db *gorm.DB, event models.Event) (*models.Event, error) {