first commit
This commit is contained in:
commit
2a1854ab08
120 files changed
+11300
No files matched your search
@@ -0,0 +1,18 @@
|
||||
use serde::Serialize;
|
||||
use sqlx::types::Json as SqlxJson;
|
||||
|
||||
#[derive(Debug, Serialize, sqlx::FromRow)]
|
||||
pub struct StopDetail {
|
||||
pub id: i32,
|
||||
pub code: String,
|
||||
pub name_th: String,
|
||||
pub city_th: Option<String>,
|
||||
pub province_th: Option<String>,
|
||||
pub station_type: String,
|
||||
pub attributes: SqlxJson<serde_json::Value>,
|
||||
pub metadata: SqlxJson<serde_json::Value>,
|
||||
pub images: SqlxJson<serde_json::Value>,
|
||||
pub routes: SqlxJson<serde_json::Value>,
|
||||
pub lng: Option<f64>,
|
||||
pub lat: Option<f64>,
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
use super::service;
|
||||
use crate::{error::AppResult, state::AppState};
|
||||
use axum::{extract::{Path, State}, routing::get, Json, Router};
|
||||
|
||||
pub fn route() -> Router<AppState> {
|
||||
Router::new().route("/api/stops/:stop_code", get(handler))
|
||||
}
|
||||
|
||||
async fn handler(
|
||||
State(state): State<AppState>,
|
||||
Path(code): Path<String>,
|
||||
) -> AppResult<Json<serde_json::Value>> {
|
||||
let row = service::handle(&state, &code).await?;
|
||||
Ok(Json(serde_json::to_value(row).unwrap()))
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
pub mod dto;
|
||||
pub mod handler;
|
||||
pub mod repo;
|
||||
pub mod service;
|
||||
pub use handler::route;
|
||||
@@ -0,0 +1,38 @@
|
||||
use super::dto::StopDetail;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub async fn fetch(pool: &PgPool, code: &str) -> Result<Option<StopDetail>, sqlx::Error> {
|
||||
let sql = r#"
|
||||
SELECT s.id, s.code, s.name_th, s.city_th, s.province_th,
|
||||
s.station_type::text AS station_type,
|
||||
s.attributes, s.metadata,
|
||||
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 = 'station' AND i.entity_id = s.code
|
||||
), '[]'::jsonb) AS images,
|
||||
COALESCE((
|
||||
SELECT jsonb_agg(jsonb_build_object(
|
||||
'route_id', r.route_id,
|
||||
'name_th', r.name_th,
|
||||
'vehicle_type', r.vehicle_type::text,
|
||||
'route_group_code', g.code,
|
||||
'route_group_label_th', g.label_th
|
||||
) ORDER BY rs.stop_sequence, r.route_id)
|
||||
FROM route_stops rs
|
||||
JOIN routes r ON r.route_id = rs.route_id
|
||||
JOIN route_groups g ON g.id = r.route_group_id
|
||||
WHERE rs.station_id = s.id
|
||||
), '[]'::jsonb) AS routes,
|
||||
s.lng, s.lat
|
||||
FROM stations s
|
||||
WHERE s.code = $1
|
||||
"#;
|
||||
sqlx::query_as::<_, StopDetail>(sql)
|
||||
.bind(code)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
use super::{dto::StopDetail, repo};
|
||||
use crate::{error::{AppError, AppResult}, state::AppState};
|
||||
|
||||
pub async fn handle(state: &AppState, code: &str) -> AppResult<StopDetail> {
|
||||
repo::fetch(&state.db, code).await?.ok_or(AppError::NotFound)
|
||||
}
|
||||
Reference in new issue
Block a user