From d6af301ecda23b0961175a9699e62d34b77d7a31 Mon Sep 17 00:00:00 2001 From: Zack Lassetter Date: Wed, 7 Feb 2024 21:47:06 -0500 Subject: [PATCH] Started working on filtering Co-authored-by: Melody Yu --- backend/src/controllers/club.go | 10 +++++++--- backend/src/models/club.go | 8 ++++++++ backend/src/server/routes/club.go | 2 +- backend/src/server/server.go | 2 +- backend/src/services/club.go | 15 ++++++--------- backend/src/transactions/club.go | 13 +++++++++++-- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/backend/src/controllers/club.go b/backend/src/controllers/club.go index e9b3d433d..f9cdfab68 100644 --- a/backend/src/controllers/club.go +++ b/backend/src/controllers/club.go @@ -1,8 +1,6 @@ package controllers import ( - "strconv" - "github.com/GenerateNU/sac/backend/src/errors" "github.com/GenerateNU/sac/backend/src/models" "github.com/GenerateNU/sac/backend/src/services" @@ -20,8 +18,14 @@ func NewClubController(clubService services.ClubServiceInterface) *ClubControlle func (cl *ClubController) GetAllClubs(c *fiber.Ctx) error { defaultLimit := 10 defaultPage := 1 + p := new(models.ClubQueryParams) + p.Limit = defaultLimit + p.Page = defaultPage + if err := c.QueryParser(p); err != nil { + return err + } - clubs, err := cl.clubService.GetClubs(c.Query("limit", strconv.Itoa(defaultLimit)), c.Query("page", strconv.Itoa(defaultPage))) + clubs, err := cl.clubService.GetClubs(p) if err != nil { return err.FiberError(c) } diff --git a/backend/src/models/club.go b/backend/src/models/club.go index 3c56614d6..5380b96ee 100644 --- a/backend/src/models/club.go +++ b/backend/src/models/club.go @@ -75,6 +75,14 @@ type UpdateClubRequestBody struct { Logo string `json:"logo" validate:"omitempty,http_url,s3_url,max=255"` // S3 URL } +type ClubQueryParams struct { + //TODO: try with specific ENUM types + RecruitmentCycle *RecruitmentCycle `query:"recruitment_cycle"` + IsRecruiting *bool `query:"is_recruiting"` + Limit int `query:"limit"` + Page int `query:"page"` +} + func (c *Club) AfterCreate(tx *gorm.DB) (err error) { tx.Model(&c).Update("num_members", c.NumMembers+1) return diff --git a/backend/src/server/routes/club.go b/backend/src/server/routes/club.go index 91d2e440a..ecbd92957 100644 --- a/backend/src/server/routes/club.go +++ b/backend/src/server/routes/club.go @@ -13,7 +13,7 @@ func Club(router fiber.Router, clubService services.ClubServiceInterface, middle clubs := router.Group("/clubs") - clubs.Get("/", middlewareService.Authorize(types.ClubReadAll), clubController.GetAllClubs) + clubs.Get("/", clubController.GetAllClubs) clubs.Post("/", clubController.CreateClub) // api/v1/clubs/:clubID/* diff --git a/backend/src/server/server.go b/backend/src/server/server.go index 5d249a0a3..7de1ea3bf 100644 --- a/backend/src/server/server.go +++ b/backend/src/server/server.go @@ -34,7 +34,7 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App { middlewareService := middleware.NewMiddlewareService(db, validate, settings.Auth) apiv1 := app.Group("/api/v1") - apiv1.Use(middlewareService.Authenticate) + // apiv1.Use(middlewareService.Authenticate) routes.Utility(app) diff --git a/backend/src/services/club.go b/backend/src/services/club.go index c05610c1f..dc677196e 100644 --- a/backend/src/services/club.go +++ b/backend/src/services/club.go @@ -11,7 +11,7 @@ import ( ) type ClubServiceInterface interface { - GetClubs(limit string, page string) ([]models.Club, *errors.Error) + GetClubs(params *models.ClubQueryParams) ([]models.Club, *errors.Error) GetClub(id string) (*models.Club, *errors.Error) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, *errors.Error) UpdateClub(id string, clubBody models.UpdateClubRequestBody) (*models.Club, *errors.Error) @@ -27,20 +27,17 @@ func NewClubService(db *gorm.DB, validate *validator.Validate) *ClubService { return &ClubService{DB: db, Validate: validate} } -func (c *ClubService) GetClubs(limit string, page string) ([]models.Club, *errors.Error) { - limitAsInt, err := utilities.ValidateNonNegative(limit) - if err != nil { +func (c *ClubService) GetClubs(params *models.ClubQueryParams) ([]models.Club, *errors.Error) { + + if params.Limit < 0 { return nil, &errors.FailedToValidateLimit } - pageAsInt, err := utilities.ValidateNonNegative(page) - if err != nil { + if params.Page < 0 { return nil, &errors.FailedToValidatePage } - offset := (*pageAsInt - 1) * *limitAsInt - - return transactions.GetClubs(c.DB, *limitAsInt, offset) + return transactions.GetClubs(c.DB, params) } func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, *errors.Error) { diff --git a/backend/src/transactions/club.go b/backend/src/transactions/club.go index 016e2d0ef..e4009565f 100644 --- a/backend/src/transactions/club.go +++ b/backend/src/transactions/club.go @@ -25,9 +25,18 @@ func GetAdminIDs(db *gorm.DB, clubID uuid.UUID) ([]uuid.UUID, *errors.Error) { return adminUUIDs, nil } -func GetClubs(db *gorm.DB, limit int, offset int) ([]models.Club, *errors.Error) { +func GetClubs(db *gorm.DB, params *models.ClubQueryParams) ([]models.Club, *errors.Error) { + offset := (*¶ms.Page - 1) * *¶ms.Limit var clubs []models.Club - result := db.Limit(limit).Offset(offset).Find(&clubs) + //TODO try with mapper function in manipulator.go + query_params := map[string]interface{}{} + if params.RecruitmentCycle != nil { + query_params["recruitment_cycle"] = params.RecruitmentCycle + } + if params.IsRecruiting != nil { + query_params["is_recruiting"] = *params.IsRecruiting + } + result := db.Where(query_params).Limit(params.Limit).Offset(offset).Find(&clubs) if result.Error != nil { return nil, &errors.FailedToGetClubs }