diff --git a/backend/entities/events/previews/service.go b/backend/entities/events/previews/service.go index b3eba712..f93f5ff9 100644 --- a/backend/entities/events/previews/service.go +++ b/backend/entities/events/previews/service.go @@ -22,7 +22,7 @@ func NewService(serviceParams types.ServiceParams) Service { func (h *Handler) GetEventPreviews(pageInfo *fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) { if start == "" || end == "" { - return GetEventPreivews(h.DB, pageInfo) + return GetEventPreviews(h.DB, pageInfo) } startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD) diff --git a/backend/entities/events/previews/transactions.go b/backend/entities/events/previews/transactions.go index 1c2636b6..8f257286 100644 --- a/backend/entities/events/previews/transactions.go +++ b/backend/entities/events/previews/transactions.go @@ -4,16 +4,16 @@ import ( "time" "github.com/garrettladley/fiberpaginate" + "github.com/google/uuid" "github.com/GenerateNU/sac/backend/database/cache" - "github.com/GenerateNU/sac/backend/entities/clubs" - "github.com/GenerateNU/sac/backend/entities/events/tags" + "github.com/GenerateNU/sac/backend/entities/models" "github.com/GenerateNU/sac/backend/utilities" "gorm.io/gorm" ) -func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) { +func GetEventPreviews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) { db = cache.SetUseCache(db, true) var events []EventStub @@ -24,19 +24,16 @@ func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPre return processEventStubs(db, events) } -func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime *time.Time, endTime *time.Time) ([]EventPreview, error) { - var events []EventStub +func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime, endTime *time.Time) ([]EventPreview, error) { + db = cache.SetUseCache(db, true) - query := cache.SetUseCache(db, true).Model(&events) + var events []EventStub + query := db.Model(&events).Scopes(utilities.IntoScope(*pageInfo, db)) if startTime != nil && endTime != nil { query = query.Where("start_time >= ? AND end_time <= ?", *startTime, *endTime) } - if pageInfo != nil { - query = query.Scopes(utilities.IntoScope(*pageInfo, db)) - } - if err := query.Find(&events).Error; err != nil { return nil, err } @@ -45,19 +42,48 @@ func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, start } func processEventStubs(db *gorm.DB, events []EventStub) ([]EventPreview, error) { - eventPreviews := make([]EventPreview, len(events)) + eventIDs := make([]uuid.UUID, len(events)) for index, event := range events { - eventTags, err := tags.GetEventTags(db, event.ID) - if err != nil { - return nil, err - } + eventIDs[index] = event.ID + } + + var eventTags []struct { + EventID uuid.UUID `gorm:"column:event_id"` + TagID uuid.UUID `gorm:"column:tag_id"` + Name string `gorm:"column:name"` + } + if err := db.Table("tags").Select("tags.id as tag_id, tags.name, event_tags.event_id"). + Joins("JOIN event_tags ON event_tags.tag_id = tags.id"). + Where("event_tags.event_id IN ?", eventIDs). + Scan(&eventTags).Error; err != nil { + return nil, err + } + + eventTagsMap := make(map[uuid.UUID][]models.Tag) + for _, et := range eventTags { + eventTagsMap[et.EventID] = append(eventTagsMap[et.EventID], + models.Tag{ + Model: models.Model{ + ID: et.TagID, + }, + Name: et.Name, + }) + } + + var hosts []models.Club + if err := db.Where("id IN ?", eventIDs).Find(&hosts).Error; err != nil { + return nil, err + } - host, err := clubs.GetClub(db, event.Host) - if err != nil { - return nil, err - } + hostsMap := make(map[uuid.UUID]models.Club) + for _, host := range hosts { + hostsMap[host.ID] = host + } - eventPreviews[index] = *event.Into(eventTags, host.Name, host.Logo) + eventPreviews := make([]EventPreview, len(events)) + for index, event := range events { + eventPreviews[index] = *event.Into(eventTagsMap[event.ID], hostsMap[event.Host].Name, hostsMap[event.Host].Logo) } + return eventPreviews, nil } diff --git a/backend/entities/events/tags/transactions.go b/backend/entities/events/tags/transactions.go index f1cf497b..d80bda35 100644 --- a/backend/entities/events/tags/transactions.go +++ b/backend/entities/events/tags/transactions.go @@ -1,7 +1,6 @@ package tags import ( - "github.com/GenerateNU/sac/backend/database/cache" "github.com/GenerateNU/sac/backend/entities/events" "github.com/GenerateNU/sac/backend/entities/models" @@ -26,15 +25,10 @@ func CreateEventTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag } func GetEventTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, error) { - db = cache.SetUseCache(db, true) - var tags []models.Tag - event, err := events.GetEvent(db, id, transactions.PreloadTag()) - if err != nil { - return nil, err - } - - if err := db.Model(&event).Association("Tag").Find(&tags); err != nil { + if err := db.Joins("JOIN event_tags ON event_tags.tag_id = tags.id"). + Where("event_tags.event_id = ?", id). + Find(&tags).Error; err != nil { return nil, err } return tags, nil