mirror of https://github.com/buster-so/buster.git
Refactor SQL query in list_assets_handler to use a Common Table Expression (CTE) for improved readability and maintainability. The CTE, `distinct_assets`, simplifies the selection of distinct asset records before applying the final ordering and limiting.
This commit is contained in:
parent
7d83163d78
commit
fdbf24e456
|
@ -162,23 +162,27 @@ pub async fn list_assets_handler(
|
|||
|
||||
let query = format!(
|
||||
r#"
|
||||
SELECT DISTINCT ON (content, asset_type)
|
||||
asset_search.asset_id,
|
||||
asset_search.content,
|
||||
asset_search.updated_at,
|
||||
asset_search.asset_type
|
||||
FROM
|
||||
asset_search
|
||||
INNER JOIN
|
||||
asset_permissions
|
||||
ON
|
||||
asset_search.asset_id = asset_permissions.asset_id
|
||||
WHERE
|
||||
asset_search.asset_type IN ({})
|
||||
AND (asset_permissions.identity_id = '{}')
|
||||
AND asset_search.deleted_at IS NULL
|
||||
AND asset_permissions.deleted_at IS NULL
|
||||
ORDER BY asset_search.content, asset_search.asset_type, asset_search.updated_at DESC
|
||||
WITH distinct_assets AS (
|
||||
SELECT DISTINCT ON (content, asset_type)
|
||||
asset_search.asset_id,
|
||||
asset_search.content,
|
||||
asset_search.updated_at,
|
||||
asset_search.asset_type
|
||||
FROM
|
||||
asset_search
|
||||
INNER JOIN
|
||||
asset_permissions
|
||||
ON
|
||||
asset_search.asset_id = asset_permissions.asset_id
|
||||
WHERE
|
||||
asset_search.asset_type IN ({})
|
||||
AND (asset_permissions.identity_id = '{}')
|
||||
AND asset_search.deleted_at IS NULL
|
||||
AND asset_permissions.deleted_at IS NULL
|
||||
)
|
||||
SELECT *
|
||||
FROM distinct_assets
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT {};
|
||||
"#,
|
||||
asset_types
|
||||
|
|
Loading…
Reference in New Issue