Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ serde_json = "1.0"
diesel = { version = "1.3.2", features = ["postgres", "chrono", "uuid"] }
chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "0.6", features = ["serde", "v4"] }
mashup = "0.1"

[dev-dependencies]
diesel_migrations = { version = "1.3.0", features = ["postgres"] }
67 changes: 66 additions & 1 deletion src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,70 @@ where

fn get_all(&self, by: Expr<Self::DBTable>) -> Result<Vec<Self::ModelWithId>, Error>;

fn update(&self, model: &Self::Model, by: Expr<Self::DBTable>) -> Result<Self::ModelWithId, Error>;
fn update(
&self,
model: &Self::Model,
by: Expr<Self::DBTable>,
) -> Result<Self::ModelWithId, Error>;
}

macro_rules! resource_controller {
($model:ident) => {

mashup! {
controller["controller"] = $model Controller;
modelWithId["modelWithId"] = $model WithId;
}

controller! {
pub struct "controller";

modelWithId! {
impl ResourceWithId for "controller" {
type ModelWithId = "modelWithId";
}
}

impl Resource for "controller" {
type Model = $model;
}

impl ResourceTable for "controller" {
type DBTable = table;
}

impl ResourceSql for "controller" {
type SQLType = SqlType;
}

use crate::db::establish_connection as connection;

impl ResourceController for "controller" {
fn create(&self, model: &Self::Model) -> Result<Self::ModelWithId, Error> {
Ok(insert_into(table)
.values(model)
.get_result(&connection())?)
}

fn get_one(&self, by: Expr<table>) -> Result<Self::ModelWithId, Error> {
Ok(table
.filter(by)
.get_result::<Self::ModelWithId>(&connection())?)
}

fn get_all(&self, by: Expr<table>) -> Result<Vec<Self::ModelWithId>, Error> {
Ok(table
.filter(by)
.get_results::<Self::ModelWithId>(&connection())?)
}

fn update(&self, model: &Self::Model, by: Expr<table>) -> Result<Self::ModelWithId, Error> {
Ok(update(table)
.filter(by)
.set(model)
.get_result::<Self::ModelWithId>(&connection())?)
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod controller;

#[macro_use]
pub mod controller;