Skip to content

Commit

Permalink
Update users.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kordianbruck authored Jan 10, 2025
1 parent 869703e commit 6d0c3cc
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,22 @@ func (r usersRoutes) updateUser(c *gin.Context) {
}

func (r usersRoutes) prepareUserSearch(c *gin.Context) (users []model.User, err error) {
q := c.Query("q")
rQ := c.Query("r")
query := c.Query("q")
roleQuery := c.Query("r")
reg := regexp.MustCompile("[^a-zA-Z0-9 ]+")
q = reg.ReplaceAllString(q, "")
query = reg.ReplaceAllString(query, "")
// make the search work with empty query but selected role
if len(q) < 3 && (rQ == "-1" || rQ == "") {
if len(query) < 3 && (roleQuery == "-1" || roleQuery == "") {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
CustomMessage: "query too short (minimum length is 3)",
})
return nil, errors.New("query too short (minimum length is 3)")
}
if rQ == "" || rQ == "-1" {
users, err = r.UsersDao.SearchUser(q)
if roleQuery == "" || roleQuery == "-1" {
users, err = r.UsersDao.SearchUser(query)
} else {
role, err := strconv.ParseUint(rQ, 10, 64)
role, err := strconv.ParseUint(roleQuery, 10, 64)
if err != nil {
_ = c.Error(tools.RequestError{
Status: http.StatusBadRequest,
Expand All @@ -149,12 +149,12 @@ func (r usersRoutes) prepareUserSearch(c *gin.Context) (users []model.User, err
})
return nil, err
}
users, err = r.UsersDao.SearchUserWithRole(q, role)
users, err = r.UsersDao.SearchUserWithRole(query, role)
}
if err != nil && err != gorm.ErrRecordNotFound {
_ = c.Error(tools.RequestError{
Status: http.StatusInternalServerError,
CustomMessage: "can not search user",
CustomMessage: "cannot search for user's",
Err: err,
})
return nil, err
Expand Down

0 comments on commit 6d0c3cc

Please sign in to comment.