diff --git a/src/app/forms.rs b/src/app/forms.rs new file mode 100644 index 0000000..fde36b9 --- /dev/null +++ b/src/app/forms.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; + +use crate::base::robots; + +#[derive(Serialize, Deserialize)] +pub struct GetRobot { + #[serde(skip_deserializing)] + pub id: Option, + pub name: String, +} + +impl std::convert::From for GetRobot { + fn from(r: robots::Robot) -> GetRobot { + GetRobot { + id: Some(r.id), + name: r.name, + } + } +} diff --git a/src/app/mod.rs b/src/app/mod.rs index ee09d7f..a208e4d 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1 +1,2 @@ +pub mod forms; pub mod robots; diff --git a/src/app/robots.rs b/src/app/robots.rs index e730168..35a8cec 100644 --- a/src/app/robots.rs +++ b/src/app/robots.rs @@ -1,39 +1,12 @@ -use crate::base::robots::DefaultRepository; - -use serde::{Deserialize, Serialize}; - use crate::simple::database::PgSharedPool; +use std::convert::TryInto; +use crate::app::forms::GetRobot; use crate::base::robots; use crate::data::robots::postgres; use crate::simple::HandlingError; -#[derive(Serialize, Deserialize)] -pub struct GetRobot { - #[serde(skip_deserializing)] - pub id: Option, - pub name: String, -} - -impl std::convert::From for GetRobot { - fn from(r: robots::Robot) -> GetRobot { - GetRobot { - id: Some(r.id), - name: r.name, - } - } -} - -impl std::convert::Into for (i64, GetRobot) { - fn into(self) -> robots::Robot { - robots::Robot { - id: self.0, - name: self.1.name, - } - } -} - pub async fn add_robot(pool: PgSharedPool, r: GetRobot) -> Result { let mut tx = pool .begin() @@ -41,16 +14,9 @@ pub async fn add_robot(pool: PgSharedPool, r: GetRobot) -> Result Result Result { let mut tx = pool - .acquire() + .begin() .await .map(|c| Box::new(c)) .map_err(|e| HandlingError::DBError(e))?; - - let mut repo = postgres::new(&mut tx); - Ok(repo.get_by_id(id).await?.into()) + Ok(postgres::get_by_id(tx.as_mut(), id).await?.into()) } pub async fn get_all( @@ -98,9 +62,9 @@ pub async fn update_robot( .map(|c| Box::new(c)) .map_err(|e| HandlingError::DBError(e))?; - let mut repo = postgres::new(&mut tx); - let _: () = robots::update(&mut repo, (id, r).into()).await?; - let robot = repo.get_by_id(id).await?; + let model = robots::Robot::new(id, r.name).try_into()?; + postgres::update(tx.as_mut(), model).await?; + let robot = postgres::get_by_id(tx.as_mut(), id).await?; tx.commit().await.map_err(|e| HandlingError::DBError(e))?; @@ -114,9 +78,7 @@ pub async fn delete_robot(pool: PgSharedPool, id: i64) -> Result<(), HandlingErr .map(|c| Box::new(c)) .map_err(|e| HandlingError::DBError(e))?; - let mut repo = postgres::new(&mut tx); - let _: () = robots::delete(&mut repo, id).await?; - + postgres::delete(tx.as_mut(), id).await?; tx.commit().await.map_err(|e| HandlingError::DBError(e))?; Ok(()) diff --git a/src/base/robots.rs b/src/base/robots.rs index bba534d..a9000d5 100644 --- a/src/base/robots.rs +++ b/src/base/robots.rs @@ -1,35 +1,29 @@ use crate::simple::HandlingError; -use crate::simple::TraitFuture; +use std::convert::TryFrom; pub struct Robot { pub id: i64, pub name: String, } -pub trait DefaultRepository<'c> { - fn add(&'c mut self, r: Robot) -> TraitFuture>; - fn get_by_id(&'c mut self, id: i64) -> TraitFuture>; - fn update(&'c mut self, r: Robot) -> TraitFuture>; - fn delete(&'c mut self, id: i64) -> TraitFuture>; +impl Robot { + pub fn new(id: i64, name: String) -> Self { + Self { id: id, name: name } + } } -pub async fn add<'c>( - repo: &'c mut impl DefaultRepository<'c>, - r: Robot, -) -> Result { - repo.add(r).await -} +pub struct RobotModel(Robot); + +impl TryFrom for RobotModel { + type Error = HandlingError; -pub async fn update<'c>( - repo: &'c mut impl DefaultRepository<'c>, - r: Robot, -) -> Result<(), HandlingError> { - repo.update(r).await + fn try_from(r: Robot) -> Result { + Ok(Self(r)) + } } -pub async fn delete<'c>( - repo: &'c mut impl DefaultRepository<'c>, - id: i64, -) -> Result<(), HandlingError> { - repo.delete(id).await +impl Into for RobotModel { + fn into(self) -> Robot { + self.0 + } } diff --git a/src/data/robots/postgres.rs b/src/data/robots/postgres.rs index 6403128..ab56e12 100644 --- a/src/data/robots/postgres.rs +++ b/src/data/robots/postgres.rs @@ -1,5 +1,3 @@ -use crate::simple::TraitFuture; - use std::boxed::Box; use crate::base::robots; @@ -79,18 +77,11 @@ where } } -pub struct RobotsImpl<'a, E> { - pub tx: &'a mut Box, -} - -pub fn new<'a, E>(tx: &'a mut Box) -> RobotsImpl<'a, E> { - RobotsImpl::<'a, E> { tx: tx } -} - -async fn add( +pub async fn add( tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>, - r: robots::Robot, + m: robots::RobotModel, ) -> Result { + let r: robots::Robot = m.into(); let result = sqlx::query("INSERT INTO robots (name) VALUES ($1::TEXT) RETURNING id") .bind(r.name) .fetch_one(tx) @@ -123,10 +114,11 @@ pub async fn get_by_id( } } -async fn update( +pub async fn update( tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>, - r: robots::Robot, + m: robots::RobotModel, ) -> Result<(), HandlingError> { + let r: robots::Robot = m.into(); let result = sqlx::query("UPDATE robots SET name = $1::TEXT WHERE id = $2::INTEGER") .bind(r.name) .bind(r.id) @@ -143,7 +135,7 @@ async fn update( } } -async fn delete( +pub async fn delete( tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>, id: i64, ) -> Result<(), HandlingError> { @@ -160,24 +152,3 @@ async fn delete( Err(e) => Err(HandlingError::DBError(e)), } } - -impl<'a, 'c, E> robots::DefaultRepository<'c> for RobotsImpl<'a, E> -where - &'c mut E: sqlx::Executor<'c, Database = sqlx::Postgres> + 'c, -{ - fn add(&'c mut self, r: robots::Robot) -> TraitFuture> { - Box::pin(add(self.tx.as_mut(), r)) - } - - fn get_by_id(&'c mut self, id: i64) -> TraitFuture> { - Box::pin(get_by_id(self.tx.as_mut(), id)) - } - - fn update(&'c mut self, r: robots::Robot) -> TraitFuture> { - Box::pin(update(self.tx.as_mut(), r)) - } - - fn delete(&'c mut self, id: i64) -> TraitFuture> { - Box::pin(delete(self.tx.as_mut(), id)) - } -} diff --git a/src/http/robots.rs b/src/http/robots.rs index dd15acb..54cb544 100644 --- a/src/http/robots.rs +++ b/src/http/robots.rs @@ -23,7 +23,7 @@ struct Pagination { } // add a new robot entry -async fn create(pool: PgSharedPool, r: app::robots::GetRobot) -> Result { +async fn create(pool: PgSharedPool, r: app::forms::GetRobot) -> Result { reply_json(app::robots::add_robot(pool, r).await) } @@ -36,7 +36,7 @@ async fn get(pool: PgSharedPool, id: i64) -> Result { async fn update( pool: PgSharedPool, id: i64, - r: app::robots::GetRobot, + r: app::forms::GetRobot, ) -> Result { reply_json(app::robots::update_robot(pool, id, r).await) } diff --git a/src/simple/mod.rs b/src/simple/mod.rs index 0819bf6..dfc7f6f 100644 --- a/src/simple/mod.rs +++ b/src/simple/mod.rs @@ -1,11 +1,6 @@ -use std::future::Future; -use std::pin::Pin; - pub mod database; pub mod http; -pub type TraitFuture<'f, T> = Pin + Send + 'f>>; - #[derive(Debug)] pub enum HandlingError { DBError(sqlx::Error),