From da9c60656ced6f77a3f33eee9a41c514c5a7f9f5 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Thu, 8 Jun 2023 19:20:18 -0400 Subject: [PATCH] Add ticks routes --- src/api/mod.rs | 15 +++++++--- src/api/ticks.rs | 68 +++++++++++++++++++++++++++++++++++++++++++ src/entities/ticks.rs | 3 +- 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/api/ticks.rs diff --git a/src/api/mod.rs b/src/api/mod.rs index 605c620..6edea8b 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,5 @@ mod error; +mod ticks; mod tracks; use std::default::default; @@ -17,6 +18,8 @@ fn status() -> &'static str { } pub(crate) fn start_server(db: DatabaseConnection) -> Rocket { + use ticks::*; + use tracks::*; rocket::build() .configure(Config { address: IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), @@ -24,8 +27,12 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket { }) .manage(db) .mount("/", routes![status]) - .mount("/tracks", { - use tracks::*; - routes![all_tracks, track, insert_track, update_track, delete_track] - }) + .mount( + "/tracks", + routes![all_tracks, track, insert_track, update_track, delete_track], + ) + .mount( + "/ticks", + routes![all_ticks, tick, insert_tick, update_tick, delete_tick], + ) } diff --git a/src/api/ticks.rs b/src/api/ticks.rs new file mode 100644 index 0000000..6249f30 --- /dev/null +++ b/src/api/ticks.rs @@ -0,0 +1,68 @@ +use either::{Either, Left, Right}; +use rocket::{http::Status, serde::json::Json, State}; +use sea_orm::{prelude::*, DatabaseConnection}; + +use crate::{ + entities::{prelude::*, *}, + error::Error, +}; + +use super::error::ApiResult; + +#[get("/")] +pub(super) async fn all_ticks( + db: &State, +) -> ApiResult>> { + let db = db as &DatabaseConnection; + let ticks = Ticks::find().all(db).await.map_err(Error::from)?; + Ok(Json(ticks)) +} + +#[get("/")] +pub(super) async fn tick( + db: &State, + id: i32, +) -> Result, Either> { + match Ticks::find_by_id(id).one(db as &DatabaseConnection).await { + Ok(Some(tick)) => Ok(Json(tick)), + Ok(None) => Err(Left(Status::NotFound)), + Err(err) => Err(Right(Error::from(err).into())), + } +} + +#[post("/", format = "application/json", data = "")] +pub(super) async fn insert_tick( + db: &State, + tick: Json, +) -> ApiResult> { + Ok(Json( + ticks::ActiveModel::from_json(tick.0) + .map_err(Error::from)? + .insert(db as &DatabaseConnection) + .await + .map_err(Error::from)?, + )) +} + +#[put("/", format = "application/json", data = "")] +pub(super) async fn update_tick( + db: &State, + tick: Json, +) -> ApiResult> { + Ok(Json( + ticks::ActiveModel::from_json(tick.0) + .map_err(Error::from)? + .update(db as &DatabaseConnection) + .await + .map_err(Error::from)?, + )) +} + +#[delete("/")] +pub(super) async fn delete_tick(db: &State, id: i32) -> ApiResult { + Tracks::delete_by_id(id) + .exec(db as &DatabaseConnection) + .await + .map_err(Error::from)?; + Ok(Status::Ok) +} diff --git a/src/entities/ticks.rs b/src/entities/ticks.rs index 59de347..e2a4914 100644 --- a/src/entities/ticks.rs +++ b/src/entities/ticks.rs @@ -1,8 +1,9 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] #[sea_orm(table_name = "ticks")] pub struct Model { #[sea_orm(primary_key)]