Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-brennan2005 committed Jun 17, 2024
1 parent ebe56e8 commit 80e66a3
Showing 1 changed file with 23 additions and 60 deletions.
83 changes: 23 additions & 60 deletions backend/search/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,25 @@ package base

import (
"fmt"
"strings"
"time"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/search/types"
"gorm.io/gorm"
)

// Use this over strings.join so elements can be single quoted if nessecary.
func joinQuoted(elems []string, seperator string) string {
elemsQuoted := make([]string, len(elems))

for i, elem := range elems {
elemsQuoted[i] = fmt.Sprintf("'%s'", elem)
}

return strings.Join(elemsQuoted, seperator)
}

func SearchClubs(db *gorm.DB, query *types.ClubSearchRequest) (*types.SearchResult[models.Club], error) {
var clubs []models.Club

Expand Down Expand Up @@ -37,29 +49,14 @@ func SearchClubs(db *gorm.DB, query *types.ClubSearchRequest) (*types.SearchResu

if len(query.Tags) != 0 {
innerJoin = "INNER JOIN club_tags ON clubs.id = club_tags.club_id"
concatenatedTags := ""
for i, tag := range query.Tags {
concatenatedTags += fmt.Sprintf("'%s'", tag)

if i < len(query.Tags)-1 {
concatenatedTags += ","
}
}

whereClauses = append(whereClauses,
fmt.Sprintf("club_tags.tag_id IN (%s)", concatenatedTags))
fmt.Sprintf("club_tags.tag_id IN (%s)", joinQuoted(query.Tags, ",")))
}

whereClausesJoined := ""
for i, clause := range whereClauses {
whereClausesJoined += clause

if i < len(whereClauses)-1 {
whereClausesJoined += " AND "
}
}

finalQuery := fmt.Sprintf("SELECT * FROM clubs %s WHERE %s", innerJoin, whereClausesJoined)
finalQuery := fmt.Sprintf(
"SELECT * FROM clubs %s WHERE %s",
innerJoin,
strings.Join(whereClauses, " AND "))

if err := dbQuery.Raw(finalQuery).Scan(&clubs).Error; err != nil {
return nil, err
Expand Down Expand Up @@ -99,54 +96,20 @@ func SearchEvents(db *gorm.DB, query *types.EventSearchRequest) (*types.SearchRe

if len(query.Tags) != 0 {
innerJoins = append(innerJoins, "INNER JOIN event_tags ON events.id = event_tags.event_id")
concatenatedTags := ""
for i, tag := range query.Tags {
concatenatedTags += fmt.Sprintf("'%s'", tag)

if i < len(query.Tags)-1 {
concatenatedTags += ","
}
}

whereClauses = append(whereClauses,
fmt.Sprintf("event_tags.tag_id IN (%s)", concatenatedTags))
fmt.Sprintf("event_tags.tag_id IN (%s)", joinQuoted(query.Tags, ",")))
}

if len(query.Clubs) != 0 {
innerJoins = append(innerJoins, "INNER JOIN club_events ON events.id = club_events.event_id")
concatenatedClubs := ""
for i, tag := range query.Clubs {
concatenatedClubs += fmt.Sprintf("'%s'", tag)

if i < len(query.Clubs)-1 {
concatenatedClubs += ","
}
}

whereClauses = append(whereClauses,
fmt.Sprintf("club_events.club_id IN (%s)", concatenatedClubs))
}

whereClausesJoined := ""
for i, clause := range whereClauses {
whereClausesJoined += clause

if i < len(whereClauses)-1 {
whereClausesJoined += " AND "
}
}

innerJoinsJoined := ""

for i, innerJoin := range innerJoins {
innerJoinsJoined += innerJoin

if i < len(innerJoins)-1 {
innerJoinsJoined += " "
}
fmt.Sprintf("club_events.club_id IN (%s)", joinQuoted(query.Clubs, ",")))
}

finalQuery := fmt.Sprintf("SELECT * FROM events %s WHERE %s", innerJoinsJoined, whereClausesJoined)
finalQuery := fmt.Sprintf(
"SELECT * FROM events %s WHERE %s",
strings.Join(innerJoins, " "),
strings.Join(whereClauses, " AND "))

if err := dbQuery.Raw(finalQuery).Scan(&events).Error; err != nil {
return nil, err
Expand Down

0 comments on commit 80e66a3

Please sign in to comment.