UPDATE: Something with module search
All checks were successful
Build and Release / release (push) Successful in 1m25s
All checks were successful
Build and Release / release (push) Successful in 1m25s
This commit is contained in:
@@ -15,12 +15,41 @@ SELECT *
|
|||||||
FROM medias
|
FROM medias
|
||||||
WHERE
|
WHERE
|
||||||
(sqlc.narg('cursor')::uuid IS NULL OR id > sqlc.narg('cursor')::uuid)
|
(sqlc.narg('cursor')::uuid IS NULL OR id > sqlc.narg('cursor')::uuid)
|
||||||
|
|
||||||
AND (
|
AND (
|
||||||
sqlc.narg('search_text')::text IS NULL OR
|
sqlc.narg('search_text')::text IS NULL OR
|
||||||
original_name ILIKE '%' || sqlc.narg('search_text')::text || '%' OR
|
original_name ILIKE '%' || sqlc.narg('search_text')::text || '%' OR
|
||||||
storage_key ILIKE '%' || sqlc.narg('search_text')::text || '%'
|
storage_key ILIKE '%' || sqlc.narg('search_text')::text || '%'
|
||||||
)
|
)
|
||||||
ORDER BY id ASC
|
|
||||||
|
ORDER BY
|
||||||
|
-- id
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'id' AND sqlc.narg('order') = 'asc' THEN id
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'id' AND sqlc.narg('order') = 'desc' THEN id
|
||||||
|
END DESC,
|
||||||
|
|
||||||
|
-- created_at
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'created_at' AND sqlc.narg('order') = 'asc' THEN created_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'created_at' AND sqlc.narg('order') = 'desc' THEN created_at
|
||||||
|
END DESC,
|
||||||
|
|
||||||
|
-- updated_at
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'updated_at' AND sqlc.narg('order') = 'asc' THEN updated_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'updated_at' AND sqlc.narg('order') = 'desc' THEN updated_at
|
||||||
|
END DESC,
|
||||||
|
|
||||||
|
-- fallback
|
||||||
|
id ASC
|
||||||
|
|
||||||
LIMIT sqlc.arg('limit');
|
LIMIT sqlc.arg('limit');
|
||||||
|
|
||||||
-- name: GetMediasByUserID :many
|
-- name: GetMediasByUserID :many
|
||||||
|
|||||||
@@ -195,59 +195,6 @@ SELECT
|
|||||||
FROM users u
|
FROM users u
|
||||||
WHERE u.email = $1 AND u.is_deleted = false;
|
WHERE u.email = $1 AND u.is_deleted = false;
|
||||||
|
|
||||||
-- name: GetUsers :many
|
|
||||||
SELECT
|
|
||||||
u.id,
|
|
||||||
u.email,
|
|
||||||
u.password_hash,
|
|
||||||
u.token_version,
|
|
||||||
u.refresh_token,
|
|
||||||
u.is_deleted,
|
|
||||||
u.created_at,
|
|
||||||
u.updated_at,
|
|
||||||
|
|
||||||
-- profile JSON
|
|
||||||
(
|
|
||||||
SELECT json_build_object(
|
|
||||||
'display_name', p.display_name,
|
|
||||||
'full_name', p.full_name,
|
|
||||||
'avatar_url', p.avatar_url,
|
|
||||||
'bio', p.bio,
|
|
||||||
'location', p.location,
|
|
||||||
'website', p.website,
|
|
||||||
'country_code', p.country_code,
|
|
||||||
'phone', p.phone
|
|
||||||
)
|
|
||||||
FROM user_profiles p
|
|
||||||
WHERE p.user_id = u.id
|
|
||||||
) AS profile,
|
|
||||||
|
|
||||||
-- roles JSON
|
|
||||||
(
|
|
||||||
SELECT COALESCE(
|
|
||||||
json_agg(json_build_object('id', r.id, 'name', r.name)),
|
|
||||||
'[]'
|
|
||||||
)::json
|
|
||||||
FROM user_roles ur
|
|
||||||
JOIN roles r ON ur.role_id = r.id
|
|
||||||
WHERE ur.user_id = u.id
|
|
||||||
) AS roles
|
|
||||||
|
|
||||||
FROM users u
|
|
||||||
WHERE
|
|
||||||
(sqlc.narg('cursor')::uuid IS NULL OR u.id > sqlc.narg('cursor')::uuid)
|
|
||||||
AND (sqlc.narg('is_deleted')::boolean IS NULL OR u.is_deleted = sqlc.narg('is_deleted')::boolean)
|
|
||||||
AND (
|
|
||||||
sqlc.narg('role_ids')::uuid[] IS NULL OR
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1 FROM user_roles ur2
|
|
||||||
WHERE ur2.user_id = u.id AND ur2.role_id = ANY(sqlc.narg('role_ids')::uuid[])
|
|
||||||
)
|
|
||||||
)
|
|
||||||
ORDER BY u.id ASC
|
|
||||||
LIMIT sqlc.arg('limit');
|
|
||||||
|
|
||||||
|
|
||||||
-- name: SearchUsers :many
|
-- name: SearchUsers :many
|
||||||
SELECT
|
SELECT
|
||||||
u.id,
|
u.id,
|
||||||
@@ -285,26 +232,51 @@ SELECT
|
|||||||
) AS roles
|
) AS roles
|
||||||
|
|
||||||
FROM users u
|
FROM users u
|
||||||
|
|
||||||
WHERE
|
WHERE
|
||||||
(sqlc.narg('cursor')::uuid IS NULL OR u.id > sqlc.narg('cursor')::uuid)
|
(sqlc.narg('cursor')::uuid IS NULL OR u.id > sqlc.narg('cursor')::uuid)
|
||||||
|
|
||||||
AND (sqlc.narg('is_deleted')::boolean IS NULL OR u.is_deleted = sqlc.narg('is_deleted')::boolean)
|
AND (sqlc.narg('is_deleted')::boolean IS NULL OR u.is_deleted = sqlc.narg('is_deleted')::boolean)
|
||||||
|
|
||||||
AND (
|
AND (
|
||||||
sqlc.narg('role_ids')::uuid[] IS NULL OR
|
sqlc.narg('role_ids')::uuid[] IS NULL OR
|
||||||
EXISTS (
|
EXISTS (
|
||||||
SELECT 1 FROM user_roles ur2
|
SELECT 1 FROM user_roles ur2
|
||||||
WHERE ur2.user_id = u.id AND ur2.role_id = ANY(sqlc.narg('role_ids')::uuid[])
|
WHERE ur2.user_id = u.id
|
||||||
|
AND ur2.role_id = ANY(sqlc.narg('role_ids')::uuid[])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
AND (sqlc.narg('search_id')::uuid IS NULL OR u.id = sqlc.narg('search_id')::uuid)
|
AND (sqlc.narg('search_id')::uuid IS NULL OR u.id = sqlc.narg('search_id')::uuid)
|
||||||
|
|
||||||
AND (
|
AND (
|
||||||
sqlc.narg('search_text')::text IS NULL OR
|
sqlc.narg('search_text')::text IS NULL OR
|
||||||
u.email ILIKE '%' || sqlc.narg('search_text')::text || '%' OR
|
u.email ILIKE '%' || sqlc.narg('search_text')::text || '%'
|
||||||
EXISTS (
|
|
||||||
SELECT 1 FROM user_profiles p
|
|
||||||
WHERE p.user_id = u.id AND p.display_name ILIKE '%' || sqlc.narg('search_text')::text || '%'
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
ORDER BY u.id ASC
|
|
||||||
|
ORDER BY
|
||||||
|
-- id
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'id' AND sqlc.narg('order') = 'asc' THEN id
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'id' AND sqlc.narg('order') = 'desc' THEN id
|
||||||
|
END DESC,
|
||||||
|
-- created_at
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'created_at' AND sqlc.narg('order') = 'asc' THEN u.created_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'created_at' AND sqlc.narg('order') = 'desc' THEN u.created_at
|
||||||
|
END DESC,
|
||||||
|
-- updated_at
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'updated_at' AND sqlc.narg('order') = 'asc' THEN u.updated_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN sqlc.narg('sort') = 'updated_at' AND sqlc.narg('order') = 'desc' THEN u.updated_at
|
||||||
|
END DESC,
|
||||||
|
-- fallback
|
||||||
|
u.id ASC
|
||||||
|
|
||||||
LIMIT sqlc.arg('limit');
|
LIMIT sqlc.arg('limit');
|
||||||
@@ -875,10 +875,9 @@ const docTemplate = `{
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"enum": [
|
"enum": [
|
||||||
|
"id",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at"
|
||||||
"email",
|
|
||||||
"display_name"
|
|
||||||
],
|
],
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"name": "sort",
|
"name": "sort",
|
||||||
|
|||||||
@@ -868,10 +868,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"enum": [
|
"enum": [
|
||||||
|
"id",
|
||||||
"created_at",
|
"created_at",
|
||||||
"updated_at",
|
"updated_at"
|
||||||
"email",
|
|
||||||
"display_name"
|
|
||||||
],
|
],
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"name": "sort",
|
"name": "sort",
|
||||||
|
|||||||
@@ -738,10 +738,9 @@ paths:
|
|||||||
name: search
|
name: search
|
||||||
type: string
|
type: string
|
||||||
- enum:
|
- enum:
|
||||||
|
- id
|
||||||
- created_at
|
- created_at
|
||||||
- updated_at
|
- updated_at
|
||||||
- email
|
|
||||||
- display_name
|
|
||||||
in: query
|
in: query
|
||||||
name: sort
|
name: sort
|
||||||
type: string
|
type: string
|
||||||
|
|||||||
@@ -30,10 +30,9 @@ type GetAllUserDto struct {
|
|||||||
type CursorPaginationDto struct {
|
type CursorPaginationDto struct {
|
||||||
Cursor string `json:"cursor" query:"cursor" validate:"omitempty,uuid"`
|
Cursor string `json:"cursor" query:"cursor" validate:"omitempty,uuid"`
|
||||||
Limit int `json:"limit" query:"limit" validate:"required,min=1,max=100"`
|
Limit int `json:"limit" query:"limit" validate:"required,min=1,max=100"`
|
||||||
Sort string `json:"sort" query:"sort" validate:"omitempty,oneof=created_at updated_at email display_name"`
|
Sort string `json:"sort" query:"sort" validate:"omitempty,oneof=id created_at updated_at"`
|
||||||
Order string `json:"order" query:"order" validate:"omitempty,oneof=asc desc"`
|
Order string `json:"order" query:"order" validate:"omitempty,oneof=asc desc"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SearchUserDto struct {
|
type SearchUserDto struct {
|
||||||
CursorPaginationDto
|
CursorPaginationDto
|
||||||
Search string `json:"search" query:"search" validate:"omitempty,min=2,max=200"`
|
Search string `json:"search" query:"search" validate:"omitempty,min=2,max=200"`
|
||||||
|
|||||||
@@ -126,23 +126,60 @@ SELECT id, user_id, storage_key, original_name, mime_type, size, file_metadata,
|
|||||||
FROM medias
|
FROM medias
|
||||||
WHERE
|
WHERE
|
||||||
($1::uuid IS NULL OR id > $1::uuid)
|
($1::uuid IS NULL OR id > $1::uuid)
|
||||||
|
|
||||||
AND (
|
AND (
|
||||||
$2::text IS NULL OR
|
$2::text IS NULL OR
|
||||||
original_name ILIKE '%' || $2::text || '%' OR
|
original_name ILIKE '%' || $2::text || '%' OR
|
||||||
storage_key ILIKE '%' || $2::text || '%'
|
storage_key ILIKE '%' || $2::text || '%'
|
||||||
)
|
)
|
||||||
ORDER BY id ASC
|
|
||||||
LIMIT $3
|
ORDER BY
|
||||||
|
-- id
|
||||||
|
CASE
|
||||||
|
WHEN $3 = 'id' AND $4 = 'asc' THEN id
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN $3 = 'id' AND $4 = 'desc' THEN id
|
||||||
|
END DESC,
|
||||||
|
|
||||||
|
-- created_at
|
||||||
|
CASE
|
||||||
|
WHEN $3 = 'created_at' AND $4 = 'asc' THEN created_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN $3 = 'created_at' AND $4 = 'desc' THEN created_at
|
||||||
|
END DESC,
|
||||||
|
|
||||||
|
-- updated_at
|
||||||
|
CASE
|
||||||
|
WHEN $3 = 'updated_at' AND $4 = 'asc' THEN updated_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN $3 = 'updated_at' AND $4 = 'desc' THEN updated_at
|
||||||
|
END DESC,
|
||||||
|
|
||||||
|
-- fallback
|
||||||
|
id ASC
|
||||||
|
|
||||||
|
LIMIT $5
|
||||||
`
|
`
|
||||||
|
|
||||||
type SearchMediasParams struct {
|
type SearchMediasParams struct {
|
||||||
Cursor pgtype.UUID `json:"cursor"`
|
Cursor pgtype.UUID `json:"cursor"`
|
||||||
SearchText pgtype.Text `json:"search_text"`
|
SearchText pgtype.Text `json:"search_text"`
|
||||||
|
Sort interface{} `json:"sort"`
|
||||||
|
Order interface{} `json:"order"`
|
||||||
Limit int32 `json:"limit"`
|
Limit int32 `json:"limit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) SearchMedias(ctx context.Context, arg SearchMediasParams) ([]Media, error) {
|
func (q *Queries) SearchMedias(ctx context.Context, arg SearchMediasParams) ([]Media, error) {
|
||||||
rows, err := q.db.Query(ctx, searchMedias, arg.Cursor, arg.SearchText, arg.Limit)
|
rows, err := q.db.Query(ctx, searchMedias,
|
||||||
|
arg.Cursor,
|
||||||
|
arg.SearchText,
|
||||||
|
arg.Sort,
|
||||||
|
arg.Order,
|
||||||
|
arg.Limit,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -286,115 +286,6 @@ func (q *Queries) GetUserByIDWithoutDeleted(ctx context.Context, id pgtype.UUID)
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUsers = `-- name: GetUsers :many
|
|
||||||
SELECT
|
|
||||||
u.id,
|
|
||||||
u.email,
|
|
||||||
u.password_hash,
|
|
||||||
u.token_version,
|
|
||||||
u.refresh_token,
|
|
||||||
u.is_deleted,
|
|
||||||
u.created_at,
|
|
||||||
u.updated_at,
|
|
||||||
|
|
||||||
-- profile JSON
|
|
||||||
(
|
|
||||||
SELECT json_build_object(
|
|
||||||
'display_name', p.display_name,
|
|
||||||
'full_name', p.full_name,
|
|
||||||
'avatar_url', p.avatar_url,
|
|
||||||
'bio', p.bio,
|
|
||||||
'location', p.location,
|
|
||||||
'website', p.website,
|
|
||||||
'country_code', p.country_code,
|
|
||||||
'phone', p.phone
|
|
||||||
)
|
|
||||||
FROM user_profiles p
|
|
||||||
WHERE p.user_id = u.id
|
|
||||||
) AS profile,
|
|
||||||
|
|
||||||
-- roles JSON
|
|
||||||
(
|
|
||||||
SELECT COALESCE(
|
|
||||||
json_agg(json_build_object('id', r.id, 'name', r.name)),
|
|
||||||
'[]'
|
|
||||||
)::json
|
|
||||||
FROM user_roles ur
|
|
||||||
JOIN roles r ON ur.role_id = r.id
|
|
||||||
WHERE ur.user_id = u.id
|
|
||||||
) AS roles
|
|
||||||
|
|
||||||
FROM users u
|
|
||||||
WHERE
|
|
||||||
($1::uuid IS NULL OR u.id > $1::uuid)
|
|
||||||
AND ($2::boolean IS NULL OR u.is_deleted = $2::boolean)
|
|
||||||
AND (
|
|
||||||
$3::uuid[] IS NULL OR
|
|
||||||
EXISTS (
|
|
||||||
SELECT 1 FROM user_roles ur2
|
|
||||||
WHERE ur2.user_id = u.id AND ur2.role_id = ANY($3::uuid[])
|
|
||||||
)
|
|
||||||
)
|
|
||||||
ORDER BY u.id ASC
|
|
||||||
LIMIT $4
|
|
||||||
`
|
|
||||||
|
|
||||||
type GetUsersParams struct {
|
|
||||||
Cursor pgtype.UUID `json:"cursor"`
|
|
||||||
IsDeleted pgtype.Bool `json:"is_deleted"`
|
|
||||||
RoleIds []pgtype.UUID `json:"role_ids"`
|
|
||||||
Limit int32 `json:"limit"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type GetUsersRow struct {
|
|
||||||
ID pgtype.UUID `json:"id"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
PasswordHash pgtype.Text `json:"password_hash"`
|
|
||||||
TokenVersion int32 `json:"token_version"`
|
|
||||||
RefreshToken pgtype.Text `json:"refresh_token"`
|
|
||||||
IsDeleted bool `json:"is_deleted"`
|
|
||||||
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
||||||
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
||||||
Profile []byte `json:"profile"`
|
|
||||||
Roles []byte `json:"roles"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *Queries) GetUsers(ctx context.Context, arg GetUsersParams) ([]GetUsersRow, error) {
|
|
||||||
rows, err := q.db.Query(ctx, getUsers,
|
|
||||||
arg.Cursor,
|
|
||||||
arg.IsDeleted,
|
|
||||||
arg.RoleIds,
|
|
||||||
arg.Limit,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer rows.Close()
|
|
||||||
items := []GetUsersRow{}
|
|
||||||
for rows.Next() {
|
|
||||||
var i GetUsersRow
|
|
||||||
if err := rows.Scan(
|
|
||||||
&i.ID,
|
|
||||||
&i.Email,
|
|
||||||
&i.PasswordHash,
|
|
||||||
&i.TokenVersion,
|
|
||||||
&i.RefreshToken,
|
|
||||||
&i.IsDeleted,
|
|
||||||
&i.CreatedAt,
|
|
||||||
&i.UpdatedAt,
|
|
||||||
&i.Profile,
|
|
||||||
&i.Roles,
|
|
||||||
); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
items = append(items, i)
|
|
||||||
}
|
|
||||||
if err := rows.Err(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return items, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
const restoreUser = `-- name: RestoreUser :exec
|
const restoreUser = `-- name: RestoreUser :exec
|
||||||
UPDATE users
|
UPDATE users
|
||||||
SET
|
SET
|
||||||
@@ -444,29 +335,54 @@ SELECT
|
|||||||
) AS roles
|
) AS roles
|
||||||
|
|
||||||
FROM users u
|
FROM users u
|
||||||
|
|
||||||
WHERE
|
WHERE
|
||||||
($1::uuid IS NULL OR u.id > $1::uuid)
|
($1::uuid IS NULL OR u.id > $1::uuid)
|
||||||
|
|
||||||
AND ($2::boolean IS NULL OR u.is_deleted = $2::boolean)
|
AND ($2::boolean IS NULL OR u.is_deleted = $2::boolean)
|
||||||
|
|
||||||
AND (
|
AND (
|
||||||
$3::uuid[] IS NULL OR
|
$3::uuid[] IS NULL OR
|
||||||
EXISTS (
|
EXISTS (
|
||||||
SELECT 1 FROM user_roles ur2
|
SELECT 1 FROM user_roles ur2
|
||||||
WHERE ur2.user_id = u.id AND ur2.role_id = ANY($3::uuid[])
|
WHERE ur2.user_id = u.id
|
||||||
|
AND ur2.role_id = ANY($3::uuid[])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
AND ($4::uuid IS NULL OR u.id = $4::uuid)
|
AND ($4::uuid IS NULL OR u.id = $4::uuid)
|
||||||
|
|
||||||
AND (
|
AND (
|
||||||
$5::text IS NULL OR
|
$5::text IS NULL OR
|
||||||
u.email ILIKE '%' || $5::text || '%' OR
|
u.email ILIKE '%' || $5::text || '%'
|
||||||
EXISTS (
|
|
||||||
SELECT 1 FROM user_profiles p
|
|
||||||
WHERE p.user_id = u.id AND p.display_name ILIKE '%' || $5::text || '%'
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
ORDER BY u.id ASC
|
|
||||||
LIMIT $6
|
ORDER BY
|
||||||
|
-- id
|
||||||
|
CASE
|
||||||
|
WHEN $6 = 'id' AND $7 = 'asc' THEN id
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN $6 = 'id' AND $7 = 'desc' THEN id
|
||||||
|
END DESC,
|
||||||
|
-- created_at
|
||||||
|
CASE
|
||||||
|
WHEN $6 = 'created_at' AND $7 = 'asc' THEN u.created_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN $6 = 'created_at' AND $7 = 'desc' THEN u.created_at
|
||||||
|
END DESC,
|
||||||
|
-- updated_at
|
||||||
|
CASE
|
||||||
|
WHEN $6 = 'updated_at' AND $7 = 'asc' THEN u.updated_at
|
||||||
|
END ASC,
|
||||||
|
CASE
|
||||||
|
WHEN $6 = 'updated_at' AND $7 = 'desc' THEN u.updated_at
|
||||||
|
END DESC,
|
||||||
|
-- fallback
|
||||||
|
u.id ASC
|
||||||
|
|
||||||
|
LIMIT $8
|
||||||
`
|
`
|
||||||
|
|
||||||
type SearchUsersParams struct {
|
type SearchUsersParams struct {
|
||||||
@@ -475,6 +391,8 @@ type SearchUsersParams struct {
|
|||||||
RoleIds []pgtype.UUID `json:"role_ids"`
|
RoleIds []pgtype.UUID `json:"role_ids"`
|
||||||
SearchID pgtype.UUID `json:"search_id"`
|
SearchID pgtype.UUID `json:"search_id"`
|
||||||
SearchText pgtype.Text `json:"search_text"`
|
SearchText pgtype.Text `json:"search_text"`
|
||||||
|
Sort interface{} `json:"sort"`
|
||||||
|
Order interface{} `json:"order"`
|
||||||
Limit int32 `json:"limit"`
|
Limit int32 `json:"limit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -498,6 +416,8 @@ func (q *Queries) SearchUsers(ctx context.Context, arg SearchUsersParams) ([]Sea
|
|||||||
arg.RoleIds,
|
arg.RoleIds,
|
||||||
arg.SearchID,
|
arg.SearchID,
|
||||||
arg.SearchText,
|
arg.SearchText,
|
||||||
|
arg.Sort,
|
||||||
|
arg.Order,
|
||||||
arg.Limit,
|
arg.Limit,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ type UserRepository interface {
|
|||||||
GetByID(ctx context.Context, id pgtype.UUID) (*models.UserEntity, error)
|
GetByID(ctx context.Context, id pgtype.UUID) (*models.UserEntity, error)
|
||||||
GetByIDWithoutDeleted(ctx context.Context, id pgtype.UUID) (*models.UserEntity, error)
|
GetByIDWithoutDeleted(ctx context.Context, id pgtype.UUID) (*models.UserEntity, error)
|
||||||
GetByEmail(ctx context.Context, email string) (*models.UserEntity, error)
|
GetByEmail(ctx context.Context, email string) (*models.UserEntity, error)
|
||||||
All(ctx context.Context, params sqlc.GetUsersParams) ([]*models.UserEntity, error)
|
|
||||||
Search(ctx context.Context, params sqlc.SearchUsersParams) ([]*models.UserEntity, error)
|
Search(ctx context.Context, params sqlc.SearchUsersParams) ([]*models.UserEntity, error)
|
||||||
UpsertUser(ctx context.Context, params sqlc.UpsertUserParams) (*models.UserEntity, error)
|
UpsertUser(ctx context.Context, params sqlc.UpsertUserParams) (*models.UserEntity, error)
|
||||||
CreateProfile(ctx context.Context, params sqlc.CreateUserProfileParams) (*models.UserProfileSimple, error)
|
CreateProfile(ctx context.Context, params sqlc.CreateUserProfileParams) (*models.UserProfileSimple, error)
|
||||||
@@ -271,57 +270,6 @@ func (r *userRepository) CreateProfile(ctx context.Context, params sqlc.CreateUs
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *userRepository) All(ctx context.Context, params sqlc.GetUsersParams) ([]*models.UserEntity, error) {
|
|
||||||
queryKey := r.generateQueryKey("user:all", params)
|
|
||||||
|
|
||||||
var cachedIDs []string
|
|
||||||
if err := r.c.Get(ctx, queryKey, &cachedIDs); err == nil && len(cachedIDs) > 0 {
|
|
||||||
return r.getByIDsWithFallback(ctx, cachedIDs)
|
|
||||||
}
|
|
||||||
rows, err := r.q.GetUsers(ctx, params)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var users []*models.UserEntity
|
|
||||||
var ids []string
|
|
||||||
usersToCache := make(map[string]any)
|
|
||||||
|
|
||||||
for _, row := range rows {
|
|
||||||
user := &models.UserEntity{
|
|
||||||
ID: convert.UUIDToString(row.ID),
|
|
||||||
Email: row.Email,
|
|
||||||
PasswordHash: convert.TextToString(row.PasswordHash),
|
|
||||||
TokenVersion: row.TokenVersion,
|
|
||||||
IsDeleted: row.IsDeleted,
|
|
||||||
CreatedAt: convert.TimeToPtr(row.CreatedAt),
|
|
||||||
UpdatedAt: convert.TimeToPtr(row.UpdatedAt),
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := user.ParseRoles(row.Roles); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := user.ParseProfile(row.Profile); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
users = append(users, user)
|
|
||||||
ids = append(ids, user.ID)
|
|
||||||
|
|
||||||
usersToCache[fmt.Sprintf("user:id:%s", user.ID)] = user
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(usersToCache) > 0 {
|
|
||||||
_ = r.c.MSet(ctx, usersToCache, constants.NormalCacheDuration)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ids) > 0 {
|
|
||||||
_ = r.c.Set(ctx, queryKey, ids, constants.ListCacheDuration)
|
|
||||||
}
|
|
||||||
|
|
||||||
return users, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *userRepository) Search(ctx context.Context, params sqlc.SearchUsersParams) ([]*models.UserEntity, error) {
|
func (r *userRepository) Search(ctx context.Context, params sqlc.SearchUsersParams) ([]*models.UserEntity, error) {
|
||||||
queryKey := r.generateQueryKey("user:search", params)
|
queryKey := r.generateQueryKey("user:search", params)
|
||||||
|
|
||||||
|
|||||||
@@ -116,6 +116,18 @@ func (m *mediaService) SearchMedia(ctx context.Context, dto *request.SearchMedia
|
|||||||
Limit: int32(dto.Limit + 1),
|
Limit: int32(dto.Limit + 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dto.Sort != "" {
|
||||||
|
arg.Sort = pgtype.Text{String: dto.Sort, Valid: true}
|
||||||
|
} else {
|
||||||
|
arg.Sort = pgtype.Text{String: "id", Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
if dto.Order != "" {
|
||||||
|
arg.Order = pgtype.Text{String: dto.Order, Valid: true}
|
||||||
|
} else {
|
||||||
|
arg.Order = pgtype.Text{String: "asc", Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
if dto.Cursor != "" {
|
if dto.Cursor != "" {
|
||||||
pgID, err := convert.StringToUUID(dto.Cursor)
|
pgID, err := convert.StringToUUID(dto.Cursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -213,6 +213,18 @@ func (u *userService) SearchUser(ctx context.Context, dto *request.SearchUserDto
|
|||||||
Limit: int32(dto.Limit + 1),
|
Limit: int32(dto.Limit + 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dto.Sort != "" {
|
||||||
|
arg.Sort = pgtype.Text{String: dto.Sort, Valid: true}
|
||||||
|
} else {
|
||||||
|
arg.Sort = pgtype.Text{String: "id", Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
if dto.Order != "" {
|
||||||
|
arg.Order = pgtype.Text{String: dto.Order, Valid: true}
|
||||||
|
} else {
|
||||||
|
arg.Order = pgtype.Text{String: "asc", Valid: true}
|
||||||
|
}
|
||||||
|
|
||||||
if dto.Cursor != "" {
|
if dto.Cursor != "" {
|
||||||
pgID, err := convert.StringToUUID(dto.Cursor)
|
pgID, err := convert.StringToUUID(dto.Cursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -259,11 +271,14 @@ func (u *userService) SearchUser(ctx context.Context, dto *request.SearchUserDto
|
|||||||
rows = rows[:dto.Limit]
|
rows = rows[:dto.Limit]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
users := models.UsersEntityToResponse(rows)
|
||||||
|
|
||||||
res := &response.PaginatedResponse{
|
res := &response.PaginatedResponse{
|
||||||
Data: rows,
|
Data: users,
|
||||||
Status: true,
|
Status: true,
|
||||||
Message: "",
|
Message: "",
|
||||||
}
|
}
|
||||||
|
|
||||||
res.Pagination.HasMore = hasMore
|
res.Pagination.HasMore = hasMore
|
||||||
res.Pagination.NextCursor = nextCursor
|
res.Pagination.NextCursor = nextCursor
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user