From ef90252bc7722eb2a8c2a401a48bf456cd03c807 Mon Sep 17 00:00:00 2001 From: Melody Yu Date: Sun, 11 Feb 2024 16:02:15 -0500 Subject: [PATCH] feat: implemented Min/MaxMembers filtering, refactored querying --- backend/src/models/club.go | 39 +++++++++++++++++++++++++++++++- backend/src/services/club.go | 1 - backend/src/transactions/club.go | 12 +++------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/backend/src/models/club.go b/backend/src/models/club.go index 5380b96ee..beaa32acd 100644 --- a/backend/src/models/club.go +++ b/backend/src/models/club.go @@ -1,6 +1,9 @@ package models import ( + "fmt" + "strings" + "github.com/google/uuid" "gorm.io/gorm" ) @@ -76,13 +79,47 @@ type UpdateClubRequestBody struct { } type ClubQueryParams struct { - //TODO: try with specific ENUM types + // Tags []uuid.UUIDs `query:"tags"` + MinMembers int `query:"min_members"` + MaxMembers int `query:"max_members"` RecruitmentCycle *RecruitmentCycle `query:"recruitment_cycle"` IsRecruiting *bool `query:"is_recruiting"` Limit int `query:"limit"` Page int `query:"page"` } +func (cqp *ClubQueryParams) IntoWhere() string { + var where string + var conditions []string + + // if len(cqp.Tags) > 0 { + // var tagPlaceholders []stri + // for _, tag := range cqp.Tags { + // tagPlaceholders = append(tagPlaceholders, fmt.Sprintf("'%v'", tag)) + // } + // conditions = append(conditions, "tags IN ("+strings.Join(tagPlaceholders, ",")+")") + // } + + if cqp.MinMembers != 0 { + conditions = append(conditions, fmt.Sprintf("num_members >= %d", cqp.MinMembers)) + } + if cqp.MaxMembers != 0 { + conditions = append(conditions, fmt.Sprintf("num_members <= %d", cqp.MaxMembers)) + } + if cqp.RecruitmentCycle != nil { + conditions = append(conditions, fmt.Sprintf("recruitment_cycle = '%s'", *cqp.RecruitmentCycle)) + } + if cqp.IsRecruiting != nil { + conditions = append(conditions, fmt.Sprintf("is_recruiting = %t", *cqp.IsRecruiting)) + } + + if len(conditions) > 0 { + where = strings.Join(conditions, " AND ") + } + + return where +} + func (c *Club) AfterCreate(tx *gorm.DB) (err error) { tx.Model(&c).Update("num_members", c.NumMembers+1) return diff --git a/backend/src/services/club.go b/backend/src/services/club.go index dc677196e..8244f7f5f 100644 --- a/backend/src/services/club.go +++ b/backend/src/services/club.go @@ -28,7 +28,6 @@ func NewClubService(db *gorm.DB, validate *validator.Validate) *ClubService { } func (c *ClubService) GetClubs(params *models.ClubQueryParams) ([]models.Club, *errors.Error) { - if params.Limit < 0 { return nil, &errors.FailedToValidateLimit } diff --git a/backend/src/transactions/club.go b/backend/src/transactions/club.go index 5976474c8..0aee456d0 100644 --- a/backend/src/transactions/club.go +++ b/backend/src/transactions/club.go @@ -28,15 +28,9 @@ func GetAdminIDs(db *gorm.DB, clubID uuid.UUID) ([]uuid.UUID, *errors.Error) { func GetClubs(db *gorm.DB, params *models.ClubQueryParams) ([]models.Club, *errors.Error) { offset := (*¶ms.Page - 1) * *¶ms.Limit var clubs []models.Club - //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) + + // result := db.Where(params.IntoWhere()).Limit(params.Limit).Offset(offset).Association("Tag").Find(&clubs) + result := db.Where(params.IntoWhere()).Limit(params.Limit).Offset(offset).Find(&clubs) if result.Error != nil { return nil, &errors.FailedToGetClubs }