Skip to content

Commit

Permalink
work on bus trip api
Browse files Browse the repository at this point in the history
  • Loading branch information
jonerrr committed Jul 8, 2024
1 parent 0eeb4b4 commit d608004
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 88 deletions.

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ async fn main() {
.route("/bus/stops", get(routes::bus::stops::get))
.route("/bus/stops/times", get(routes::bus::stops::times))
.route("/bus/trips", get(routes::bus::trips::get))
.route("/bus/trips/:id", get(routes::bus::trips::by_id))
.route("/bus/routes", get(routes::bus::routes::get))
.layer(TraceLayer::new_for_http())
.layer(CompressionLayer::new())
Expand Down
67 changes: 65 additions & 2 deletions backend/src/routes/bus/trips.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::stops::Parameters;
use crate::routes::{errors::ServerError, CurrentTime};
use axum::{
extract::{Query, State},
extract::{Path, Query, State},
response::IntoResponse,
Json,
};
use chrono::Utc;
use serde::Serialize;
use sqlx::{FromRow, PgPool};
use sqlx::{types::JsonValue, FromRow, PgPool};
use uuid::Uuid;

#[derive(FromRow, Serialize)]
Expand Down Expand Up @@ -87,3 +87,66 @@ WHERE
}

//

#[derive(FromRow, Serialize)]
pub struct BusTripData {
id: Uuid,
route_id: String,
direction: i16,
vehicle_id: i32,
deviation: Option<i32>,
created_at: chrono::DateTime<Utc>,
headsign: Option<String>,
stop_times: Option<JsonValue>,
}

pub async fn by_id(
State(pool): State<PgPool>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, ServerError> {
let trip = sqlx::query_as!(
BusTripData,
r#"SELECT
t.id,
t.route_id,
t.direction,
t.vehicle_id,
t.created_at,
t.deviation,
jsonb_agg(jsonb_build_object('stop_id',
bst.stop_id,
'arrival',
bst.arrival,
'departure',
bst.departure,
'stop_sequence',
bst.stop_sequence)
ORDER BY
bst.arrival) AS stop_times,
(
SELECT
brs.headsign
FROM
bus_route_stops brs
WHERE
brs.route_id = t.route_id
AND brs.direction = t.direction
LIMIT 1) AS headsign
FROM
bus_trips t
LEFT JOIN bus_stop_times bst ON
t.id = bst.trip_id
WHERE
t.id = $1
GROUP BY
t.id"#,
id
)
.fetch_optional(&pool)
.await?;

match trip {
Some(trip) => Ok(Json(trip)),
None => Err(ServerError::NotFound),
}
}
3 changes: 3 additions & 0 deletions backend/src/routes/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use serde_json::json;
pub enum ServerError {
#[error("{0}")]
Database(#[from] sqlx::Error),
#[error("Not found")]
NotFound,
}

impl IntoResponse for ServerError {
Expand All @@ -21,6 +23,7 @@ impl IntoResponse for ServerError {
StatusCode::INTERNAL_SERVER_ERROR,
"Database error".to_string(),
),
ServerError::NotFound => (StatusCode::NOT_FOUND, "Not found".to_string()),
};

let body = Json(json!({ "message": message }));
Expand Down
10 changes: 6 additions & 4 deletions backend/src/routes/trips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use axum::{
};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use sqlx::types::JsonValue;
use sqlx::{FromRow, PgPool};
use sqlx::{types::JsonValue, FromRow, PgPool};
use uuid::Uuid;

#[derive(FromRow, Serialize)]
Expand Down Expand Up @@ -114,8 +113,11 @@ pub async fn by_id(
t.id"#,
id
)
.fetch_one(&pool)
.fetch_optional(&pool)
.await?;

Ok(Json(trip))
match trip {
Some(trip) => Ok(Json(trip)),
None => Err(ServerError::NotFound),
}
}

0 comments on commit d608004

Please sign in to comment.