first commit
This commit is contained in:
commit
2a1854ab08
120 files changed
+11300
No files matched your search
@@ -0,0 +1,22 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::types::Json as SqlxJson;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ListRoutesQuery {
|
||||
pub group: Option<String>,
|
||||
pub province: Option<i16>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, sqlx::FromRow)]
|
||||
pub struct RouteListRow {
|
||||
pub route_id: String,
|
||||
pub name_th: String,
|
||||
pub vehicle_type: String,
|
||||
pub route_group_id: i16,
|
||||
pub route_group_code: String,
|
||||
pub route_group_label_th: String,
|
||||
pub contact: SqlxJson<serde_json::Value>,
|
||||
pub metadata: SqlxJson<serde_json::Value>,
|
||||
pub province_ids: Vec<i16>,
|
||||
pub images: SqlxJson<serde_json::Value>,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
use super::{dto::ListRoutesQuery, service};
|
||||
use crate::{error::AppResult, state::AppState};
|
||||
use axum::{extract::{Query, State}, routing::get, Json, Router};
|
||||
|
||||
pub fn route() -> Router<AppState> {
|
||||
Router::new().route("/api/routes", get(handler))
|
||||
}
|
||||
|
||||
async fn handler(
|
||||
State(state): State<AppState>,
|
||||
Query(q): Query<ListRoutesQuery>,
|
||||
) -> AppResult<Json<serde_json::Value>> {
|
||||
let rows = service::handle(&state, &q).await?;
|
||||
Ok(Json(serde_json::json!({ "items": rows })))
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod dto;
|
||||
pub mod handler;
|
||||
pub mod repo;
|
||||
pub mod service;
|
||||
pub use handler::route;
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use super::{dto::*, repo};
|
||||
use crate::{error::AppResult, state::AppState};
|
||||
|
||||
pub async fn handle(state: &AppState, q: &ListRoutesQuery) -> AppResult<Vec<RouteListRow>> {
|
||||
Ok(repo::list(&state.db, q).await?)
|
||||
}
|
||||
Reference in new issue
Block a user