Skip to content

Commit

Permalink
Merge branch 'SAC-32-Natural-Language-Search-to-Club-GET' into Club-S…
Browse files Browse the repository at this point in the history
…earch-(SAC31+SAC32)
  • Loading branch information
michael-brennan2005 committed Feb 27, 2024
2 parents ef4b700 + fe49fb5 commit 1924477
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
24 changes: 24 additions & 0 deletions backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@ type ClubQueryParams struct {
IsRecruiting *bool `query:"is_recruiting"`
Limit int `query:"limit"`
Page int `query:"page"`
Search string `query:"search"`
}

type ClubSearch struct {
SearchString string `query:"search"`
}

func NewClubSearch(searchQuery string) *ClubSearch {
return &ClubSearch{
SearchString: searchQuery,
}
}

// dummy searchID
func (cs *ClubSearch) SearchId() string {
return ""
}

func (cs *ClubSearch) Namespace() string {
return "clubs"
}

func (cs *ClubSearch) EmbeddingString() string {
return cs.SearchString
}

func (cqp *ClubQueryParams) IntoWhere() string {
Expand Down
11 changes: 6 additions & 5 deletions backend/src/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ func (a *AuthService) Login(userBody models.LoginUserResponseBody) (*models.User
return nil, &errors.FailedToValidateUser
}

user, err := transactions.GetUserByEmail(a.DB, userBody.Email)
if err != nil {
return nil, err
user, getUserByEmailErr := transactions.GetUserByEmail(a.DB, userBody.Email)
if getUserByEmailErr != nil {
return nil, getUserByEmailErr
}

correct, passwordErr := auth.ComparePasswordAndHash(userBody.Password, user.PasswordHash)
if passwordErr != nil || !correct {

Check failure on line 56 in backend/src/services/auth.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
correct, err := auth.ComparePasswordAndHash(userBody.Password, user.PasswordHash)
if err != nil || !correct {
return nil, &errors.FailedToValidateUser
}

Expand Down
3 changes: 2 additions & 1 deletion backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type ClubServiceInterface interface {
}

type ClubService struct {
pineconeClient *search.PineconeClient
DB *gorm.DB

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

View workflow job for this annotation

GitHub Actions / Lint

File is not `goimports`-ed (goimports)
Pinecone search.PineconeClientInterface
Validate *validator.Validate
Expand All @@ -38,7 +39,7 @@ func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams) ([]models.Cl
return nil, &errors.FailedToValidatePage
}

return transactions.GetClubs(c.DB, queryParams)
return transactions.GetClubs(c.DB, queryParams, *c.pineconeClient)
}

func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models.Club, *errors.Error) {
Expand Down
19 changes: 15 additions & 4 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package transactions
import (
stdliberrors "errors"

"github.com/GenerateNU/sac/backend/src/search"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/search"

"github.com/google/uuid"
"gorm.io/gorm"
Expand All @@ -26,7 +27,7 @@ func GetAdminIDs(db *gorm.DB, clubID uuid.UUID) ([]uuid.UUID, *errors.Error) {
return adminUUIDs, nil
}

func GetClubs(db *gorm.DB, queryParams *models.ClubQueryParams) ([]models.Club, *errors.Error) {
func GetClubs(db *gorm.DB, queryParams *models.ClubQueryParams, pineconeClient search.PineconeClient) ([]models.Club, *errors.Error) {
query := db.Model(&models.Club{})

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
Expand All @@ -39,8 +40,18 @@ func GetClubs(db *gorm.DB, queryParams *models.ClubQueryParams) ([]models.Club,

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
Where("club_tags.tag_id IN ?", queryParams.Tags). // add search function here
Group("clubs.id") // ensure unique club records
}

if queryParams.Search != "" {
clubSearch := models.NewClubSearch(queryParams.Search)
resultIDs, err := pineconeClient.Search(clubSearch, 10)
if err != nil {
return nil, &errors.FailedToSearchToPinecone
}

query = query.Where("id IN ?", resultIDs)
}

var clubs []models.Club
Expand Down

0 comments on commit 1924477

Please sign in to comment.