From e0d0db4ccb5b0324e4f83dff6b87ebedfbb67172 Mon Sep 17 00:00:00 2001 From: garrettladley Date: Tue, 4 Jun 2024 21:35:10 -0400 Subject: [PATCH] get events start & end and preview --- backend/entities/events/base/controller.go | 20 ++++++-- backend/entities/events/base/models.go | 18 +++++++ backend/entities/events/base/service.go | 49 ++++++++++++++++++-- backend/entities/events/base/transactions.go | 37 +++++++++++++-- backend/utilities/time.go | 13 ++++++ 5 files changed, 124 insertions(+), 13 deletions(-) create mode 100644 backend/entities/events/base/models.go create mode 100644 backend/utilities/time.go diff --git a/backend/entities/events/base/controller.go b/backend/entities/events/base/controller.go index 7d5a8003c..e36ed8b5c 100644 --- a/backend/entities/events/base/controller.go +++ b/backend/entities/events/base/controller.go @@ -37,12 +37,22 @@ func (e *EventController) GetAllEvents(c *fiber.Ctx) error { return utilities.ErrExpectedPageInfo } - events, err := e.eventService.GetEvents(*pageInfo) - if err != nil { - return err + start := c.Query("start") + end := c.Query("end") + + if c.QueryBool("preview", false) { + events, err := e.eventService.GetEventsPreview(*pageInfo, start, end) + if err != nil { + return err + } + return c.Status(http.StatusOK).JSON(events) + } else { + events, err := e.eventService.GetEvents(*pageInfo, start, end) + if err != nil { + return err + } + return c.Status(http.StatusOK).JSON(events) } - - return c.Status(http.StatusOK).JSON(events) } // GetEvent godoc diff --git a/backend/entities/events/base/models.go b/backend/entities/events/base/models.go new file mode 100644 index 000000000..b5f96e20d --- /dev/null +++ b/backend/entities/events/base/models.go @@ -0,0 +1,18 @@ +package base + +import ( + "time" + + "github.com/GenerateNU/sac/backend/entities/models" +) + +type EventPreview struct { + 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"` +} diff --git a/backend/entities/events/base/service.go b/backend/entities/events/base/service.go index 0f7c9fa1e..f3e746500 100644 --- a/backend/entities/events/base/service.go +++ b/backend/entities/events/base/service.go @@ -1,6 +1,8 @@ package base import ( + "errors" + "github.com/GenerateNU/sac/backend/entities/events" "github.com/GenerateNU/sac/backend/entities/models" @@ -11,7 +13,8 @@ import ( type EventServiceInterface interface { // getters - GetEvents(pageInfo fiberpaginate.PageInfo) ([]models.Event, error) + GetEvents(pageInfo fiberpaginate.PageInfo, start string, end string) ([]models.Event, error) + GetEventsPreview(pageInfo fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) GetEvent(eventID string) (*models.Event, error) // event cud CreateEvent(body events.CreateEventRequestBody) (*models.Event, error) @@ -27,8 +30,48 @@ func NewEventService(serviceParams types.ServiceParams) EventServiceInterface { return &EventService{serviceParams} } -func (e *EventService) GetEvents(pageInfo fiberpaginate.PageInfo) ([]models.Event, error) { - return GetEvents(e.DB, pageInfo) +func (e *EventService) GetEvents(pageInfo fiberpaginate.PageInfo, start string, end string) ([]models.Event, error) { + if start != "" && end != "" { + return GetEvents(e.DB, pageInfo) + } + + startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD) + if err != nil { + return nil, utilities.BadRequest(err) + } + + endTime, err := utilities.ParseTime(end, utilities.YYYY_dash_MM_dash_DD) + if err != nil { + return nil, utilities.BadRequest(err) + } + + if startTime.After(endTime) { + return nil, utilities.BadRequest(errors.New("start time must be before end time")) + } + + return GetEventsByTime(e.DB, startTime, endTime) +} + +func (e *EventService) GetEventsPreview(pageInfo fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) { + if start != "" && end != "" { + return GetEventsPreview(e.DB, pageInfo) + } + + startTime, err := utilities.ParseTime(start, utilities.YYYY_dash_MM_dash_DD) + if err != nil { + return nil, utilities.BadRequest(err) + } + + endTime, err := utilities.ParseTime(end, utilities.YYYY_dash_MM_dash_DD) + if err != nil { + return nil, utilities.BadRequest(err) + } + + if startTime.After(endTime) { + return nil, utilities.BadRequest(errors.New("start time must be before end time")) + } + + return GetEventsPreviewByTime(e.DB, startTime, endTime) } func (e *EventService) GetEvent(eventID string) (*models.Event, error) { diff --git a/backend/entities/events/base/transactions.go b/backend/entities/events/base/transactions.go index 10b47a15e..1010abdd4 100644 --- a/backend/entities/events/base/transactions.go +++ b/backend/entities/events/base/transactions.go @@ -3,6 +3,7 @@ package base import ( "errors" "log/slog" + "time" "github.com/GenerateNU/sac/backend/constants" "github.com/GenerateNU/sac/backend/entities/events" @@ -18,12 +19,19 @@ import ( ) func GetEvents(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Event, error) { - var events []models.Event - if err := db.Scopes(utilities.IntoScope(pageInfo, db)).Find(&events).Error; err != nil { - return nil, err - } + return getEvents[models.Event](db, &pageInfo, nil, nil) +} - return events, nil +func GetEventsPreview(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]EventPreview, error) { + return getEvents[EventPreview](db, &pageInfo, nil, nil) +} + +func GetEventsByTime(db *gorm.DB, startTime time.Time, endTime time.Time) ([]models.Event, error) { + return getEvents[models.Event](db, nil, &startTime, &endTime) +} + +func GetEventsPreviewByTime(db *gorm.DB, startTime time.Time, endTime time.Time) ([]EventPreview, error) { + return getEvents[EventPreview](db, nil, &startTime, &endTime) } func CreateEvent(db *gorm.DB, event models.Event) (*models.Event, error) { @@ -101,3 +109,22 @@ func DeleteEvent(db *gorm.DB, id uuid.UUID) error { return nil } + +func getEvents[T any](db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime, endTime *time.Time) ([]T, error) { + var events []T + query := db.Model(&events) + + 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 + } + + return events, nil +} diff --git a/backend/utilities/time.go b/backend/utilities/time.go new file mode 100644 index 000000000..5676509cc --- /dev/null +++ b/backend/utilities/time.go @@ -0,0 +1,13 @@ +package utilities + +import "time" + +type TimeFormat string + +const ( + YYYY_dash_MM_dash_DD TimeFormat = "2006-01-02" +) + +func ParseTime(s string, format TimeFormat) (time.Time, error) { + return time.Parse(string(format), s) +}