Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 chore: Extend Event Preview Struct #1005

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading