From 1438ab6d24e77f0d37c902ad9b2562f82ae31a1c Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Wed, 14 Jun 2023 11:33:10 -0400 Subject: [PATCH] Add endpoint for getting ticks related to a track --- server/src/api/mod.rs | 13 ++++++++++--- server/src/api/tracks.rs | 21 ++++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index 1392b52..24123a2 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -8,11 +8,11 @@ mod tracks; use std::default::default; use std::net::{IpAddr, Ipv4Addr}; +use crate::error::Error; +use crate::rocket::{Build, Rocket}; use rocket::fs::{FileServer, NamedFile}; use rocket::{routes, Config}; use sea_orm::DatabaseConnection; -use crate::error::Error; -use crate::rocket::{Build, Rocket}; pub(crate) use error::ErrorResponder; @@ -44,7 +44,14 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket { .mount("/api/v1", routes![status]) .mount( "/api/v1/tracks", - routes![all_tracks, track, insert_track, update_track, delete_track], + routes![ + all_tracks, + track, + ticks_for_track, + insert_track, + update_track, + delete_track + ], ) .mount( "/api/v1/ticks", diff --git a/server/src/api/tracks.rs b/server/src/api/tracks.rs index 887db47..61d8121 100644 --- a/server/src/api/tracks.rs +++ b/server/src/api/tracks.rs @@ -4,7 +4,7 @@ use crate::error::Error; use either::Either::{self, Left, Right}; use rocket::http::Status; use rocket::{serde::json::Json, State}; -use sea_orm::{prelude::*, DatabaseConnection}; +use sea_orm::{prelude::*, DatabaseConnection, EntityOrSelect}; use std::default::default; #[get("/")] @@ -29,6 +29,25 @@ pub(super) async fn track( } } +#[get("//ticks")] +pub(super) async fn ticks_for_track( + db: &State, + id: i32, +) -> Result>, Either> { + let db = db as &DatabaseConnection; + match Tracks::find_by_id(id).one(db).await { + Ok(Some(track)) => { + let result = track.find_related(Ticks).all(db).await; + match result { + Ok(ticks) => Ok(Json(ticks)), + Err(err) => Err(Right(Error::from(err).into())), + } + } + Ok(None) => Err(Left(Status::NotFound)), + Err(err) => Err(Right(Error::from(err).into())), + } +} + #[post("/", format = "application/json", data = "")] pub(super) async fn insert_track( db: &State,