Skip to content

Commit

Permalink
clean up | format | lint
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 17, 2024
1 parent 0e8526c commit 44ed6a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
13 changes: 8 additions & 5 deletions backend/src/controllers/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ 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 {

var queryParams models.ClubQueryParams

queryParams.Limit = defaultLimit
queryParams.Page = defaultPage

if err := c.QueryParser(queryParams); err != nil {
return err
}

clubs, err := cl.clubService.GetClubs(p)
clubs, err := cl.clubService.GetClubs(&queryParams)
if err != nil {
return err.FiberError(c)
}
Expand Down
20 changes: 4 additions & 16 deletions backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,7 @@ type ClubQueryParams struct {
}

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, ",")+")")
// }
conditions := make([]string, 0)

if cqp.MinMembers != 0 {
conditions = append(conditions, fmt.Sprintf("num_members >= %d", cqp.MinMembers))
Expand All @@ -117,13 +108,10 @@ func (cqp *ClubQueryParams) IntoWhere() string {
conditions = append(conditions, fmt.Sprintf("is_recruiting = %t", *cqp.IsRecruiting))
}

// conditions = append(conditions, fmt.Sprintf("'clubs'.'club_tags' = 8761310a-4e1a-4cc4-b9c9-799dd9950e07"))

if len(conditions) > 0 {
where = strings.Join(conditions, " AND ")
if len(conditions) == 0 {
return ""
}

return where
return "WHERE " + strings.Join(conditions, " AND ")
}

func (c *Club) AfterCreate(tx *gorm.DB) (err error) {
Expand Down
10 changes: 5 additions & 5 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(params *models.ClubQueryParams) ([]models.Club, *errors.Error)
GetClubs(queryParams *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,16 +27,16 @@ func NewClubService(db *gorm.DB, validate *validator.Validate) *ClubService {
return &ClubService{DB: db, Validate: validate}
}

func (c *ClubService) GetClubs(params *models.ClubQueryParams) ([]models.Club, *errors.Error) {
if params.Limit < 0 {
func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams) ([]models.Club, *errors.Error) {
if queryParams.Limit < 0 {
return nil, &errors.FailedToValidateLimit
}

if params.Page < 0 {
if queryParams.Page < 0 {
return nil, &errors.FailedToValidatePage
}

return transactions.GetClubs(c.DB, params)
return transactions.GetClubs(c.DB, queryParams)
}

func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, *errors.Error) {
Expand Down
30 changes: 18 additions & 12 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@ func GetAdminIDs(db *gorm.DB, clubID uuid.UUID) ([]uuid.UUID, *errors.Error) {
return adminUUIDs, nil
}

func GetClubs(db *gorm.DB, params *models.ClubQueryParams) ([]models.Club, *errors.Error) {
offset := (*&params.Page - 1) * *&params.Limit
var clubs []models.Club
func GetClubs(db *gorm.DB, queryParams *models.ClubQueryParams) ([]models.Club, *errors.Error) {
// Initialize the base query
query := db.Model(&models.Club{})

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
query = query.Preload("Tags")
}

db_call := db
if params.Tags != nil {
//We only want to query tags if necessary
//i.e. when we filter
db_call = db.Preload("Tag")
for key, value := range queryParams.IntoWhere() {
query = query.Where(key, value)
}
query := db_call.Where(params.IntoWhere())
if params.Tags != nil {
query = query.Where("id IN (SELECT club_id FROM club_tags WHERE tag_id IN ?)", params.Tags)

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
query = query.Joins("JOIN club_tags ON club_tags.club_id = clubs.id").
Where("club_tags.tag_id IN ?", queryParams.Tags).
Group("clubs.id") // ensure unique club records
}
result := query.Limit(params.Limit).Offset(offset).Find(&clubs)

var clubs []models.Club
offset := (queryParams.Page - 1) * queryParams.Limit

result := query.Limit(queryParams.Limit).Offset(offset).Find(&clubs)
if result.Error != nil {
return nil, &errors.FailedToGetClubs
}
Expand Down

0 comments on commit 44ed6a8

Please sign in to comment.