Search users by username, first name, last name, and full name across all list endpoints.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -88,10 +88,7 @@ func (r *repository) ListStaff(ctx context.Context, filter user.ListFilter) (use
|
||||
|
||||
func buildStaffWhere(filter user.ListFilter) (whereClause string, args []interface{}) {
|
||||
conditions := []string{`EXISTS (SELECT 1 FROM branch_user bu WHERE bu.user_id = u.id)`}
|
||||
if filter.Username != "" {
|
||||
args = append(args, likePattern(filter.Username))
|
||||
conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, len(args)))
|
||||
}
|
||||
conditions, args = appendNameSearch(conditions, args, filter.Search)
|
||||
return " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
@@ -136,11 +133,7 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause
|
||||
fromClause = ` FROM users u `
|
||||
|
||||
var conditions []string
|
||||
if filter.Username != "" {
|
||||
args = append(args, likePattern(filter.Username))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`u.username ILIKE $%d`, n))
|
||||
}
|
||||
conditions, args = appendNameSearch(conditions, args, filter.Search)
|
||||
if len(roles) == 1 {
|
||||
args = append(args, roles[0])
|
||||
n := len(args)
|
||||
@@ -161,6 +154,19 @@ func buildFilteredListQuery(filter user.ListFilter, roles ...string) (fromClause
|
||||
return fromClause, " WHERE " + strings.Join(conditions, " AND "), args
|
||||
}
|
||||
|
||||
func appendNameSearch(conditions []string, args []interface{}, term string) ([]string, []interface{}) {
|
||||
if term == "" {
|
||||
return conditions, args
|
||||
}
|
||||
args = append(args, likePattern(term))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(
|
||||
`(u.username ILIKE $%d OR u.first_name ILIKE $%d OR u.last_name ILIKE $%d OR CONCAT_WS(' ', u.first_name, u.last_name) ILIKE $%d)`,
|
||||
n, n, n, n,
|
||||
))
|
||||
return conditions, args
|
||||
}
|
||||
|
||||
func likePattern(term string) string {
|
||||
term = strings.ReplaceAll(term, `\`, `\\`)
|
||||
term = strings.ReplaceAll(term, `%`, `\%`)
|
||||
|
||||
@@ -56,9 +56,9 @@ type UpdateUserDTO struct {
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
Username string
|
||||
Page int
|
||||
Limit int
|
||||
Search string
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ListUserDTO struct {
|
||||
|
||||
@@ -62,7 +62,8 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
// @Tags user
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param username query string false "Filter by username"
|
||||
// @Param search query string false "Search by username, first name, or last name"
|
||||
// @Param username query string false "Alias for search"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
@@ -92,7 +93,8 @@ func (h *handler) list(c *gin.Context) {
|
||||
// @Tags user
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param username query string false "Filter by username"
|
||||
// @Param search query string false "Search by username, first name, or last name"
|
||||
// @Param username query string false "Alias for search"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
@@ -122,7 +124,8 @@ func (h *handler) listClients(c *gin.Context) {
|
||||
// @Tags user
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param username query string false "Filter by username"
|
||||
// @Param search query string false "Search by username, first name, or last name"
|
||||
// @Param username query string false "Alias for search"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
@@ -316,8 +319,10 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("username")); raw != "" {
|
||||
filter.Username = raw
|
||||
if raw := strings.TrimSpace(c.Query("search")); raw != "" {
|
||||
filter.Search = raw
|
||||
} else if raw := strings.TrimSpace(c.Query("username")); raw != "" {
|
||||
filter.Search = raw
|
||||
}
|
||||
|
||||
return filter, true
|
||||
|
||||
Reference in New Issue
Block a user