diff --git a/backend/entities/events/base/controller.go b/backend/entities/events/base/controller.go index f144be5b..76971f52 100644 --- a/backend/entities/events/base/controller.go +++ b/backend/entities/events/base/controller.go @@ -17,7 +17,7 @@ func NewEventController(eventService EventServiceInterface) *EventController { return &EventController{eventService: eventService} } -// GetAllEvents godoc +// GetEvents godoc // // @Summary Retrieve all events // @Description Retrieves all events @@ -31,7 +31,7 @@ func NewEventController(eventService EventServiceInterface) *EventController { // @Failure 404 {object} error // @Failure 500 {object} error // @Router /events/ [get] -func (e *EventController) GetAllEvents(c *fiber.Ctx) error { +func (e *EventController) GetEvents(c *fiber.Ctx) error { pageInfo, ok := fiberpaginate.FromContext(c) if !ok { return utilities.ErrExpectedPageInfo @@ -42,22 +42,11 @@ func (e *EventController) GetAllEvents(c *fiber.Ctx) error { pageInfo = nil } - 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) + events, err := e.eventService.GetEvents(*pageInfo, c.Query("start"), c.Query("end")) + if err != nil { + return err } + 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 deleted file mode 100644 index d57e3219..00000000 --- a/backend/entities/events/base/models.go +++ /dev/null @@ -1,60 +0,0 @@ -package base - -import ( - "time" - - "github.com/GenerateNU/sac/backend/entities/models" - "github.com/google/uuid" -) - -type EventStub 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"` - - Host uuid.UUID `json:"host"` -} - -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"` -} diff --git a/backend/entities/events/base/routes.go b/backend/entities/events/base/routes.go index c0be2cb8..874ce72a 100644 --- a/backend/entities/events/base/routes.go +++ b/backend/entities/events/base/routes.go @@ -1,6 +1,7 @@ package base import ( + "github.com/GenerateNU/sac/backend/entities/events/previews" "github.com/GenerateNU/sac/backend/entities/events/series" "github.com/GenerateNU/sac/backend/entities/events/tags" "github.com/gofiber/fiber/v2" @@ -12,6 +13,7 @@ import ( func EventRoutes(eventParams types.RouteParams) { eventParams.Router = EventRouter(eventParams) + previews.EventPreviews(eventParams) series.EventSeries(eventParams) tags.EventTags(eventParams) } @@ -22,7 +24,7 @@ func EventRouter(eventParams types.RouteParams) fiber.Router { // api/v1/events/* events := eventParams.Router.Group("/events") - events.Get("/", eventParams.UtilityMiddleware.Paginator, eventController.GetAllEvents) + events.Get("/", eventParams.UtilityMiddleware.Paginator, eventController.GetEvents) events.Post( "/", authMiddleware.AttachExtractor(eventParams.AuthMiddleware.ClubAuthorizeById, authMiddleware.ExtractFromParams("clubID")), diff --git a/backend/entities/events/base/service.go b/backend/entities/events/base/service.go index 72c7126f..41ad3d99 100644 --- a/backend/entities/events/base/service.go +++ b/backend/entities/events/base/service.go @@ -2,11 +2,9 @@ package base import ( "errors" - "time" "github.com/GenerateNU/sac/backend/entities/events" "github.com/GenerateNU/sac/backend/entities/models" - "gorm.io/gorm" "github.com/GenerateNU/sac/backend/types" "github.com/GenerateNU/sac/backend/utilities" @@ -16,7 +14,6 @@ import ( type EventServiceInterface interface { // getters 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) @@ -33,11 +30,25 @@ func NewEventService(serviceParams types.ServiceParams) EventServiceInterface { } func (e *EventService) GetEvents(pageInfo fiberpaginate.PageInfo, start string, end string) ([]models.Event, error) { - return serviceGetEvents(e.DB, pageInfo, start, end, GetEvents, GetEventsByTime) -} + if start == "" || end == "" { + return events.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) + } -func (e *EventService) GetEventsPreview(pageInfo fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) { - return serviceGetEvents(e.DB, pageInfo, start, end, GetEventsPreview, GetEventsPreviewByTime) + if startTime.After(*endTime) { + return nil, utilities.BadRequest(errors.New("start time must be before end time")) + } + + return GetEventsByTime(e.DB, pageInfo, *startTime, *endTime) } func (e *EventService) GetEvent(eventID string) (*models.Event, error) { @@ -83,25 +94,3 @@ func (e *EventService) DeleteEvent(eventID string) error { return DeleteEvent(e.DB, *idAsUUID) } - -func serviceGetEvents[E any](db *gorm.DB, pageInfo fiberpaginate.PageInfo, start string, end string, getEFunc func(*gorm.DB, fiberpaginate.PageInfo) ([]E, error), getEByTimeFunc func(*gorm.DB, fiberpaginate.PageInfo, time.Time, time.Time) ([]E, error)) ([]E, error) { - if start == "" || end == "" { - return getEFunc(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 getEByTimeFunc(db, pageInfo, *startTime, *endTime) -} diff --git a/backend/entities/events/base/transactions.go b/backend/entities/events/base/transactions.go index 036bdbe9..4c975bb8 100644 --- a/backend/entities/events/base/transactions.go +++ b/backend/entities/events/base/transactions.go @@ -6,9 +6,7 @@ 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" @@ -21,63 +19,17 @@ import ( ) func GetEvents(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Event, error) { - return transactionsGetEvents[models.Event](db, &pageInfo, nil, nil) -} - -func GetEventsPreview(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]EventPreview, error) { - 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 + return events.GetEvents(db, pageInfo) } 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) -} + var events []models.Event -func GetEventsPreviewByTime(db *gorm.DB, pageInfo fiberpaginate.PageInfo, startTime time.Time, endTime time.Time) ([]EventPreview, error) { - events, err := transactionsGetEvents[EventStub](db, &pageInfo, &startTime, &endTime) - if err != nil { + if err := db.Model(&events).Where("start_time >= ? AND end_time <= ?", startTime, endTime).Scopes(utilities.IntoScope(pageInfo, db)).Find(&events).Error; 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 + return events, nil } func CreateEvent(db *gorm.DB, event models.Event) (*models.Event, error) { @@ -155,22 +107,3 @@ func DeleteEvent(db *gorm.DB, id uuid.UUID) error { return nil } - -func transactionsGetEvents[E any](db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime *time.Time, endTime *time.Time) ([]E, error) { - var events []E - 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/entities/events/previews/controller.go b/backend/entities/events/previews/controller.go new file mode 100644 index 00000000..810d1057 --- /dev/null +++ b/backend/entities/events/previews/controller.go @@ -0,0 +1,35 @@ +package previews + +import ( + "net/http" + + "github.com/GenerateNU/sac/backend/utilities" + "github.com/garrettladley/fiberpaginate" + "github.com/gofiber/fiber/v2" +) + +type EventPreviewController struct { + service Service +} + +func NewEventController(service Service) *EventPreviewController { + return &EventPreviewController{service: service} +} + +func (e *EventPreviewController) GetEventPreviews(c *fiber.Ctx) error { + pageInfo, ok := fiberpaginate.FromContext(c) + if !ok { + return utilities.ErrExpectedPageInfo + } + + usePagination := c.QueryBool("pagination", true) + if !usePagination { + pageInfo = nil + } + + events, err := e.service.GetEventPreviews(pageInfo, c.Query("start"), c.Query("end")) + if err != nil { + return err + } + return c.Status(http.StatusOK).JSON(events) +} diff --git a/backend/entities/events/previews/models.go b/backend/entities/events/previews/models.go new file mode 100644 index 00000000..5d18ac11 --- /dev/null +++ b/backend/entities/events/previews/models.go @@ -0,0 +1,43 @@ +package previews + +import ( + "time" + + "github.com/GenerateNU/sac/backend/entities/models" + "github.com/google/uuid" +) + +type common 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"` +} + +type EventStub struct { + common + Host uuid.UUID `json:"host"` +} + +func (es *EventStub) Into(tags []models.Tag, hostName string, hostLogo string) *EventPreview { + return &EventPreview{ + common: es.common, + Tags: tags, + HostName: hostName, + HostLogo: hostLogo, + } +} + +func (es EventStub) TableName() string { + return "events" +} + +type EventPreview struct { + common + Tags []models.Tag `json:"tags"` + HostName string `json:"host_name"` + HostLogo string `json:"host_logo"` +} diff --git a/backend/entities/events/previews/routes.go b/backend/entities/events/previews/routes.go new file mode 100644 index 00000000..9c7912ac --- /dev/null +++ b/backend/entities/events/previews/routes.go @@ -0,0 +1,16 @@ +package previews + +import ( + "github.com/gofiber/fiber/v2" + + "github.com/GenerateNU/sac/backend/types" +) + +func EventPreviews(eventParams types.RouteParams) { + eventController := NewEventController(NewService(eventParams.ServiceParams)) + + // api/v1/events/previews/ + eventParams.Router.Route("/previews", func(events fiber.Router) { + events.Get("/", eventParams.UtilityMiddleware.Paginator, eventController.GetEventPreviews) + }) +} diff --git a/backend/entities/events/previews/service.go b/backend/entities/events/previews/service.go new file mode 100644 index 00000000..b3eba712 --- /dev/null +++ b/backend/entities/events/previews/service.go @@ -0,0 +1,43 @@ +package previews + +import ( + "errors" + + "github.com/GenerateNU/sac/backend/types" + "github.com/GenerateNU/sac/backend/utilities" + "github.com/garrettladley/fiberpaginate" +) + +type Service interface { + GetEventPreviews(pageInfo *fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) +} + +type Handler struct { + types.ServiceParams +} + +func NewService(serviceParams types.ServiceParams) Service { + return &Handler{serviceParams} +} + +func (h *Handler) GetEventPreviews(pageInfo *fiberpaginate.PageInfo, start string, end string) ([]EventPreview, error) { + if start == "" || end == "" { + return GetEventPreivews(h.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 GetEventPreviewsByTime(h.DB, pageInfo, startTime, endTime) +} diff --git a/backend/entities/events/previews/transactions.go b/backend/entities/events/previews/transactions.go new file mode 100644 index 00000000..599d3456 --- /dev/null +++ b/backend/entities/events/previews/transactions.go @@ -0,0 +1,61 @@ +package previews + +import ( + "time" + + "github.com/garrettladley/fiberpaginate" + + "github.com/GenerateNU/sac/backend/entities/clubs" + "github.com/GenerateNU/sac/backend/entities/events/tags" + "github.com/GenerateNU/sac/backend/utilities" + + "gorm.io/gorm" +) + +func GetEventPreivews(db *gorm.DB, pageInfo *fiberpaginate.PageInfo) ([]EventPreview, error) { + var events []EventStub + + if err := db.Scopes(utilities.IntoScope(*pageInfo, db)).Find(&events).Error; err != nil { + return nil, err + } + + return processEventStubs(db, events) +} + +func GetEventPreviewsByTime(db *gorm.DB, pageInfo *fiberpaginate.PageInfo, startTime *time.Time, endTime *time.Time) ([]EventPreview, error) { + var events []EventStub + + 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 processEventStubs(db, events) +} + +func processEventStubs(db *gorm.DB, events []EventStub) ([]EventPreview, error) { + 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) + } + return eventPreviews, nil +} diff --git a/backend/entities/events/transactions.go b/backend/entities/events/transactions.go index 1a4b3ca7..9b9daf83 100644 --- a/backend/entities/events/transactions.go +++ b/backend/entities/events/transactions.go @@ -7,10 +7,21 @@ import ( "github.com/GenerateNU/sac/backend/entities/models" "github.com/GenerateNU/sac/backend/transactions" "github.com/GenerateNU/sac/backend/utilities" + "github.com/garrettladley/fiberpaginate" "github.com/google/uuid" "gorm.io/gorm" ) +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 events, nil +} + func GetEvent(db *gorm.DB, eventID uuid.UUID, preloads ...transactions.OptionalQuery) (*models.Event, error) { var event models.Event