move activemodel creation to from trait

This commit is contained in:
manuel 2022-05-22 22:35:27 +02:00
parent 6f8a802deb
commit 55c8a99a3c

View File

@ -48,6 +48,16 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
} }
} }
impl From<ActixAdminModel> for ActiveModel {
fn from(model: ActixAdminModel) -> Self {
ActiveModel {
title: Set("test".to_string()),
text: Set("test".to_string()),
..Default::default()
}
}
}
#[async_trait(?Send)] #[async_trait(?Send)]
impl ActixAdminViewModelTrait for Entity { impl ActixAdminViewModelTrait for Entity {
async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> { async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
@ -56,11 +66,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
} }
async fn create_entity(db: &DatabaseConnection, mut model: ActixAdminModel) -> ActixAdminModel { async fn create_entity(db: &DatabaseConnection, mut model: ActixAdminModel) -> ActixAdminModel {
let new_model = ActiveModel { let new_model = ActiveModel::from(model.clone());
title: Set("test".to_string()),
text: Set("test".to_string()),
..Default::default()
};
let insert_operation = Entity::insert(new_model).exec(db).await; let insert_operation = Entity::insert(new_model).exec(db).await;
model model
@ -78,7 +84,6 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
.fetch_page(page - 1) .fetch_page(page - 1)
.await .await
.expect("could not retrieve entities"); .expect("could not retrieve entities");
// TODO: must be dynamic
let mut model_entities = Vec::new(); let mut model_entities = Vec::new();
for entity in entities { for entity in entities {
model_entities.push( model_entities.push(