|
|
|
|
@ -1,31 +1,30 @@
|
|
|
|
|
use crate::simple::TraitFuture;
|
|
|
|
|
use std::boxed::Box;
|
|
|
|
|
use std::pin::Pin;
|
|
|
|
|
|
|
|
|
|
use crate::simple::handling::{HandlingError};
|
|
|
|
|
use crate::base::robots;
|
|
|
|
|
use crate::simple::HandlingError;
|
|
|
|
|
|
|
|
|
|
use std::future::Future;
|
|
|
|
|
|
|
|
|
|
use sqlx::FromRow;
|
|
|
|
|
use sqlx::Done;
|
|
|
|
|
use sqlx::FromRow;
|
|
|
|
|
|
|
|
|
|
pub struct Collection<'a> {
|
|
|
|
|
tx : &'a mut Box<sqlx::Transaction<'static, sqlx::Postgres>>,
|
|
|
|
|
q : sqlx::query::Query<'a, sqlx::Postgres, sqlx::postgres::PgArguments>,
|
|
|
|
|
q_id : Option<i64>
|
|
|
|
|
tx: &'a mut Box<sqlx::Transaction<'static, sqlx::Postgres>>,
|
|
|
|
|
q: sqlx::query::Query<'a, sqlx::Postgres, sqlx::postgres::PgArguments>,
|
|
|
|
|
q_id: Option<i64>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Collection<'_> {
|
|
|
|
|
pub fn by_id(self, id : i64) -> Self {
|
|
|
|
|
pub fn by_id(self, id: i64) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
tx: self.tx,
|
|
|
|
|
q: self.q,
|
|
|
|
|
q_id: Some(id)
|
|
|
|
|
q_id: Some(id),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn fetch(self, offset : i64, limit : i64) -> Result<Vec<robots::Robot>, HandlingError> {
|
|
|
|
|
let result = self.q
|
|
|
|
|
pub async fn fetch(self, offset: i64, limit: i64) -> Result<Vec<robots::Robot>, HandlingError> {
|
|
|
|
|
let result = self
|
|
|
|
|
.q
|
|
|
|
|
.bind(self.q_id.is_some())
|
|
|
|
|
.bind(self.q_id.unwrap_or_default())
|
|
|
|
|
.bind(offset)
|
|
|
|
|
@ -37,18 +36,19 @@ impl Collection<'_> {
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|d| {
|
|
|
|
|
let (id, name) = <(i64, String)>::from_row(&d)?;
|
|
|
|
|
Ok(robots::Robot{
|
|
|
|
|
id: id,
|
|
|
|
|
name: name
|
|
|
|
|
})
|
|
|
|
|
Ok(robots::Robot { id: id, name: name })
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e))
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn paginate(self, offset : i64, limit : i64) -> Result<(Vec<robots::Robot>, bool), HandlingError> {
|
|
|
|
|
let mut items = self.fetch(offset, limit+1).await?;
|
|
|
|
|
pub async fn paginate(
|
|
|
|
|
self,
|
|
|
|
|
offset: i64,
|
|
|
|
|
limit: i64,
|
|
|
|
|
) -> Result<(Vec<robots::Robot>, bool), HandlingError> {
|
|
|
|
|
let mut items = self.fetch(offset, limit + 1).await?;
|
|
|
|
|
if items.len() > (limit as usize) {
|
|
|
|
|
let _ = items.pop();
|
|
|
|
|
Ok((items, true))
|
|
|
|
|
@ -59,19 +59,18 @@ impl Collection<'_> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct RobotsImpl<'a> {
|
|
|
|
|
pub tx : &'a mut Box<sqlx::Transaction<'static, sqlx::Postgres>>
|
|
|
|
|
pub tx: &'a mut Box<sqlx::Transaction<'static, sqlx::Postgres>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub type ModelImpl<'a> = RobotsImpl<'a>;
|
|
|
|
|
|
|
|
|
|
impl<'a> RobotsImpl<'_> {
|
|
|
|
|
pub fn new<'b>(tx : &'b mut Box<sqlx::Transaction<'static, sqlx::Postgres>>) -> RobotsImpl<'b> {
|
|
|
|
|
RobotsImpl{tx: tx}
|
|
|
|
|
pub fn new<'b>(tx: &'b mut Box<sqlx::Transaction<'static, sqlx::Postgres>>) -> RobotsImpl<'b> {
|
|
|
|
|
RobotsImpl { tx: tx }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn add(&mut self, r : robots::Robot) -> Result<i64, HandlingError> {
|
|
|
|
|
let result = sqlx::query(
|
|
|
|
|
"INSERT INTO robots (name) VALUES ($1::TEXT) RETURNING id")
|
|
|
|
|
async fn add(&mut self, r: robots::Robot) -> Result<i64, HandlingError> {
|
|
|
|
|
let result = sqlx::query("INSERT INTO robots (name) VALUES ($1::TEXT) RETURNING id")
|
|
|
|
|
.bind(r.name)
|
|
|
|
|
.fetch_one(self.tx.as_mut())
|
|
|
|
|
.await;
|
|
|
|
|
@ -79,28 +78,24 @@ impl<'a> RobotsImpl<'_> {
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
let (id,) = <(i64,)>::from_row(&v)?;
|
|
|
|
|
Ok(id)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
Err(sqlx::Error::RowNotFound) => Err(HandlingError::NotFound),
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e))
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_by_id(&mut self, id : i64) -> Result<robots::Robot, HandlingError> {
|
|
|
|
|
let result = sqlx::query(
|
|
|
|
|
"SELECT * FROM robots WHERE id = $1::INTEGER")
|
|
|
|
|
pub async fn get_by_id(&mut self, id: i64) -> Result<robots::Robot, HandlingError> {
|
|
|
|
|
let result = sqlx::query("SELECT * FROM robots WHERE id = $1::INTEGER")
|
|
|
|
|
.bind(id)
|
|
|
|
|
.fetch_one(self.tx.as_mut())
|
|
|
|
|
.await;
|
|
|
|
|
match result {
|
|
|
|
|
Ok(v) => {
|
|
|
|
|
let (id, name) = <(i64, String)>::from_row(&v)?;
|
|
|
|
|
Ok(robots::Robot{
|
|
|
|
|
id: id,
|
|
|
|
|
name: name
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
Ok(robots::Robot { id: id, name: name })
|
|
|
|
|
}
|
|
|
|
|
Err(sqlx::Error::RowNotFound) => Err(HandlingError::NotFound),
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e))
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -111,18 +106,17 @@ impl<'a> RobotsImpl<'_> {
|
|
|
|
|
WHERE
|
|
|
|
|
(CASE WHEN $1::BOOLEAN THEN (id = $2::INTEGER) ELSE TRUE END)
|
|
|
|
|
OFFSET $3
|
|
|
|
|
LIMIT $4"
|
|
|
|
|
LIMIT $4",
|
|
|
|
|
);
|
|
|
|
|
Ok(Collection{
|
|
|
|
|
Ok(Collection {
|
|
|
|
|
tx: self.tx,
|
|
|
|
|
q: all,
|
|
|
|
|
q_id: None
|
|
|
|
|
q_id: None,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn update(&mut self, r : robots::Robot) -> Result<(), HandlingError> {
|
|
|
|
|
let result = sqlx::query(
|
|
|
|
|
"UPDATE robots SET name = $1::TEXT WHERE id = $2::INTEGER")
|
|
|
|
|
async fn update(&mut self, r: robots::Robot) -> Result<(), HandlingError> {
|
|
|
|
|
let result = sqlx::query("UPDATE robots SET name = $1::TEXT WHERE id = $2::INTEGER")
|
|
|
|
|
.bind(r.name)
|
|
|
|
|
.bind(r.id)
|
|
|
|
|
.execute(self.tx.as_mut())
|
|
|
|
|
@ -131,18 +125,15 @@ impl<'a> RobotsImpl<'_> {
|
|
|
|
|
Ok(v) => match v.rows_affected() {
|
|
|
|
|
1 => Ok(()),
|
|
|
|
|
0 => Err(HandlingError::NotFound),
|
|
|
|
|
_ => Err(HandlingError::InternalError)
|
|
|
|
|
_ => Err(HandlingError::InternalError),
|
|
|
|
|
},
|
|
|
|
|
Err(sqlx::Error::RowNotFound) => Err(HandlingError::NotFound),
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e))
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async fn delete(&mut self, id : i64) -> Result<(), HandlingError> {
|
|
|
|
|
let result = sqlx::query(
|
|
|
|
|
"DELETE FROM robots WHERE id = $1::INTEGER")
|
|
|
|
|
async fn delete(&mut self, id: i64) -> Result<(), HandlingError> {
|
|
|
|
|
let result = sqlx::query("DELETE FROM robots WHERE id = $1::INTEGER")
|
|
|
|
|
.bind(id)
|
|
|
|
|
.execute(self.tx.as_mut())
|
|
|
|
|
.await;
|
|
|
|
|
@ -150,23 +141,23 @@ impl<'a> RobotsImpl<'_> {
|
|
|
|
|
Ok(v) => match v.rows_affected() {
|
|
|
|
|
1 => Ok(()),
|
|
|
|
|
0 => Err(HandlingError::NotFound),
|
|
|
|
|
_ => Err(HandlingError::InternalError)
|
|
|
|
|
_ => Err(HandlingError::InternalError),
|
|
|
|
|
},
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e))
|
|
|
|
|
Err(e) => Err(HandlingError::DBError(e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl robots::DefaultRepository for RobotsImpl<'_> {
|
|
|
|
|
fn add(&mut self, r : robots::Robot) -> Pin<Box<dyn Future<Output = Result<i64, HandlingError>> + Send + '_>> {
|
|
|
|
|
fn add(&mut self, r: robots::Robot) -> TraitFuture<Result<i64, HandlingError>> {
|
|
|
|
|
Box::pin(self.add(r))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn update(&mut self, r : robots::Robot) -> Pin<Box<dyn Future<Output = Result<(), HandlingError>> + Send + '_>> {
|
|
|
|
|
fn update(&mut self, r: robots::Robot) -> TraitFuture<Result<(), HandlingError>> {
|
|
|
|
|
Box::pin(self.update(r))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn delete(&mut self, id : i64) -> Pin<Box<dyn Future<Output = Result<(), HandlingError>> + Send + '_>> {
|
|
|
|
|
fn delete(&mut self, id: i64) -> TraitFuture<Result<(), HandlingError>> {
|
|
|
|
|
Box::pin(self.delete(id))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|