Skip to content

Commit

Permalink
feat: implemented Min/MaxMembers filtering, refactored querying
Browse files Browse the repository at this point in the history
  • Loading branch information
yu-melody committed Feb 11, 2024
1 parent 6d1bb43 commit ef90252
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
39 changes: 38 additions & 1 deletion backend/src/models/club.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package models

import (
"fmt"
"strings"

"github.com/google/uuid"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 3 additions & 9 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := (*&params.Page - 1) * *&params.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
}
Expand Down

0 comments on commit ef90252

Please sign in to comment.