Skip to content

Commit

Permalink
Started working on filtering
Browse files Browse the repository at this point in the history
Co-authored-by: Melody Yu <[email protected]>
  • Loading branch information
zacklassetter and Melody Yu committed Feb 8, 2024
1 parent dcd59f4 commit d6af301
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
10 changes: 7 additions & 3 deletions backend/src/controllers/club.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion backend/src/server/routes/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
2 changes: 1 addition & 1 deletion backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 6 additions & 9 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {

Check failure on line 30 in backend/src/services/club.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)

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) {
Expand Down
13 changes: 11 additions & 2 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := (*&params.Page - 1) * *&params.Limit

Check failure on line 29 in backend/src/transactions/club.go

View workflow job for this annotation

GitHub Actions / Lint

SA4001: *&x will be simplified to x. It will not copy x. (staticcheck)
var clubs []models.Club
result := db.Limit(limit).Offset(offset).Find(&clubs)
//TODO try with mapper function in manipulator.go

Check failure on line 31 in backend/src/transactions/club.go

View workflow job for this annotation

GitHub Actions / Lint

commentFormatting: put a space between `//` and comment text (gocritic)
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
}
Expand Down

0 comments on commit d6af301

Please sign in to comment.