Add groups routes

This commit is contained in:
D. Scott Boggs 2023-06-09 04:30:26 -04:00
parent da9c60656c
commit ab9d0e1bde
3 changed files with 82 additions and 4 deletions

71
src/api/groups.rs Normal file
View file

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

View file

@ -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<Build> {
use groups::*;
use ticks::*;
use tracks::*;
rocket::build()
@ -26,13 +28,17 @@ pub(crate) fn start_server(db: DatabaseConnection) -> Rocket<Build> {
..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],
)
}

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 = "groups")]
pub struct Model {
#[sea_orm(primary_key)]