add prelude

This commit is contained in:
Manuel Gugger 2022-05-27 13:13:44 +02:00
parent 6dc1401550
commit 411c8668eb
10 changed files with 35 additions and 45 deletions

View File

@ -44,7 +44,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
use std::convert::From; use std::convert::From;
use async_trait::async_trait; use async_trait::async_trait;
use actix_web::{web, HttpResponse, HttpRequest, Error}; use actix_web::{web, HttpResponse, HttpRequest, Error};
use actix_admin::{ ActixAdminField, ActixAdminModelTrait, ActixAdminViewModelTrait, ActixAdminViewModel, ActixAdminModel, AppDataTrait , hashmap}; use actix_admin::prelude::*;
use sea_orm::ActiveValue::Set; use sea_orm::ActiveValue::Set;
use sea_orm::{ConnectOptions, DatabaseConnection}; use sea_orm::{ConnectOptions, DatabaseConnection};
use sea_orm::{entity::*, query::*}; use sea_orm::{entity::*, query::*};
@ -64,12 +64,8 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
impl From<Model> for ActixAdminModel { impl From<Model> for ActixAdminModel {
fn from(model: Model) -> Self { fn from(model: Model) -> Self {
ActixAdminModel { ActixAdminModel {
// TODO: create dynamically
values: hashmap![ values: hashmap![
#(#fields_for_from_model),* #(#fields_for_from_model),*
// "title" => model.title,
// "text" => model.text,
// "id" => model.id.to_string()
] ]
} }
} }

View File

@ -4,10 +4,7 @@
use actix_web::{web}; use actix_web::{web};
use std::collections::HashMap; use std::collections::HashMap;
use crate::ActixAdmin; use crate::prelude::*;
use crate::ActixAdminViewModelTrait;
use crate::ActixAdminViewModel;
use crate::AppDataTrait;
use crate::routes::list; use crate::routes::list;
use crate::routes::create_get; use crate::routes::create_get;
@ -21,8 +18,8 @@ pub struct ActixAdminBuilder {
pub trait ActixAdminBuilderTrait { pub trait ActixAdminBuilderTrait {
fn new() -> Self; fn new() -> Self;
fn add_entity<T: AppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static>(&mut self, view_model: &ActixAdminViewModel); fn add_entity<T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static>(&mut self, view_model: &ActixAdminViewModel);
fn get_scope<T: AppDataTrait + 'static>(self) -> actix_web::Scope; fn get_scope<T: ActixAdminAppDataTrait + 'static>(self) -> actix_web::Scope;
fn get_actix_admin(&self) -> ActixAdmin; fn get_actix_admin(&self) -> ActixAdmin;
} }
@ -37,7 +34,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
} }
} }
fn add_entity<T: AppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static>( fn add_entity<T: ActixAdminAppDataTrait + 'static, E: ActixAdminViewModelTrait + 'static>(
&mut self, &mut self,
view_model: &ActixAdminViewModel, view_model: &ActixAdminViewModel,
) { ) {
@ -54,7 +51,7 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
self.actix_admin.view_models.insert(key, view_model.clone()); self.actix_admin.view_models.insert(key, view_model.clone());
} }
fn get_scope<T: AppDataTrait + 'static>(self) -> actix_web::Scope { fn get_scope<T: ActixAdminAppDataTrait + 'static>(self) -> actix_web::Scope {
let mut scope = web::scope("/admin").route("/", web::get().to(index::<T>)); let mut scope = web::scope("/admin").route("/", web::get().to(index::<T>));
for entity_scope in self.scopes { for entity_scope in self.scopes {
scope = scope.service(entity_scope); scope = scope.service(entity_scope);

View File

@ -4,21 +4,22 @@ use serde::{Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use tera::{Tera}; use tera::{Tera};
pub use actix_admin_macros::DeriveActixAdminModel; pub mod view_model;
pub mod model;
pub mod routes;
pub mod builder;
mod view_model; pub mod prelude {
pub use view_model::ActixAdminViewModel; pub use crate::builder::{ ActixAdminBuilder, ActixAdminBuilderTrait};
pub use view_model::ActixAdminViewModelTrait; pub use crate::model::{ ActixAdminModel, ActixAdminModelTrait};
pub use crate::view_model::{ ActixAdminViewModel, ActixAdminViewModelTrait};
pub use actix_admin_macros::{ DeriveActixAdminModel };
pub use crate::{ ActixAdminAppDataTrait, ActixAdmin};
pub use crate::{ hashmap };
pub use crate::{ ActixAdminField };
}
mod model; use crate::prelude::*;
pub use model::ActixAdminModel;
pub use model::ActixAdminModelTrait;
mod builder;
pub use builder::ActixAdminBuilder;
pub use builder::ActixAdminBuilderTrait;
mod routes;
#[macro_export] #[macro_export]
macro_rules! hashmap { macro_rules! hashmap {
@ -42,7 +43,7 @@ pub enum ActixAdminField {
} }
// AppDataTrait // AppDataTrait
pub trait AppDataTrait { pub trait ActixAdminAppDataTrait {
fn get_db(&self) -> &DatabaseConnection; fn get_db(&self) -> &DatabaseConnection;
fn get_actix_admin(&self) -> &ActixAdmin; fn get_actix_admin(&self) -> &ActixAdmin;
} }

View File

@ -1,11 +1,11 @@
use actix_web::{error, web, Error, HttpRequest, HttpResponse}; use actix_web::{error, web, Error, HttpRequest, HttpResponse};
use tera::{Context}; use tera::{Context};
use crate::AppDataTrait; use crate::prelude::*;
use crate::ActixAdminViewModelTrait;
use crate::TERA; use crate::TERA;
pub async fn create_get<T: AppDataTrait, E: ActixAdminViewModelTrait>( pub async fn create_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest, _req: HttpRequest,
data: web::Data<T>, data: web::Data<T>,
_body: web::Payload, _body: web::Payload,

View File

@ -1,11 +1,9 @@
use actix_web::http::header; use actix_web::http::header;
use actix_web::{web, Error, HttpRequest, HttpResponse}; use actix_web::{web, Error, HttpRequest, HttpResponse};
use crate::AppDataTrait; use crate::prelude::*;
use crate::ActixAdminViewModelTrait;
use crate::ActixAdminModel;
pub async fn create_post<T: AppDataTrait, E: ActixAdminViewModelTrait>( pub async fn create_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest, _req: HttpRequest,
data: web::Data<T>, data: web::Data<T>,
text: String, text: String,

View File

@ -1,10 +1,11 @@
use actix_web::{error, web, Error, HttpResponse}; use actix_web::{error, web, Error, HttpResponse};
use tera::{Context}; use tera::{Context};
use crate::AppDataTrait; use crate::prelude::*;
use crate::TERA; use crate::TERA;
pub async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Error> { pub async fn index<T: ActixAdminAppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Error> {
let entity_names = &data.get_actix_admin().entity_names; let entity_names = &data.get_actix_admin().entity_names;
let mut ctx = Context::new(); let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names); ctx.insert("entity_names", &entity_names);

View File

@ -2,7 +2,8 @@ use actix_web::{error, web, Error, HttpRequest, HttpResponse};
use serde::{Deserialize}; use serde::{Deserialize};
use tera::{Context}; use tera::{Context};
use crate::AppDataTrait; use crate::prelude::*;
use crate::ActixAdminViewModelTrait; use crate::ActixAdminViewModelTrait;
use crate::ActixAdminViewModel; use crate::ActixAdminViewModel;
use crate::ActixAdminModel; use crate::ActixAdminModel;
@ -16,7 +17,7 @@ pub struct Params {
entities_per_page: Option<usize>, entities_per_page: Option<usize>,
} }
pub async fn list<T: AppDataTrait, E: ActixAdminViewModelTrait>( pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
req: HttpRequest, req: HttpRequest,
data: web::Data<T>, data: web::Data<T>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {

View File

@ -1,7 +1,6 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use actix_admin::prelude::*;
use actix_admin::{ DeriveActixAdminModel };
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)]
#[sea_orm(table_name = "comment")] #[sea_orm(table_name = "comment")]

View File

@ -1,6 +1,6 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use actix_admin::{ DeriveActixAdminModel }; use actix_admin::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdminModel)]
#[sea_orm(table_name = "post")] #[sea_orm(table_name = "post")]

View File

@ -1,9 +1,6 @@
extern crate serde_derive; extern crate serde_derive;
use actix_admin::{ use actix_admin::prelude::*;
ActixAdmin, ActixAdminBuilder, ActixAdminBuilderTrait, ActixAdminViewModel,
AppDataTrait as ActixAdminAppDataTrait,
};
use actix_session::{CookieSession, Session}; use actix_session::{CookieSession, Session};
use actix_web::{web, App, HttpResponse, HttpServer, middleware}; use actix_web::{web, App, HttpResponse, HttpServer, middleware};
use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo}; use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo};