From a22195d5d840536e429e6a49443b8ed530f88521 Mon Sep 17 00:00:00 2001 From: manuel Date: Sun, 22 May 2022 22:00:33 +0200 Subject: [PATCH] working list impl --- actix_admin/actix_admin_macros/src/lib.rs | 7 +++---- actix_admin/src/lib.rs | 16 +++++++++------- actix_admin/templates/list.html | 6 +++++- database.db | Bin 20480 -> 24576 bytes database.db-wal | Bin 16512 -> 16512 bytes src/main.rs | 6 +++--- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/actix_admin/actix_admin_macros/src/lib.rs b/actix_admin/actix_admin_macros/src/lib.rs index b1b7cee..d861f48 100644 --- a/actix_admin/actix_admin_macros/src/lib.rs +++ b/actix_admin/actix_admin_macros/src/lib.rs @@ -37,13 +37,12 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea #[async_trait(?Send)] impl ActixAdminViewModelTrait for Entity { - async fn list(self, db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec { - let model = ActixAdminViewModel::from(Entity); + async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec { let entities = Entity::list_model(db, 1, 5).await; entities } - async fn create_entity(self, db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel { + async fn create_entity(db: &DatabaseConnection, mut model: ActixAdminModel) -> ActixAdminModel { let new_model = ActiveModel { title: Set("test".to_string()), text: Set("test".to_string()), @@ -51,7 +50,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea }; let insert_operation = Entity::insert(new_model).exec(db).await; - ActixAdminModel{ values: HashMap::new() } + model } } diff --git a/actix_admin/src/lib.rs b/actix_admin/src/lib.rs index a8fd114..2df3cae 100644 --- a/actix_admin/src/lib.rs +++ b/actix_admin/src/lib.rs @@ -51,8 +51,8 @@ pub struct ActixAdminModel { // ActixAdminViewModel #[async_trait(?Send)] pub trait ActixAdminViewModelTrait { - async fn list(self, db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec; - async fn create_entity(self, db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel; + async fn list(db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec; + async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel; } #[derive(Clone, Debug, Serialize)] @@ -102,7 +102,7 @@ pub async fn index(data: web::Data) -> Result(req: HttpRequest, data: web::Data, path: web::Path) -> Result { +pub async fn list(req: HttpRequest, data: web::Data, path: web::Path) -> Result { let entity_name: String = path.into_inner(); let actix_admin = data.get_actix_admin(); let view_model: &ActixAdminViewModel = actix_admin.view_models.get(&entity_name).unwrap(); @@ -114,7 +114,7 @@ pub async fn list(req: HttpRequest, data: web::Data, path: w let entities_per_page = params.entities_per_page.unwrap_or(DEFAULT_ENTITIES_PER_PAGE); let db = data.get_db(); - let entities: Vec = Vec::new(); // entity.list(db, page, entities_per_page).await; + let entities: Vec = E::list(db, page, entities_per_page).await; let mut ctx = Context::new(); ctx.insert("entity_names", &entity_names); @@ -130,7 +130,7 @@ pub async fn list(req: HttpRequest, data: web::Data, path: w Ok(HttpResponse::Ok().content_type("text/html").body(body)) } -pub async fn create_get(_req: HttpRequest, data: web::Data, _body: web::Payload, _text: String, entity_name: web::Path) -> Result { +pub async fn create_get(_req: HttpRequest, data: web::Data, _body: web::Payload, _text: String, entity_name: web::Path) -> Result { let _db = &data.get_db(); let entity_name: String = entity_name.into_inner(); println!("{}", &entity_name); @@ -151,12 +151,14 @@ pub async fn create_get(_req: HttpRequest, data: web::Data, Ok(HttpResponse::Ok().content_type("text/html").body(body)) } -pub async fn create_post(_req: HttpRequest, data: web::Data, text: String, entity_name: web::Path) -> Result { - let _db = &data.get_db(); +pub async fn create_post(_req: HttpRequest, data: web::Data, text: String, entity_name: web::Path) -> Result { + let db = &data.get_db(); let entity_name: String = entity_name.into_inner(); let actix_admin = data.get_actix_admin(); let view_model = actix_admin.view_models.get(&entity_name).unwrap(); + let mut model = ActixAdminModel{ values: HashMap::new() }; + model = E::create_entity(db, model).await; println!("{}", &entity_name); println!("{}", &text); diff --git a/actix_admin/templates/list.html b/actix_admin/templates/list.html index 3612ff2..0661d1c 100644 --- a/actix_admin/templates/list.html +++ b/actix_admin/templates/list.html @@ -8,8 +8,12 @@ {{ model_field[0] }} {%- endfor %} + {% for entity in entities -%} - + {% for model_field in view_model.fields -%} + {{ entity.values | get(key=model_field[0]) }} + {%- endfor %} + {%- endfor %} {% endblock content %} \ No newline at end of file diff --git a/database.db b/database.db index c70ecb13954b3dbd920af046528cfaf8aaab10b0..f47e70a88bd4910a26463dc72c7d6eda550b1556 100644 GIT binary patch delta 71 zcmZozz}RqraRZCPW)_1_{9+u8%%Y41`NbtnybKHsO#Dw6_&@PK*(@kWaRZCPW)^`z`~WG11t|ak diff --git a/database.db-wal b/database.db-wal index 9af5d47473151066a07656f2590029dc27bf4802..966c0bb8711988422f4115913b3c218bca3b47e2 100644 GIT binary patch literal 16512 zcmeI&F-yZh7{>9tUJ^PaT$g}*=Qs%&yLGFbg;EOA*f(N&Tvb1cuU?|FfH zRxE8Qvkou3+w=WP*eH< literal 16512 zcmeI&PiqrF7zXg&wAt9&G=hZnILCx0f`|x$=%%hVcC)GJ291|ZnvCsg{5sC9$IJ zuuG86dUdfjjO2<%%QWm^UJ$LX(E1J5YmuB)%!OjKPwPE7DwlgLx;y;c!A+*mjA{B} zR#m)qgZ6h8xYpwxzwU-H++*PuxeuTFlOKzw$?d&g{C)oJdBKy@bMFpcy?ZJyL%~r( z&oH8!2Uvd5@$p8QV diff --git a/src/main.rs b/src/main.rs index c7a2791..ef37717 100644 --- a/src/main.rs +++ b/src/main.rs @@ -64,9 +64,9 @@ fn setup_actix_admin( .create_scope::() .service( web::scope("/{entity_name}") - .route("/list", web::get().to(actix_admin::list::)) - .route("/create", web::get().to(actix_admin::create_get::)) - .route("/create", web::post().to(actix_admin::create_post::)) + .route("/list", web::get().to(actix_admin::list::)) + .route("/create", web::get().to(actix_admin::create_get::)) + .route("/create", web::post().to(actix_admin::create_post::)) ) }