From 802c6c37b549e9fc9c6a473c19784cc24f5eaec3 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Wed, 12 Jun 2024 20:38:44 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20feat:=20optimize=20event=20previ?= =?UTF-8?q?ew=20query?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/entities/events/previews/service.go | 2 +- .../entities/events/previews/transactions.go | 49 ++++++++++--------- backend/entities/events/tags/transactions.go | 12 ++--- 3 files changed, 31 insertions(+), 32 deletions(-) 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..49095584 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,27 @@ func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, start } func processEventStubs(db *gorm.DB, events []EventStub) ([]EventPreview, error) { + eventIDs := make([]uuid.UUID, len(events)) + for i, event := range events { + eventIDs[i] = event.ID + } + + var eventTagsMap map[uuid.UUID][]models.Tag + if err := db.Model(&models.Tag{}).Joins("JOIN event_tags ON event_tags.tag_id = tags.id"). + Where("event_tags.event_id IN ?", eventIDs). + Find(&eventTagsMap).Error; err != nil { + return nil, err + } + + var hostsMap map[uuid.UUID]models.Club + if err := db.Model(&models.Club{}).Where("id IN ?", eventIDs).Find(&hostsMap).Error; err != nil { + return nil, err + } + eventPreviews := make([]EventPreview, len(events)) - for index, event := range events { - eventTags, err := tags.GetEventTags(db, event.ID) - if err != nil { - return nil, err - } - - host, err := clubs.GetClub(db, event.Host) - if err != nil { - return nil, err - } - - eventPreviews[index] = *event.Into(eventTags, host.Name, host.Logo) + for i, event := range events { + eventPreviews[i] = *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