first commit
This commit is contained in:
commit
2a1854ab08
120 files changed
+11300
No files matched your search
@@ -0,0 +1,36 @@
|
||||
use super::dto::{ListRoutesQuery, RouteListRow};
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub async fn list(pool: &PgPool, q: &ListRoutesQuery) -> Result<Vec<RouteListRow>, sqlx::Error> {
|
||||
let sql = r#"
|
||||
SELECT r.route_id,
|
||||
r.name_th,
|
||||
r.vehicle_type::text AS vehicle_type,
|
||||
r.route_group_id,
|
||||
g.code AS route_group_code,
|
||||
g.label_th AS route_group_label_th,
|
||||
r.contact,
|
||||
r.metadata,
|
||||
COALESCE(ARRAY(SELECT province_id FROM route_provinces rp WHERE rp.route_id = r.route_id ORDER BY province_id), '{}') AS province_ids,
|
||||
COALESCE((
|
||||
SELECT jsonb_agg(jsonb_build_object(
|
||||
'id', i.id, 'kind', i.kind, 'url_path', i.url_path,
|
||||
'display_order', i.display_order, 'meta', i.meta
|
||||
) ORDER BY i.display_order)
|
||||
FROM images i
|
||||
WHERE i.entity_type = 'route' AND i.entity_id = r.route_id
|
||||
), '[]'::jsonb) AS images
|
||||
FROM routes r
|
||||
JOIN route_groups g ON g.id = r.route_group_id
|
||||
WHERE ($1::text IS NULL OR g.code = $1)
|
||||
AND ($2::smallint IS NULL OR EXISTS (
|
||||
SELECT 1 FROM route_provinces rp
|
||||
WHERE rp.route_id = r.route_id AND rp.province_id = $2))
|
||||
ORDER BY g.display_order, r.route_id
|
||||
"#;
|
||||
sqlx::query_as::<_, RouteListRow>(sql)
|
||||
.bind(q.group.as_deref())
|
||||
.bind(q.province)
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
Reference in new issue
Block a user