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