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,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>(
|
pub struct RobotModel(Robot);
|
||||||
repo: &'c mut impl DefaultRepository<'c>,
|
|
||||||
r: Robot,
|
impl TryFrom<Robot> for RobotModel {
|
||||||
) -> Result<i64, HandlingError> {
|
type Error = HandlingError;
|
||||||
repo.add(r).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update<'c>(
|
fn try_from(r: Robot) -> Result<Self, Self::Error> {
|
||||||
repo: &'c mut impl DefaultRepository<'c>,
|
Ok(Self(r))
|
||||||
r: Robot,
|
}
|
||||||
) -> Result<(), HandlingError> {
|
|
||||||
repo.update(r).await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue