Last active
December 3, 2022 20:54
-
-
Save rj76/2870356257c6e33c3db9d999f53510e3 to your computer and use it in GitHub Desktop.
CRUD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub trait CrudModel: Table + IntoUpdateTarget { | |
type FormObj; | |
fn get<T>(self, conn: &mut TenantConnection, id: i32) -> QueryResult<T> | |
where | |
Self: FindDsl<i32>, | |
<Self as FindDsl<i32>>::Output: | |
Table + AsQuery + for<'a> LoadQuery<'a, TenantConnection, T>, | |
{ | |
self.find(id).get_result::<T>(conn) | |
} | |
fn create<T>(self, conn: &mut TenantConnection, form: Self::FormObj) -> QueryResult<T> | |
where | |
Self::FormObj: diesel::Insertable<Self>, | |
InsertStatement<Self, <<Self as CrudModel>::FormObj as diesel::Insertable<Self>>::Values>: | |
AsQuery + for<'a> LoadQuery<'a, TenantConnection, T>, | |
{ | |
diesel::insert_into(self).values(form).get_result::<T>(conn) | |
} | |
fn update(self, conn: &mut TenantConnection, id: i32, form: Self::FormObj) -> QueryResult<usize> | |
where | |
Self: FindDsl<i32>, | |
Self::FormObj: diesel::AsChangeset<Target = Self>, | |
dsl::Find<Self, i32>: IntoUpdateTarget + HasTable<Table = Self>, | |
UpdateStatement< | |
Self, | |
<dsl::Find<Self, i32> as IntoUpdateTarget>::WhereClause, | |
<Self::FormObj as diesel::AsChangeset>::Changeset, | |
>: AsQuery + ExecuteDsl<TenantConnection>, | |
{ | |
diesel::update(self.find(id)).set(form).execute(conn) | |
} | |
fn delete(self, conn: &mut TenantConnection, id: i32) -> Result<usize, diesel::result::Error> | |
where | |
Self: FindDsl<i32>, | |
dsl::Find<Self, i32>: IntoUpdateTarget + HasTable<Table = Self>, | |
DeleteStatement<Self, <dsl::Find<Self, i32> as IntoUpdateTarget>::WhereClause>: | |
AsQuery + ExecuteDsl<TenantConnection>, | |
{ | |
diesel::delete(self.find(id)).execute(conn) | |
} | |
} | |
// we can now do: | |
impl CrudModel for order_statuscode::table { | |
type FormObj = OrderStatuscodeForm; | |
} | |
order_statuscode::table.get(conn, id) | |
order_statuscode::table.create(conn, form) | |
order_statuscode::table.update(conn, id, form) | |
order_statuscode::table.delete(conn, id) | |
// in stead of | |
impl OrderStatuscode { | |
pub fn get(conn: &mut TenantConnection, id: i32) -> Result<OrderStatuscode, Error> { | |
let bla = order_statuscode::table; | |
print_type_of(&bla); | |
let result = order_statuscode::table | |
.find(id) | |
.get_result::<OrderStatuscode>(conn) | |
.expect("Error getting statuscode"); | |
Ok(result) | |
} | |
pub fn create( | |
conn: &mut TenantConnection, | |
statuscode_form: OrderStatuscodeForm, | |
) -> Result<OrderStatuscode, Error> { | |
let orderline: OrderStatuscode = diesel::insert_into(order_statuscode::table) | |
.values(&statuscode_form) | |
.get_result::<OrderStatuscode>(conn) | |
.expect("Unable to create statuscode"); | |
Ok(orderline) | |
} | |
pub fn update( | |
conn: &mut TenantConnection, | |
id: i32, | |
statuscode_form: OrderStatuscodeForm, | |
) -> Result<usize, Error> { | |
let result = diesel::update(order_statuscode::table.find(id)) | |
.set(&statuscode_form) | |
.execute(conn) | |
.expect("Error updating statuscode"); | |
Ok(result) | |
} | |
pub fn delete(conn: &mut TenantConnection, id: i32) -> Result<usize, Error> { | |
let res = diesel::delete(order_statuscode::table.find(id)) | |
.execute(conn) | |
.expect("Error deleting statuscode"); | |
Ok(res) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment