Add endpoint for getting ticks related to a track

This commit is contained in:
D. Scott Boggs 2023-06-14 11:33:10 -04:00
parent 76cb73bb03
commit 1438ab6d24
2 changed files with 30 additions and 4 deletions

View file

@ -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<Build> {
.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",

View file

@ -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("/<id>/ticks")]
pub(super) async fn ticks_for_track(
db: &State<DatabaseConnection>,
id: i32,
) -> Result<Json<Vec<ticks::Model>>, Either<Status, api::ErrorResponder>> {
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 = "<track>")]
pub(super) async fn insert_track(
db: &State<DatabaseConnection>,