Skip to content

Commit

Permalink
Merge branch 'main' into 1006-feat-db-redis-caching
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Jun 11, 2024
2 parents aba79e1 + ccb0a3c commit f4c4bbf
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
39 changes: 37 additions & 2 deletions backend/entities/events/base/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/google/uuid"
)

type EventPreview struct {
type EventStub struct {
ID uuid.UUID `json:"id"`

Title string `json:"title"`
Expand All @@ -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"`
}
50 changes: 48 additions & 2 deletions backend/entities/events/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -23,15 +25,59 @@ 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) {
return transactionsGetEvents[models.Event](db, &pageInfo, &startTime, &endTime)
}

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) {
Expand Down

0 comments on commit f4c4bbf

Please sign in to comment.