From a5ebc202e5e9558ec80fe058c6f6a90adf8dd8e4 Mon Sep 17 00:00:00 2001 From: Manuel Gugger Date: Tue, 27 Jun 2023 13:05:44 +0200 Subject: [PATCH] remove unnecessary trait --- docs/content/docs/adding-crud-models.md | 9 ++- docs/content/docs/custom-handler.md | 28 ++++----- docs/content/docs/getting-started.md | 33 ++--------- examples/azure_auth/main.rs | 75 ++++++++----------------- examples/basic/main.rs | 35 +++--------- src/builder.rs | 48 +++++++--------- src/lib.rs | 8 +-- src/routes/create_or_edit_get.rs | 23 ++++---- src/routes/create_or_edit_post.rs | 31 +++++----- src/routes/delete.rs | 19 ++++--- src/routes/file.rs | 16 +++--- src/routes/index.rs | 12 ++-- src/routes/list.rs | 11 ++-- src/routes/show.rs | 10 ++-- tests/test_setup/helper.rs | 65 ++++++++------------- tests/test_setup/mod.rs | 3 +- 16 files changed, 170 insertions(+), 256 deletions(-) diff --git a/docs/content/docs/adding-crud-models.md b/docs/content/docs/adding-crud-models.md index 1e9cbdf..d0a432a 100644 --- a/docs/content/docs/adding-crud-models.md +++ b/docs/content/docs/adding-crud-models.md @@ -24,7 +24,10 @@ pub struct Model { pub comment: String } +// Custom Validation Functions impl ActixAdminModelValidationTrait for Entity {} +// Custom Search Filters +impl ActixAdminModelFilterTrait for Entity {} ``` ## Derive Implementations @@ -65,7 +68,7 @@ fn create_actix_admin_builder() -> ActixAdminBuilder { let mut admin_builder = ActixAdminBuilder::new(configuration); let comment_view_model = ActixAdminViewModel::from(Comment); - admin_builder.add_entity::(&comment_view_model); + admin_builder.add_entity::(&comment_view_model); admin_builder } @@ -75,8 +78,8 @@ fn create_actix_admin_builder() -> ActixAdminBuilder { Views / Models can be grouped in the Navbar by using the following functions instead of ```admin_builder.add_entity```: ```rust -admin_builder.add_entity::(&comment_view_model); -admin_builder.add_entity_to_category::(&comment_view_model, "Group 1"); +admin_builder.add_entity::(&comment_view_model); +admin_builder.add_entity_to_category::(&comment_view_model, "Group 1"); ``` ## Additional Model Attributes diff --git a/docs/content/docs/custom-handler.md b/docs/content/docs/custom-handler.md index b604537..e502db7 100644 --- a/docs/content/docs/custom-handler.md +++ b/docs/content/docs/custom-handler.md @@ -22,16 +22,16 @@ A custom *custom_index.html* view can be defined as follows by extending the bas To display the *custom_index.html*, define the corresponding function which extends the current tera context from actix-admin and also uses the actix-admin tera instance to render the custom index function. ```rust -async fn custom_index( +async fn custom_index( session: Session, - data: web::Data + data: web::Data + actix_admin: web::Data ) -> Result { let mut ctx = Context::new(); - ctx.extend(get_admin_ctx(session, &data)); + ctx.extend(get_admin_ctx(session, &actix_admin)); - let body = data.get_tmpl() - .render("custom_index.html", &ctx).unwrap(); + let body = data.tmpl.render("custom_index.html", &ctx).unwrap(); Ok(HttpResponse::Ok().content_type("text/html").body(body)) } @@ -40,8 +40,8 @@ async fn custom_index( After this in the builder, pass your custom index function defined above: ```rust let mut admin_builder = ActixAdminBuilder::new(configuration); -admin_builder.add_custom_handler_for_index::( - web::get().to(custom_index::) +admin_builder.add_custom_handler_for_index( + web::get().to(custom_index) ); ``` @@ -53,7 +53,7 @@ Similarly to the custom index above, the builder accepts additional routes to be ```rust // This will be shown in the top level menu let show_in_menu = true; -admin_builder.add_custom_handler("Custom Route in Menu", "/custom_route_in_menu", web::get().to custom_index::), show_in_menu); +admin_builder.add_custom_handler("Custom Route in Menu", "/custom_route_in_menu", web::get().to(custom_index), show_in_menu); ``` ### Tied to a specific entity @@ -61,11 +61,11 @@ admin_builder.add_custom_handler("Custom Route in Menu", "/custom_route_in_menu" // this will expose a menu item which links to /admin/comment/custom_handler and is shown in the NavBar menu let show_in_menu = true; let some_category = "Some Category"; -admin_builder.add_entity_to_category::(&comment_view_model, some_category); -admin_builder.add_custom_handler_for_entity::( +admin_builder.add_entity_to_category::(&comment_view_model, some_category); +admin_builder.add_custom_handler_for_entity::( "My custom handler", "/custom_handler", - web::get().to(custom_handler::), + web::get().to(custom_handler::), show_in_menu ); ``` @@ -75,11 +75,11 @@ admin_builder.add_custom_handler_for_entity::( // this will expose a menu item which links to /admin/comment/custom_handler and is shown in the NavBar menu in the group "Some Category" let show_in_menu = true; let some_category = "Some Category"; -admin_builder.add_entity_to_category::(&comment_view_model, some_category); -admin_builder.add_custom_handler_for_entity_in_category::( +admin_builder.add_entity_to_category::(&comment_view_model, some_category); +admin_builder.add_custom_handler_for_entity_in_category::( "My custom handler", "/custom_handler", - web::get().to(custom_handler::), + web::get().to(custom_handler::), some_category, show_in_menu ); diff --git a/docs/content/docs/getting-started.md b/docs/content/docs/getting-started.md index 423466c..1f31449 100644 --- a/docs/content/docs/getting-started.md +++ b/docs/content/docs/getting-started.md @@ -12,30 +12,7 @@ weight: 1 Cargo.toml: ```cargo [dependencies] -actix-admin = "0.3.0" -``` - -## Implement the Trait for AppState - -Actix-Admin requires to get the database connection and its configuration from the actix AppState. The trait "ActixAdminAppDataTrait" must be implemented for your AppState: - -```rust -use actix_admin::prelude::*; - -#[derive(Clone)] -pub struct AppState { - pub db: DatabaseConnection, - pub actix_admin: ActixAdmin, -} - -impl ActixAdminAppDataTrait for AppState { - fn get_db(&self) -> &DatabaseConnection { - &self.db - } - fn get_actix_admin(&self) -> &ActixAdmin { - &self.actix_admin - } -} +actix-admin = "0.4.0" ``` ## Build the Actix-Admin Configuration @@ -65,15 +42,13 @@ fn create_actix_admin_builder() -> ActixAdminBuilder { The AppState and the configuration can be passed to Actix-Web like in the following snippet. The ActixAdminBuilder creates an own */admin/* Scope which is registered as a service in the Actix-Web app. ```rust +let conn = sea_orm::Database::connect(opt).await.unwrap(); let actix_admin_builder = create_actix_admin_builder(); -let app_state = AppState { - db: conn.clone(), - actix_admin: actix_admin_builder.get_actix_admin(), -}; - let app = App::new() .app_data(web::Data::new(app_state)) + .app_data(web::Data::new(conn.clone())) + .app_data(web::Data::new(actix_admin_builder.get_actix_admin())) .service( actix_admin_builder.get_scope::() ) diff --git a/examples/azure_auth/main.rs b/examples/azure_auth/main.rs index 1c7bc2e..9b7379d 100644 --- a/examples/azure_auth/main.rs +++ b/examples/azure_auth/main.rs @@ -6,7 +6,7 @@ use actix_web::{cookie::Key, web, App, HttpResponse, HttpServer, middleware}; use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo}; use oauth2::basic::BasicClient; use oauth2::RedirectUrl; -use sea_orm::{ConnectOptions, DatabaseConnection}; +use sea_orm::{ConnectOptions}; use std::env; use std::time::Duration; use tera::{Context, Tera}; @@ -18,28 +18,7 @@ use entity::{Post, Comment}; #[derive(Clone)] pub struct AppState { pub oauth: BasicClient, - pub tmpl: Tera, - pub db: DatabaseConnection, - pub actix_admin: ActixAdmin, -} - -impl ActixAdminAppDataTrait for AppState { - fn get_db(&self) -> &DatabaseConnection { - &self.db - } - fn get_actix_admin(&self) -> &ActixAdmin { - &self.actix_admin - } -} - -trait AppDataTrait { - fn get_tmpl(&self) -> &Tera; -} - -impl AppDataTrait for AppState { - fn get_tmpl(&self) -> &Tera { - &self.tmpl - } + pub tmpl: Tera } impl AzureAuthAppDataTrait for AppState { @@ -48,42 +27,36 @@ impl AzureAuthAppDataTrait for AppState { } } -async fn custom_handler< - T: ActixAdminAppDataTrait + AppDataTrait, - E: ActixAdminViewModelTrait, ->( +async fn custom_handler( session: Session, - data: web::Data, + data: web::Data, + actix_admin: web::Data, _text: String ) -> Result { let mut ctx = Context::new(); - ctx.extend(get_admin_ctx(session, &data)); + ctx.extend(get_admin_ctx(session, &actix_admin)); - let body = data.get_tmpl() - .render("custom_handler.html", &ctx).unwrap(); + let body = data.tmpl.render("custom_handler.html", &ctx).unwrap(); Ok(HttpResponse::Ok().content_type("text/html").body(body)) } -async fn custom_index< - T: ActixAdminAppDataTrait + AppDataTrait ->( +async fn custom_index( session: Session, - data: web::Data, + data: web::Data, + actix_admin: web::Data, _text: String ) -> Result { let mut ctx = Context::new(); - ctx.extend(get_admin_ctx(session, &data)); + ctx.extend(get_admin_ctx(session, &actix_admin)); - let body = data.get_tmpl() - .render("custom_index.html", &ctx).unwrap(); + let body = data.tmpl.render("custom_index.html", &ctx).unwrap(); Ok(HttpResponse::Ok().content_type("text/html").body(body)) } - async fn index(session: Session, data: web::Data) -> HttpResponse { let login = session.get::("user_info").unwrap(); let web_auth_link = if login.is_some() { @@ -115,19 +88,19 @@ fn create_actix_admin_builder() -> ActixAdminBuilder { }; let mut admin_builder = ActixAdminBuilder::new(configuration); - admin_builder.add_custom_handler_for_index::( - web::get().to(custom_index::) + admin_builder.add_custom_handler_for_index( + web::get().to(custom_index) ); - admin_builder.add_entity::(&post_view_model); - admin_builder.add_custom_handler("Custom Route in Menu", "/custom_route_in_menu", web::get().to(custom_index::), true); - admin_builder.add_custom_handler("Custom Route not in Menu", "/custom_route_not_in_menu", web::get().to(custom_index::), false); + admin_builder.add_entity::(&post_view_model); + admin_builder.add_custom_handler("Custom Route in Menu", "/custom_route_in_menu", web::get().to(custom_index), true); + admin_builder.add_custom_handler("Custom Route not in Menu", "/custom_route_not_in_menu", web::get().to(custom_index), false); let some_category = "Some Category"; - admin_builder.add_entity_to_category::(&comment_view_model, some_category); - admin_builder.add_custom_handler_for_entity_in_category::( + admin_builder.add_entity_to_category::(&comment_view_model, some_category); + admin_builder.add_custom_handler_for_entity_in_category::( "My custom handler", "/custom_handler", - web::get().to(custom_handler::), + web::get().to(custom_handler), some_category, true ); @@ -179,18 +152,18 @@ async fn main() { let app_state = AppState { oauth: client.clone(), - tmpl: tera.clone(), - db: conn.clone(), - actix_admin: actix_admin, + tmpl: tera.clone() }; App::new() .app_data(web::Data::new(app_state.clone())) + .app_data(web::Data::new(conn.clone())) + .app_data(web::Data::new(actix_admin.clone())) .wrap(SessionMiddleware::new(CookieSessionStore::default(), cookie_secret_key.clone())) .route("/", web::get().to(index)) .service(azure_auth.clone().create_scope::()) .service( - actix_admin_builder.get_scope::() + actix_admin_builder.get_scope() ) .wrap(middleware::Logger::default()) }) diff --git a/examples/basic/main.rs b/examples/basic/main.rs index b38179c..7c03920 100644 --- a/examples/basic/main.rs +++ b/examples/basic/main.rs @@ -2,26 +2,11 @@ extern crate serde_derive; use actix_admin::prelude::*; use actix_web::{web, App, HttpServer, middleware}; -use sea_orm::{ConnectOptions, DatabaseConnection}; +use sea_orm::{ConnectOptions}; use std::time::Duration; mod entity; use entity::{Post, Comment, User}; -#[derive(Clone)] -pub struct AppState { - pub db: DatabaseConnection, - pub actix_admin: ActixAdmin, -} - -impl ActixAdminAppDataTrait for AppState { - fn get_db(&self) -> &DatabaseConnection { - &self.db - } - fn get_actix_admin(&self) -> &ActixAdmin { - &self.actix_admin - } -} - fn create_actix_admin_builder() -> ActixAdminBuilder { let configuration = ActixAdminConfiguration { enable_auth: false, @@ -35,13 +20,13 @@ fn create_actix_admin_builder() -> ActixAdminBuilder { let mut admin_builder = ActixAdminBuilder::new(configuration); let post_view_model = ActixAdminViewModel::from(Post); - admin_builder.add_entity::(&post_view_model); + admin_builder.add_entity::(&post_view_model); let some_category = "Group"; let comment_view_model = ActixAdminViewModel::from(Comment); - admin_builder.add_entity_to_category::(&comment_view_model, some_category); + admin_builder.add_entity_to_category::(&comment_view_model, some_category); let user_view_model = ActixAdminViewModel::from(User); - admin_builder.add_entity_to_category::(&user_view_model, some_category); + admin_builder.add_entity_to_category::(&user_view_model, some_category); admin_builder } @@ -60,7 +45,7 @@ fn get_db_options() -> ConnectOptions { #[actix_rt::main] async fn main() { let opt = get_db_options(); - let conn = sea_orm::Database::connect(opt).await.unwrap(); + let conn: sea_orm::DatabaseConnection = sea_orm::Database::connect(opt).await.unwrap(); let _ = entity::create_post_table(&conn).await; println!("The admin interface is available at http://localhost:5000/admin/"); @@ -69,15 +54,11 @@ async fn main() { let actix_admin_builder = create_actix_admin_builder(); - let app_state = AppState { - db: conn.clone(), - actix_admin: actix_admin_builder.get_actix_admin(), - }; - App::new() - .app_data(web::Data::new(app_state)) + .app_data(web::Data::new(actix_admin_builder.get_actix_admin())) + .app_data(web::Data::new(conn.clone())) .service( - actix_admin_builder.get_scope::() + actix_admin_builder.get_scope() ) .wrap(middleware::Logger::default()) }) diff --git a/src/builder.rs b/src/builder.rs index 44f5d82..0819610 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -20,12 +20,11 @@ pub struct ActixAdminBuilder { /// The trait to work with ActixAdminBuilder pub trait ActixAdminBuilderTrait { fn new(configuration: ActixAdminConfiguration) -> Self; - fn add_entity( + fn add_entity( &mut self, view_model: &ActixAdminViewModel, ); fn add_entity_to_category< - T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static, >( &mut self, @@ -40,7 +39,6 @@ pub trait ActixAdminBuilderTrait { add_to_menu: bool, ); fn add_custom_handler_for_entity< - T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static, >( &mut self, @@ -50,7 +48,6 @@ pub trait ActixAdminBuilderTrait { add_to_menu: bool ); fn add_custom_handler_for_entity_in_category< - T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static, >( &mut self, @@ -60,8 +57,8 @@ pub trait ActixAdminBuilderTrait { category_name: &str, add_to_menu: bool, ); - fn add_custom_handler_for_index(&mut self, route: Route); - fn get_scope(self) -> actix_web::Scope; + fn add_custom_handler_for_index(&mut self, route: Route); + fn get_scope(self) -> actix_web::Scope; fn get_actix_admin(&self) -> ActixAdmin; } @@ -207,15 +204,14 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { } } - fn add_entity( + fn add_entity( &mut self, view_model: &ActixAdminViewModel, ) { - let _ = &self.add_entity_to_category::(view_model, ""); + let _ = &self.add_entity_to_category::(view_model, ""); } fn add_entity_to_category< - T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static, >( &mut self, @@ -225,17 +221,17 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { self.scopes.insert( E::get_entity_name(), web::scope(&format!("/{}", E::get_entity_name())) - .route("/list", web::get().to(list::)) - .route("/create", web::get().to(create_get::)) - .route("/create", web::post().to(create_post::)) - .route("/edit/{id}", web::get().to(edit_get::)) - .route("/edit/{id}", web::post().to(edit_post::)) - .route("/delete", web::delete().to(delete_many::)) - .route("/delete/{id}", web::delete().to(delete::)) - .route("/show/{id}", web::get().to(show::)) - .route("/file/{id}/{column_name}", web::get().to(download::)) - .route("/file/{id}/{column_name}", web::delete().to(delete_file::)) - .default_service(web::to(not_found::)) + .route("/list", web::get().to(list::)) + .route("/create", web::get().to(create_get::)) + .route("/create", web::post().to(create_post::)) + .route("/edit/{id}", web::get().to(edit_get::)) + .route("/edit/{id}", web::post().to(edit_post::)) + .route("/delete", web::delete().to(delete_many::)) + .route("/delete/{id}", web::delete().to(delete::)) + .route("/show/{id}", web::get().to(show::)) + .route("/file/{id}/{column_name}", web::get().to(download::)) + .route("/file/{id}/{column_name}", web::delete().to(delete_file::)) + .default_service(web::to(not_found)) ); fs::create_dir_all(format!("{}/{}", &self.actix_admin.configuration.file_upload_directory, E::get_entity_name())).unwrap(); @@ -261,7 +257,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { self.actix_admin.view_models.insert(key, view_model.clone()); } - fn add_custom_handler_for_index(&mut self, route: Route) { + fn add_custom_handler_for_index(&mut self, route: Route) { self.custom_index = Some(route); } @@ -293,7 +289,6 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { } fn add_custom_handler_for_entity< - T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static, >( &mut self, @@ -302,7 +297,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { route: Route, add_to_menu: bool, ) { - let _ = &self.add_custom_handler_for_entity_in_category::( + let _ = &self.add_custom_handler_for_entity_in_category::( menu_element_name, path, route, @@ -312,7 +307,6 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { } fn add_custom_handler_for_entity_in_category< - T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static, >( &mut self, @@ -356,14 +350,14 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder { } } - fn get_scope(self) -> actix_web::Scope { + fn get_scope(self) -> actix_web::Scope { let index_handler = match self.custom_index { Some(handler) => handler, - _ => web::get().to(index::), + _ => web::get().to(index), }; let mut admin_scope = web::scope("/admin") .route("/", index_handler) - .default_service(web::to(not_found::)); + .default_service(web::to(not_found)); for (_entity, scope) in self.scopes { admin_scope = admin_scope.service(scope); diff --git a/src/lib.rs b/src/lib.rs index 9aba321..5ab6215 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,7 +31,7 @@ pub mod prelude { ActixAdminViewModelSerializable, ActixAdminViewModelTrait, ActixAdminViewModelFilter }; pub use crate::{hashmap, ActixAdminSelectListTrait}; - pub use crate::{ActixAdmin, ActixAdminAppDataTrait, ActixAdminConfiguration, ActixAdminError}; + pub use crate::{ActixAdmin, ActixAdminConfiguration, ActixAdminError}; pub use actix_admin_macros::{ DeriveActixAdmin, DeriveActixAdminEnumSelectList, DeriveActixAdminModel, DeriveActixAdminModelSelectList, DeriveActixAdminViewModel, @@ -53,12 +53,6 @@ macro_rules! hashmap { }} } -// AppDataTrait -pub trait ActixAdminAppDataTrait { - fn get_db(&self) -> &DatabaseConnection; - fn get_actix_admin(&self) -> &ActixAdmin; -} - // SelectListTrait #[async_trait] pub trait ActixAdminSelectListTrait { diff --git a/src/routes/create_or_edit_get.rs b/src/routes/create_or_edit_get.rs index 7af2187..02e72cc 100644 --- a/src/routes/create_or_edit_get.rs +++ b/src/routes/create_or_edit_get.rs @@ -1,4 +1,5 @@ use actix_web::{error, web, Error, HttpRequest, HttpResponse}; +use sea_orm::DatabaseConnection; use tera::{Context}; use actix_session::{Session}; use crate::ActixAdminError; @@ -9,34 +10,36 @@ use super::DEFAULT_ENTITIES_PER_PAGE; use super::Params; use super::{ add_auth_context, user_can_access_page, render_unauthorized}; -pub async fn create_get( +pub async fn create_get( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, _body: web::Payload, _text: String, ) -> Result { - let db = &data.get_db(); + let db = db.get_ref(); let model = ActixAdminModel::create_empty(); - create_or_edit_get::(&session, req, &data, db, Ok(model)).await + create_or_edit_get::(&session, req, &data, db, Ok(model)).await } -pub async fn edit_get( +pub async fn edit_get( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, _text: String, id: web::Path ) -> Result { - let db = &data.get_db(); + let db = db.get_ref(); let model = E::get_entity(db, id.into_inner()).await; - create_or_edit_get::(&session, req, &data, db, model).await + create_or_edit_get::(&session, req, &data, db, model).await } -async fn create_or_edit_get(session: &Session, req: HttpRequest, data: &web::Data, db: &sea_orm::DatabaseConnection, model_result: Result) -> Result{ - let actix_admin = &data.get_actix_admin(); +async fn create_or_edit_get(session: &Session, req: HttpRequest, data: &web::Data, db: &sea_orm::DatabaseConnection, model_result: Result) -> Result{ + let actix_admin = &data.get_ref(); let mut ctx = Context::new(); add_auth_context(&session, actix_admin, &mut ctx); let entity_names = &actix_admin.entity_names; diff --git a/src/routes/create_or_edit_post.rs b/src/routes/create_or_edit_post.rs index 66ed779..4c54d9f 100644 --- a/src/routes/create_or_edit_post.rs +++ b/src/routes/create_or_edit_post.rs @@ -8,16 +8,18 @@ use actix_multipart::MultipartError; use actix_session::Session; use actix_web::http::header; use actix_web::{error, web, Error, HttpRequest, HttpResponse}; +use sea_orm::DatabaseConnection; use std::collections::HashMap; use tera::Context; -pub async fn create_post( +pub async fn create_post( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, payload: Multipart, ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = data.get_ref(); let model = ActixAdminModel::create_from_payload( payload, &format!( @@ -27,17 +29,18 @@ pub async fn create_post ), ) .await; - create_or_edit_post::(&session, req, &data, model, None, actix_admin).await + create_or_edit_post::(&session, req, db, model, None, actix_admin).await } -pub async fn edit_post( +pub async fn edit_post( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, payload: Multipart, id: web::Path, ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = &data.get_ref(); let model = ActixAdminModel::create_from_payload( payload, &format!( @@ -47,10 +50,10 @@ pub async fn edit_post( ), ) .await; - create_or_edit_post::( + create_or_edit_post::( &session, req, - &data, + db, model, Some(id.into_inner()), actix_admin, @@ -58,10 +61,10 @@ pub async fn edit_post( .await } -pub async fn create_or_edit_post( +pub async fn create_or_edit_post( session: &Session, req: HttpRequest, - data: &web::Data, + db: web::Data, model_res: Result, id: Option, actix_admin: &ActixAdmin, @@ -76,7 +79,7 @@ pub async fn create_or_edit_post( +pub async fn delete( session: Session, _req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, _text: String, id: web::Path, ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = &data.into_inner(); let entity_name = E::get_entity_name(); let view_model = actix_admin.view_models.get(&entity_name).unwrap(); @@ -23,7 +25,7 @@ pub async fn delete( return render_unauthorized(&ctx, &actix_admin); } - let db = &data.get_db(); + let db = &db.get_ref(); let id = id.into_inner(); let model_result = E::get_entity(db, id).await; let delete_result = E::delete_entity(db, id).await; @@ -53,13 +55,14 @@ pub async fn delete( } } -pub async fn delete_many( +pub async fn delete_many( session: Session, _req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, form: web::Form>, ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = data.get_ref(); let entity_name = E::get_entity_name(); let view_model = actix_admin.view_models.get(&entity_name).unwrap(); @@ -71,7 +74,7 @@ pub async fn delete_many return render_unauthorized(&ctx, &actix_admin); } - let db = &data.get_db(); + let db = &db.get_ref(); let entity_name = E::get_entity_name(); let ids: Vec = form.iter().filter(|el| el.0 == "ids").map(|el| el.1.parse::().unwrap()).collect(); diff --git a/src/routes/file.rs b/src/routes/file.rs index e907f68..a4cd66a 100644 --- a/src/routes/file.rs +++ b/src/routes/file.rs @@ -1,13 +1,14 @@ use actix_web::{web, error, Error, HttpResponse, HttpRequest}; use actix_session::{Session}; +use sea_orm::DatabaseConnection; use tera::{Context}; use crate::prelude::*; use super::{ user_can_access_page, render_unauthorized}; -pub async fn download(req: HttpRequest, session: Session, data: web::Data, params: web::Path<(i32, String)>) -> Result { - let actix_admin = data.get_actix_admin(); - let db = &data.get_db(); +pub async fn download(req: HttpRequest, session: Session, data: web::Data, db: web::Data, params: web::Path<(i32, String)>) -> Result { + let actix_admin = &data.into_inner(); + let db = &db.into_inner(); let ctx = Context::new(); let entity_name = E::get_entity_name(); @@ -41,9 +42,8 @@ pub async fn download(re } -pub async fn delete_file(session: Session, data: web::Data, params: web::Path<(i32, String)>) -> Result { - let actix_admin = data.get_actix_admin(); - let db = &data.get_db(); +pub async fn delete_file(session: Session, data: web::Data, db: web::Data, params: web::Path<(i32, String)>) -> Result { + let actix_admin = &data.into_inner(); let mut ctx = Context::new(); let entity_name = E::get_entity_name(); @@ -54,7 +54,7 @@ pub async fn delete_file let (id, column_name) = params.into_inner(); let mut errors: Vec = Vec::new(); - let result = E::get_entity(db, id).await; + let result = E::get_entity(db.get_ref(), id).await; let mut model; match result { Ok(res) => { @@ -70,7 +70,7 @@ pub async fn delete_file let file_path = format!("{}/{}/{}", actix_admin.configuration.file_upload_directory, E::get_entity_name(), file_name.unwrap_or_default()); std::fs::remove_file(file_path).unwrap(); model.values.remove(&column_name); - let _edit_res = E::edit_entity(db, id, model.clone()).await; + let _edit_res = E::edit_entity(db.get_ref(), id, model.clone()).await; let view_model_field = &view_model.fields.iter().find(|field| field.field_name == column_name).unwrap(); ctx.insert("model_field", view_model_field); diff --git a/src/routes/index.rs b/src/routes/index.rs index b87176f..595f5d8 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -6,8 +6,8 @@ use crate::prelude::*; use super::{ add_auth_context }; -pub fn get_admin_ctx(session: Session, data: &web::Data) -> Context { - let actix_admin = data.get_actix_admin(); +pub fn get_admin_ctx(session: Session, data: &web::Data) -> Context { + let actix_admin = data.get_ref(); let mut ctx = Context::new(); ctx.insert("entity_names", &actix_admin.entity_names); @@ -17,8 +17,8 @@ pub fn get_admin_ctx(session: Session, data: &web::Da ctx } -pub async fn index(session: Session, data: web::Data) -> Result { - let actix_admin = data.get_actix_admin(); +pub async fn index(session: Session, data: web::Data) -> Result { + let actix_admin = &data.into_inner(); let notifications: Vec = Vec::new(); let mut ctx = Context::new(); @@ -33,8 +33,8 @@ pub async fn index(session: Session, data: web::Data< Ok(HttpResponse::Ok().content_type("text/html").body(body)) } -pub async fn not_found(data: web::Data) -> Result { - let body = data.get_actix_admin().tera +pub async fn not_found(data: web::Data) -> Result { + let body = data.get_ref().tera .render("not_found.html", &Context::new()) .map_err(|_| error::ErrorInternalServerError("Template error"))?; Ok(HttpResponse::NotFound().content_type("text/html").body(body)) diff --git a/src/routes/list.rs b/src/routes/list.rs index d81a45e..d413c88 100644 --- a/src/routes/list.rs +++ b/src/routes/list.rs @@ -1,4 +1,5 @@ use std::fmt; +use sea_orm::DatabaseConnection; use urlencoding::decode; use crate::prelude::*; use actix_web::{error, web, Error, HttpRequest, HttpResponse}; @@ -46,12 +47,13 @@ pub fn replace_regex(view_model: &ActixAdminViewModel, models: &mut Vec( +pub async fn list( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = &data.into_inner(); let entity_name = E::get_entity_name(); let view_model: &ActixAdminViewModel = actix_admin.view_models.get(&entity_name).unwrap(); let mut errors: Vec = Vec::new(); @@ -74,7 +76,6 @@ pub async fn list( let render_partial = req.headers().contains_key("HX-Target"); let search = params.search.clone().unwrap_or(String::new()); - let db = data.get_db(); let sort_by = params .sort_by .clone() @@ -94,7 +95,7 @@ pub async fn list( af }).collect(); - let result = E::list(db, page, entities_per_page, actixadminfilters, &search, &sort_by, &sort_order).await; + let result = E::list(&db, page, entities_per_page, actixadminfilters, &search, &sort_by, &sort_order).await; match result { Ok(res) => { diff --git a/src/routes/show.rs b/src/routes/show.rs index 9affa81..bf957ec 100644 --- a/src/routes/show.rs +++ b/src/routes/show.rs @@ -1,6 +1,7 @@ use actix_web::HttpRequest; use actix_web::{error, web, Error, HttpResponse}; use actix_session::{Session}; +use sea_orm::DatabaseConnection; use tera::{Context}; use crate::ActixAdminNotification; @@ -9,9 +10,10 @@ use crate::prelude::*; use super::{Params, DEFAULT_ENTITIES_PER_PAGE}; use super::{ add_auth_context, user_can_access_page, render_unauthorized}; -pub async fn show(session: Session, req: HttpRequest, data: web::Data, id: web::Path) -> Result { - let actix_admin = data.get_actix_admin(); - let db = &data.get_db(); +pub async fn show( + session: Session, req: HttpRequest, data: web::Data, id: web::Path, db: web::Data +) -> Result { + let actix_admin = &data.into_inner(); let mut ctx = Context::new(); let entity_name = E::get_entity_name(); @@ -21,7 +23,7 @@ pub async fn show(sessio } let mut errors: Vec = Vec::new(); - let result = E::get_entity(db, id.into_inner()).await; + let result = E::get_entity(&db, id.into_inner()).await; let model; match result { Ok(res) => { diff --git a/tests/test_setup/helper.rs b/tests/test_setup/helper.rs index ceeaa8b..4ce7827 100644 --- a/tests/test_setup/helper.rs +++ b/tests/test_setup/helper.rs @@ -57,36 +57,17 @@ macro_rules! create_app ( let conn = $db.clone(); let actix_admin_builder = super::create_actix_admin_builder(); let actix_admin = actix_admin_builder.get_actix_admin(); - let app_state = super::AppState { - db: conn, - actix_admin, - }; test::init_service( App::new() - .app_data(actix_web::web::Data::new(app_state.clone())) - .service(actix_admin_builder.get_scope::()) + .app_data(actix_web::web::Data::new(actix_admin)) + .app_data(actix_web::web::Data::new(conn)) + .service(actix_admin_builder.get_scope()) ) .await }); ); -#[derive(Clone)] -pub struct AppState { - pub db: DatabaseConnection, - pub actix_admin: ActixAdmin, -} - -impl ActixAdminAppDataTrait for AppState { - fn get_db(&self) -> &DatabaseConnection { - &self.db - } - - fn get_actix_admin(&self) -> &ActixAdmin { - &self.actix_admin - } -} - pub fn create_actix_admin_builder() -> ActixAdminBuilder { let post_view_model = ActixAdminViewModel::from(Post); let comment_view_model = ActixAdminViewModel::from(Comment); @@ -101,64 +82,66 @@ pub fn create_actix_admin_builder() -> ActixAdminBuilder { }; let mut admin_builder = ActixAdminBuilder::new(configuration); - admin_builder.add_entity::(&post_view_model); - admin_builder.add_entity::(&comment_view_model); + admin_builder.add_entity::(&post_view_model); + admin_builder.add_entity::(&comment_view_model); - admin_builder.add_custom_handler_for_entity::( + admin_builder.add_custom_handler_for_entity::( "Create Comment From Plaintext", "/create_post_from_plaintext", - web::post().to(create_post_from_plaintext::), + web::post().to(create_post_from_plaintext::), false, ); - admin_builder.add_custom_handler_for_entity::( + admin_builder.add_custom_handler_for_entity::( "Create Post From Plaintext", "/create_post_from_plaintext", - web::post().to(create_post_from_plaintext::), + web::post().to(create_post_from_plaintext::), false, ); - admin_builder.add_custom_handler_for_entity::( + admin_builder.add_custom_handler_for_entity::( "Edit Post From Plaintext", "/edit_post_from_plaintext/{id}", - web::post().to(edit_post_from_plaintext::), + web::post().to(edit_post_from_plaintext::), false, ); - admin_builder.add_custom_handler_for_entity::( + admin_builder.add_custom_handler_for_entity::( "Edit Comment From Plaintext", "/edit_post_from_plaintext/{id}", - web::post().to(edit_post_from_plaintext::), + web::post().to(edit_post_from_plaintext::), false, ); admin_builder } -async fn create_post_from_plaintext( +async fn create_post_from_plaintext( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, text: String, ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = data.get_ref(); let model = ActixAdminModel::from(text); - create_or_edit_post::(&session, req, &data, Ok(model), None, actix_admin).await + create_or_edit_post::(&session, req, db, Ok(model), None, actix_admin).await } -async fn edit_post_from_plaintext( +async fn edit_post_from_plaintext( session: Session, req: HttpRequest, - data: web::Data, + data: web::Data, + db: web::Data, text: String, id: web::Path, ) -> Result { - let actix_admin = data.get_actix_admin(); + let actix_admin = data.get_ref(); let model = ActixAdminModel::from(text); - create_or_edit_post::( + create_or_edit_post::( &session, req, - &data, + db, Ok(model), Some(id.into_inner()), actix_admin, diff --git a/tests/test_setup/mod.rs b/tests/test_setup/mod.rs index ff163a3..a9ed404 100644 --- a/tests/test_setup/mod.rs +++ b/tests/test_setup/mod.rs @@ -10,8 +10,7 @@ pub use post::Entity as Post; pub mod prelude { pub use crate::test_setup::helper::{ create_actix_admin_builder, - setup_db, - AppState, + setup_db, BodyTest }; pub use super::comment;