Skip to content

Commit

Permalink
moved functions within file for better differentiation between purpos…
Browse files Browse the repository at this point in the history
…es of functions
  • Loading branch information
karjo24 committed Sep 30, 2024
1 parent ca80508 commit 94dae04
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ func (r searchRoutes) searchCourses(c *gin.Context) {
c.JSON(http.StatusOK, responseToMap(res))
}

func getDefaultParameters(c *gin.Context) (*model.User, string, uint64) {
user := c.MustGet("TUMLiveContext").(tools.TUMLiveContext).User
query := c.Query("q")
limit, err := strconv.ParseUint(c.Query("limit"), 10, 16)
if err != nil || limit > math.MaxInt64 { // second condition should never evaluate to true (maximum bitSize for ParseUint is 16)
limit = DefaultLimit
}
return user, query, limit
}

func (r searchRoutes) search(c *gin.Context) {
user, query, limit := getDefaultParameters(c)

Expand Down Expand Up @@ -136,17 +126,6 @@ func (r searchRoutes) search(c *gin.Context) {
c.JSON(http.StatusOK, responseToMap(res))
}

func responseToMap(res *meilisearch.MultiSearchResponse) MeiliSearchMap {
msm := make(MeiliSearchMap)
if res == nil {
return msm
}
for _, r := range res.Results {
msm[r.IndexUID] = r.Hits
}
return msm
}

func semesterSearchHelper(c *gin.Context, m tools.MeiliSearchInterface, query string, limit int64, user *model.User, courseSearchOnly bool) (*meilisearch.MultiSearchResponse, error) {
var res *meilisearch.MultiSearchResponse
firstSemesterParam := c.Query("firstSemester")
Expand Down Expand Up @@ -472,6 +451,27 @@ func meiliSemesterFilter(firstSemester model.Semester, lastSemester model.Semest

// Utility functions

func getDefaultParameters(c *gin.Context) (*model.User, string, uint64) {
user := c.MustGet("TUMLiveContext").(tools.TUMLiveContext).User
query := c.Query("q")
limit, err := strconv.ParseUint(c.Query("limit"), 10, 16)
if err != nil || limit > math.MaxInt64 { // second condition should never evaluate to true (maximum bitSize for ParseUint is 16)
limit = DefaultLimit
}
return user, query, limit
}

func responseToMap(res *meilisearch.MultiSearchResponse) MeiliSearchMap {
msm := make(MeiliSearchMap)
if res == nil {
return msm
}
for _, r := range res.Results {
msm[r.IndexUID] = r.Hits
}
return msm
}

func parseSemesters(semestersParam string) ([]model.Semester, error) {
if semestersParam == "" {
return nil, errors.New("empty semestersParam")
Expand Down

0 comments on commit 94dae04

Please sign in to comment.