diff --git a/backend/src/server/routes/club.go b/backend/src/server/routes/club.go index f593389ea..db1cbd376 100644 --- a/backend/src/server/routes/club.go +++ b/backend/src/server/routes/club.go @@ -24,5 +24,10 @@ func Club(router fiber.Router, clubService services.ClubServiceInterface, middle clubsID.Patch("/", middlewareService.Authorize(types.ClubWrite), clubController.UpdateClub) clubsID.Delete("/", middlewareService.Authorize(types.ClubDelete), clubController.DeleteClub) + // TODO: refactor into club_event vertical (controller <-> service <-> model) + // api/v1/clubs/:clubID/events/* + events := clubsID.Group("/events") + events.Get("/", clubController.GetClubEvents) + return clubsID } diff --git a/backend/src/server/routes/event.go b/backend/src/server/routes/event.go new file mode 100644 index 000000000..1b49172b8 --- /dev/null +++ b/backend/src/server/routes/event.go @@ -0,0 +1,21 @@ +package routes + +import ( + "github.com/GenerateNU/sac/backend/src/controllers" + "github.com/GenerateNU/sac/backend/src/services" + "github.com/gofiber/fiber/v2" +) + +func Event(router fiber.Router, eventService services.EventServiceInterface) { + eventController := controllers.NewEventController(eventService) + + events := router.Group("/events") + + events.Get("/:id", eventController.GetEvent) + events.Get("/:id/series", eventController.GetSeriesByEventId) + events.Get("/", eventController.GetAllEvents) + events.Post("/", eventController.CreateEvent) + events.Patch("/:id", eventController.UpdateEvent) + events.Delete("/:id", eventController.DeleteEvent) + events.Delete("/:id/series", eventController.DeleteEventSeries) +} \ No newline at end of file diff --git a/backend/src/server/server.go b/backend/src/server/server.go index 1446227a1..4717cbde4 100644 --- a/backend/src/server/server.go +++ b/backend/src/server/server.go @@ -36,8 +36,6 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App { apiv1 := app.Group("/api/v1") apiv1.Use(middlewareService.Authenticate) - - eventRoutes(apiv1, &services.EventService{DB: db, Validate: validate}) routes.Utility(app) routes.Auth(apiv1, services.NewAuthService(db, validate), settings.Auth) @@ -60,6 +58,8 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App { categoryRouter := routes.Category(apiv1, services.NewCategoryService(db, validate)) routes.CategoryTag(categoryRouter, services.NewCategoryTagService(db, validate)) + routes.Event(apiv1, services.NewEventService(db, validate)) + return app } @@ -80,24 +80,3 @@ func newFiberApp() *fiber.App { return app } - - - //events := clubs.Group("/:id/events") - //events.Get("/", clubController.GetClubEvents) - - - -func eventRoutes(router fiber.Router, eventService services.EventServiceInterface) { - eventController := controllers.NewEventController(eventService) - - events := router.Group("/events") - - events.Get("/:id", eventController.GetEvent) - events.Get("/:id/series", eventController.GetSeriesByEventId) - events.Get("/", eventController.GetAllEvents) - events.Post("/", eventController.CreateEvent) - events.Patch("/:id", eventController.UpdateEvent) - events.Delete("/:id", eventController.DeleteEvent) - events.Delete("/:id/series", eventController.DeleteEventSeries) -} - diff --git a/backend/src/services/event.go b/backend/src/services/event.go index 6ff153eea..20e9ec48f 100644 --- a/backend/src/services/event.go +++ b/backend/src/services/event.go @@ -26,15 +26,17 @@ type EventService struct { Validate *validator.Validate } +func NewEventService(db *gorm.DB, validate *validator.Validate) *EventService { + return &EventService{DB: db, Validate: validate} +} + func (c *EventService) GetEvents(limit string, page string) ([]models.Event, *errors.Error) { limitAsInt, err := utilities.ValidateNonNegative(limit) - if err != nil { return nil, &errors.FailedToValidateLimit } pageAsInt, err := utilities.ValidateNonNegative(page) - if err != nil { return nil, &errors.FailedToValidatePage } @@ -56,7 +58,6 @@ func (c *EventService) GetClubEvents(id string) ([]models.Event, *errors.Error) // TODO: add logic for creating the []event here // TODO Q: should we always return a slice of events? or should we return a slice of events if it's a series and a single event if it's not? func (c *EventService) CreateEvent(eventBody models.CreateEventRequestBody) ([]models.Event, *errors.Error) { - if err := c.Validate.Struct(eventBody); err != nil { return nil, &errors.FailedToValidateEvent } @@ -142,17 +143,15 @@ func (c *EventService) DeleteEvent(id string) *errors.Error { } func (c *EventService) DeleteEventSeries(id string) *errors.Error { - idAsUUID, err := utilities.ValidateID(id) if err != nil { return &errors.FailedToValidateID } return transactions.DeleteEventSeries(c.DB, *idAsUUID) - } -//TODO: CreateEventSeries, GetEventSeries, DeleteEventSeries +// TODO: CreateEventSeries, GetEventSeries, DeleteEventSeries // Helper to create other events in a given series using the given firstEvent func CreateEventSlice(firstEvent *models.Event, series models.Series) []models.Event {