Remove unnecessary repository abstraction code

develop
cahe 5 years ago
parent 54522ba8c6
commit 993821ab77

@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use crate::base::robots;
#[derive(Serialize, Deserialize)]
pub struct GetRobot {
#[serde(skip_deserializing)]
pub id: Option<i64>,
pub name: String,
}
impl std::convert::From<robots::Robot> for GetRobot {
fn from(r: robots::Robot) -> GetRobot {
GetRobot {
id: Some(r.id),
name: r.name,
}
}
}

@ -1 +1,2 @@
pub mod forms;
pub mod robots; pub mod robots;

@ -1,39 +1,12 @@
use crate::base::robots::DefaultRepository;
use serde::{Deserialize, Serialize};
use crate::simple::database::PgSharedPool; use crate::simple::database::PgSharedPool;
use std::convert::TryInto;
use crate::app::forms::GetRobot;
use crate::base::robots; use crate::base::robots;
use crate::data::robots::postgres; use crate::data::robots::postgres;
use crate::simple::HandlingError; use crate::simple::HandlingError;
#[derive(Serialize, Deserialize)]
pub struct GetRobot {
#[serde(skip_deserializing)]
pub id: Option<i64>,
pub name: String,
}
impl std::convert::From<robots::Robot> for GetRobot {
fn from(r: robots::Robot) -> GetRobot {
GetRobot {
id: Some(r.id),
name: r.name,
}
}
}
impl std::convert::Into<robots::Robot> 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<GetRobot, HandlingError> { pub async fn add_robot(pool: PgSharedPool, r: GetRobot) -> Result<GetRobot, HandlingError> {
let mut tx = pool let mut tx = pool
.begin() .begin()
@ -41,16 +14,9 @@ pub async fn add_robot(pool: PgSharedPool, r: GetRobot) -> Result<GetRobot, Hand
.map(|c| Box::new(c)) .map(|c| Box::new(c))
.map_err(|e| HandlingError::DBError(e))?; .map_err(|e| HandlingError::DBError(e))?;
let mut repo = postgres::new(&mut tx); let model = robots::Robot::new(0, r.name).try_into()?;
let id = robots::add( let id = postgres::add(tx.as_mut(), model).await?;
&mut repo, let robot = postgres::get_by_id(tx.as_mut(), id).await?;
robots::Robot {
id: 0,
name: r.name,
},
)
.await?;
let robot = repo.get_by_id(id).await?;
tx.commit().await.map_err(|e| HandlingError::DBError(e))?; tx.commit().await.map_err(|e| HandlingError::DBError(e))?;
@ -59,13 +25,11 @@ pub async fn add_robot(pool: PgSharedPool, r: GetRobot) -> Result<GetRobot, Hand
pub async fn get_robot(pool: PgSharedPool, id: i64) -> Result<GetRobot, HandlingError> { pub async fn get_robot(pool: PgSharedPool, id: i64) -> Result<GetRobot, HandlingError> {
let mut tx = pool let mut tx = pool
.acquire() .begin()
.await .await
.map(|c| Box::new(c)) .map(|c| Box::new(c))
.map_err(|e| HandlingError::DBError(e))?; .map_err(|e| HandlingError::DBError(e))?;
Ok(postgres::get_by_id(tx.as_mut(), id).await?.into())
let mut repo = postgres::new(&mut tx);
Ok(repo.get_by_id(id).await?.into())
} }
pub async fn get_all( pub async fn get_all(
@ -98,9 +62,9 @@ pub async fn update_robot(
.map(|c| Box::new(c)) .map(|c| Box::new(c))
.map_err(|e| HandlingError::DBError(e))?; .map_err(|e| HandlingError::DBError(e))?;
let mut repo = postgres::new(&mut tx); let model = robots::Robot::new(id, r.name).try_into()?;
let _: () = robots::update(&mut repo, (id, r).into()).await?; postgres::update(tx.as_mut(), model).await?;
let robot = repo.get_by_id(id).await?; let robot = postgres::get_by_id(tx.as_mut(), id).await?;
tx.commit().await.map_err(|e| HandlingError::DBError(e))?; 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(|c| Box::new(c))
.map_err(|e| HandlingError::DBError(e))?; .map_err(|e| HandlingError::DBError(e))?;
let mut repo = postgres::new(&mut tx); postgres::delete(tx.as_mut(), id).await?;
let _: () = robots::delete(&mut repo, id).await?;
tx.commit().await.map_err(|e| HandlingError::DBError(e))?; tx.commit().await.map_err(|e| HandlingError::DBError(e))?;
Ok(()) Ok(())

@ -1,35 +1,29 @@
use crate::simple::HandlingError; use crate::simple::HandlingError;
use crate::simple::TraitFuture; use std::convert::TryFrom;
pub struct Robot { pub struct Robot {
pub id: i64, pub id: i64,
pub name: String, pub name: String,
} }
pub trait DefaultRepository<'c> { impl Robot {
fn add(&'c mut self, r: Robot) -> TraitFuture<Result<i64, HandlingError>>; pub fn new(id: i64, name: String) -> Self {
fn get_by_id(&'c mut self, id: i64) -> TraitFuture<Result<Robot, HandlingError>>; Self { id: id, name: name }
fn update(&'c mut self, r: Robot) -> TraitFuture<Result<(), HandlingError>>;
fn delete(&'c mut self, id: i64) -> TraitFuture<Result<(), HandlingError>>;
} }
pub async fn add<'c>(
repo: &'c mut impl DefaultRepository<'c>,
r: Robot,
) -> Result<i64, HandlingError> {
repo.add(r).await
} }
pub async fn update<'c>( pub struct RobotModel(Robot);
repo: &'c mut impl DefaultRepository<'c>,
r: Robot, impl TryFrom<Robot> for RobotModel {
) -> Result<(), HandlingError> { type Error = HandlingError;
repo.update(r).await
fn try_from(r: Robot) -> Result<Self, Self::Error> {
Ok(Self(r))
}
} }
pub async fn delete<'c>( impl Into<Robot> for RobotModel {
repo: &'c mut impl DefaultRepository<'c>, fn into(self) -> Robot {
id: i64, self.0
) -> Result<(), HandlingError> { }
repo.delete(id).await
} }

@ -1,5 +1,3 @@
use crate::simple::TraitFuture;
use std::boxed::Box; use std::boxed::Box;
use crate::base::robots; use crate::base::robots;
@ -79,18 +77,11 @@ where
} }
} }
pub struct RobotsImpl<'a, E> { pub async fn add(
pub tx: &'a mut Box<E>,
}
pub fn new<'a, E>(tx: &'a mut Box<E>) -> RobotsImpl<'a, E> {
RobotsImpl::<'a, E> { tx: tx }
}
async fn add(
tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>, tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
r: robots::Robot, m: robots::RobotModel,
) -> Result<i64, HandlingError> { ) -> Result<i64, HandlingError> {
let r: robots::Robot = m.into();
let result = sqlx::query("INSERT INTO robots (name) VALUES ($1::TEXT) RETURNING id") let result = sqlx::query("INSERT INTO robots (name) VALUES ($1::TEXT) RETURNING id")
.bind(r.name) .bind(r.name)
.fetch_one(tx) .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>, tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
r: robots::Robot, m: robots::RobotModel,
) -> Result<(), HandlingError> { ) -> Result<(), HandlingError> {
let r: robots::Robot = m.into();
let result = sqlx::query("UPDATE robots SET name = $1::TEXT WHERE id = $2::INTEGER") let result = sqlx::query("UPDATE robots SET name = $1::TEXT WHERE id = $2::INTEGER")
.bind(r.name) .bind(r.name)
.bind(r.id) .bind(r.id)
@ -143,7 +135,7 @@ async fn update(
} }
} }
async fn delete( pub async fn delete(
tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>, tx: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
id: i64, id: i64,
) -> Result<(), HandlingError> { ) -> Result<(), HandlingError> {
@ -160,24 +152,3 @@ async fn delete(
Err(e) => Err(HandlingError::DBError(e)), 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<Result<i64, HandlingError>> {
Box::pin(add(self.tx.as_mut(), r))
}
fn get_by_id(&'c mut self, id: i64) -> TraitFuture<Result<robots::Robot, HandlingError>> {
Box::pin(get_by_id(self.tx.as_mut(), id))
}
fn update(&'c mut self, r: robots::Robot) -> TraitFuture<Result<(), HandlingError>> {
Box::pin(update(self.tx.as_mut(), r))
}
fn delete(&'c mut self, id: i64) -> TraitFuture<Result<(), HandlingError>> {
Box::pin(delete(self.tx.as_mut(), id))
}
}

@ -23,7 +23,7 @@ struct Pagination<T> {
} }
// add a new robot entry // add a new robot entry
async fn create(pool: PgSharedPool, r: app::robots::GetRobot) -> Result<impl Reply, Rejection> { async fn create(pool: PgSharedPool, r: app::forms::GetRobot) -> Result<impl Reply, Rejection> {
reply_json(app::robots::add_robot(pool, r).await) reply_json(app::robots::add_robot(pool, r).await)
} }
@ -36,7 +36,7 @@ async fn get(pool: PgSharedPool, id: i64) -> Result<impl Reply, Rejection> {
async fn update( async fn update(
pool: PgSharedPool, pool: PgSharedPool,
id: i64, id: i64,
r: app::robots::GetRobot, r: app::forms::GetRobot,
) -> Result<impl Reply, Rejection> { ) -> Result<impl Reply, Rejection> {
reply_json(app::robots::update_robot(pool, id, r).await) reply_json(app::robots::update_robot(pool, id, r).await)
} }

@ -1,11 +1,6 @@
use std::future::Future;
use std::pin::Pin;
pub mod database; pub mod database;
pub mod http; pub mod http;
pub type TraitFuture<'f, T> = Pin<Box<dyn Future<Output = T> + Send + 'f>>;
#[derive(Debug)] #[derive(Debug)]
pub enum HandlingError { pub enum HandlingError {
DBError(sqlx::Error), DBError(sqlx::Error),

Loading…
Cancel
Save