Add ticks routes

This commit is contained in:
D. Scott Boggs 2023-06-08 19:20:18 -04:00
parent 3203a3fd70
commit da9c60656c
3 changed files with 81 additions and 5 deletions

View file

@ -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<Build> {
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<Build> {
})
.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],
)
}

68
src/api/ticks.rs Normal file
View file

@ -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<DatabaseConnection>,
) -> ApiResult<Json<Vec<ticks::Model>>> {
let db = db as &DatabaseConnection;
let ticks = Ticks::find().all(db).await.map_err(Error::from)?;
Ok(Json(ticks))
}
#[get("/<id>")]
pub(super) async fn tick(
db: &State<DatabaseConnection>,
id: i32,
) -> Result<Json<ticks::Model>, Either<Status, super::ErrorResponder>> {
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 = "<tick>")]
pub(super) async fn insert_tick(
db: &State<DatabaseConnection>,
tick: Json<serde_json::Value>,
) -> ApiResult<Json<ticks::Model>> {
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 = "<tick>")]
pub(super) async fn update_tick(
db: &State<DatabaseConnection>,
tick: Json<serde_json::Value>,
) -> ApiResult<Json<ticks::Model>> {
Ok(Json(
ticks::ActiveModel::from_json(tick.0)
.map_err(Error::from)?
.update(db as &DatabaseConnection)
.await
.map_err(Error::from)?,
))
}
#[delete("/<id>")]
pub(super) async fn delete_tick(db: &State<DatabaseConnection>, id: i32) -> ApiResult<Status> {
Tracks::delete_by_id(id)
.exec(db as &DatabaseConnection)
.await
.map_err(Error::from)?;
Ok(Status::Ok)
}

View file

@ -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)]