Skip to content

Commit

Permalink
📝 feat: optimize event preview query
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jun 13, 2024
1 parent 2dd0789 commit 802c6c3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
2 changes: 1 addition & 1 deletion backend/entities/events/previews/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
49 changes: 27 additions & 22 deletions backend/entities/events/previews/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
}
12 changes: 3 additions & 9 deletions backend/entities/events/tags/transactions.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 802c6c3

Please sign in to comment.