mirror of
https://github.com/usememos/memos.git
synced 2025-02-19 12:50:41 +01:00
chore: implement list random users
This commit is contained in:
parent
2fe6d606ec
commit
03c93785f4
@ -2,6 +2,8 @@ package mysql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -96,7 +98,15 @@ func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User
|
|||||||
where, args = append(where, "`nickname` = ?"), append(args, *v)
|
where, args = append(where, "`nickname` = ?"), append(args, *v)
|
||||||
}
|
}
|
||||||
|
|
||||||
query := "SELECT `id`, `username`, `role`, `email`, `nickname`, `password_hash`, `avatar_url`, `description`, UNIX_TIMESTAMP(`created_ts`), UNIX_TIMESTAMP(`updated_ts`), `row_status` FROM `user` WHERE " + strings.Join(where, " AND ") + " ORDER BY `created_ts` DESC, `row_status` DESC"
|
orderBy := []string{"`created_ts` DESC", "`row_status` DESC"}
|
||||||
|
if find.Random {
|
||||||
|
orderBy = slices.Concat([]string{"RAND()"}, orderBy)
|
||||||
|
}
|
||||||
|
|
||||||
|
query := "SELECT `id`, `username`, `role`, `email`, `nickname`, `password_hash`, `avatar_url`, `description`, UNIX_TIMESTAMP(`created_ts`), UNIX_TIMESTAMP(`updated_ts`), `row_status` FROM `user` WHERE " + strings.Join(where, " AND ") + " ORDER BY " + strings.Join(orderBy, ", ")
|
||||||
|
if v := find.Limit; v != nil {
|
||||||
|
query += fmt.Sprintf(" LIMIT %d", *v)
|
||||||
|
}
|
||||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -2,6 +2,8 @@ package postgres
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
@ -98,6 +100,11 @@ func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User
|
|||||||
where, args = append(where, "nickname = "+placeholder(len(args)+1)), append(args, *v)
|
where, args = append(where, "nickname = "+placeholder(len(args)+1)), append(args, *v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
orderBy := []string{"created_ts DESC", "row_status DESC"}
|
||||||
|
if find.Random {
|
||||||
|
orderBy = slices.Concat([]string{"RANDOM()"}, orderBy)
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
@ -112,9 +119,10 @@ func (d *DB) ListUsers(ctx context.Context, find *store.FindUser) ([]*store.User
|
|||||||
updated_ts,
|
updated_ts,
|
||||||
row_status
|
row_status
|
||||||
FROM "user"
|
FROM "user"
|
||||||
WHERE ` + strings.Join(where, " AND ") + `
|
WHERE ` + strings.Join(where, " AND ") + ` ORDER BY ` + strings.Join(orderBy, ", ")
|
||||||
ORDER BY created_ts DESC, row_status DESC
|
if v := find.Limit; v != nil {
|
||||||
`
|
query += fmt.Sprintf(" LIMIT %d", *v)
|
||||||
|
}
|
||||||
rows, err := d.db.QueryContext(ctx, query, args...)
|
rows, err := d.db.QueryContext(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user