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 c70ecb1..f47e70a 100644 Binary files a/database.db and b/database.db differ diff --git a/database.db-wal b/database.db-wal index 9af5d47..966c0bb 100644 Binary files a/database.db-wal and b/database.db-wal differ 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::)) ) }