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:
dal 2025-01-20 13:01:02 -07:00
parent 7d83163d78
commit fdbf24e456
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 21 additions and 17 deletions

View File

@ -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