From ab9d0e1bdecb79c62f012ea155cd58c196067c34 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Fri, 9 Jun 2023 04:30:26 -0400 Subject: [PATCH] Add groups routes --- src/api/groups.rs | 71 ++++++++++++++++++++++++++++++++++++++++++ src/api/mod.rs | 12 +++++-- src/entities/groups.rs | 3 +- 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 src/api/groups.rs diff --git a/src/api/groups.rs b/src/api/groups.rs new file mode 100644 index 0000000..94df8e3 --- /dev/null +++ b/src/api/groups.rs @@ -0,0 +1,71 @@ +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_groups( + db: &State, +) -> ApiResult>> { + Ok(Json( + Groups::find() + .all(db as &DatabaseConnection) + .await + .map_err(Error::from)?, + )) +} + +#[get("/")] +pub(super) async fn group( + db: &State, + id: i32, +) -> Result, Either> { + match Groups::find_by_id(id).one(db as &DatabaseConnection).await { + Ok(Some(group)) => Ok(Json(group)), + Ok(None) => Err(Left(Status::NotFound)), + Err(err) => Err(Right(Error::from(err).into())), + } +} + +#[post("/", format = "application/json", data = "")] +pub(super) async fn insert_group( + db: &State, + group: Json, +) -> ApiResult> { + Ok(Json( + groups::ActiveModel::from_json(group.0) + .map_err(Error::from)? + .insert(db as &DatabaseConnection) + .await + .map_err(Error::from)?, + )) +} + +#[put("/", format = "application/json", data = "")] +pub(super) async fn update_group( + db: &State, + group: Json, +) -> ApiResult> { + Ok(Json( + groups::ActiveModel::from_json(group.0) + .map_err(Error::from)? + .update(db as &DatabaseConnection) + .await + .map_err(Error::from)?, + )) +} + +#[delete("/")] +pub(super) async fn delete_group(db: &State, id: i32) -> ApiResult { + Groups::delete_by_id(id) + .exec(db as &DatabaseConnection) + .await + .map_err(Error::from)?; + Ok(Status::Ok) +} diff --git a/src/api/mod.rs b/src/api/mod.rs index 6edea8b..7080be8 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,4 +1,5 @@ mod error; +mod groups; mod ticks; mod tracks; @@ -18,6 +19,7 @@ fn status() -> &'static str { } pub(crate) fn start_server(db: DatabaseConnection) -> Rocket { + use groups::*; use ticks::*; use tracks::*; rocket::build() @@ -26,13 +28,17 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket { ..default() }) .manage(db) - .mount("/", routes![status]) + .mount("/api/v1", routes![status]) .mount( - "/tracks", + "/api/v1/tracks", routes![all_tracks, track, insert_track, update_track, delete_track], ) .mount( - "/ticks", + "/api/v1/ticks", routes![all_ticks, tick, insert_tick, update_tick, delete_tick], ) + .mount( + "/api/v1/groups", + routes![all_groups, group, insert_group, update_group, delete_group], + ) } diff --git a/src/entities/groups.rs b/src/entities/groups.rs index 374bbd2..7c9d55e 100644 --- a/src/entities/groups.rs +++ b/src/entities/groups.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 = "groups")] pub struct Model { #[sea_orm(primary_key)]