This commit is contained in:
manuel 2022-05-22 16:46:33 +02:00
parent 2827099708
commit dc60135cb9
3 changed files with 21 additions and 26 deletions

View File

@ -23,6 +23,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
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::*};
use std::collections::HashMap;
use sea_orm::EntityTrait; use sea_orm::EntityTrait;
impl From<Entity> for ActixAdminViewModel { impl From<Entity> for ActixAdminViewModel {
@ -36,24 +37,24 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
#[async_trait(?Send)] #[async_trait(?Send)]
impl ActixAdminViewModelTrait for Entity { impl ActixAdminViewModelTrait for Entity {
async fn list<T: AppDataTrait>(db: DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> { async fn list(self, db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
let model = ActixAdminViewModel::from(Entity); let model = ActixAdminViewModel::from(Entity);
let entity_names = &data.get_actix_admin().entity_names; let entities = Entity::list_model(db, 1, 5).await;
let entities = Entity::list_model(db, 1, 5);
entities entities
} }
async fn create_post(db: DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel{ async fn create_entity(self, db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel {
let new_model = ActiveModel { let new_model = ActiveModel {
title: Set("test".to_string()), title: Set("test".to_string()),
text: Set("test".to_string()), text: Set("test".to_string()),
..Default::default() ..Default::default()
}; };
let insert_operation = Entity::insert(new_model).exec(data.get_db()).await; let insert_operation = Entity::insert(new_model).exec(db).await;
actix_admin::create_post_model(req, &data, view_model) ActixAdminModel{ values: HashMap::new() }
} }
} }
#[async_trait] #[async_trait]
impl ActixAdminModelTrait for Entity { impl ActixAdminModelTrait for Entity {
async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel> { async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel> {
@ -66,13 +67,13 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
.await .await
.expect("could not retrieve entities"); .expect("could not retrieve entities");
// TODO: must be dynamic // TODO: must be dynamic
model_entities = Vec::new(); let mut model_entities = Vec::new();
for entity in entities { for entity in entities {
model_values = HashMap::new(); let mut model_values = HashMap::new();
model_values.insert("title", entity.title); model_values.insert("title", entity.title);
model_values.insert("text", entity.text); model_values.insert("text", entity.text);
model_values.insert("id", entity.id); model_values.insert("id", entity.id.to_string());
vec.push( model_entities.push(
ActixAdminModel { ActixAdminModel {
values: model_values, values: model_values,
}); });

View File

@ -38,21 +38,21 @@ pub trait AppDataTrait {
// ActixAdminModel // ActixAdminModel
#[async_trait] #[async_trait]
pub trait ActixAdminModelTrait: Clone { pub trait ActixAdminModelTrait {
async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str>; async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel>;
fn get_fields() -> Vec<(String, ActixAdminField)>; fn get_fields() -> Vec<(String, ActixAdminField)>;
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct ActixAdminModel { pub struct ActixAdminModel {
values: HashMap<&'static str, &'static str> pub values: HashMap<&'static str, String>
} }
// ActixAdminViewModel // ActixAdminViewModel
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait ActixAdminViewModelTrait { pub trait ActixAdminViewModelTrait {
async fn list(db: DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel>; async fn list(self, db: &DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel>;
async fn create_post(db: DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel; async fn create_entity(self, db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
@ -114,7 +114,7 @@ pub async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, path: w
let entities_per_page = params.entities_per_page.unwrap_or(DEFAULT_ENTITIES_PER_PAGE); let entities_per_page = params.entities_per_page.unwrap_or(DEFAULT_ENTITIES_PER_PAGE);
let db = data.get_db(); let db = data.get_db();
let entities: Vec<ActixAdminModel> = view_model.list(db, page, entities_per_page); let entities: Vec<ActixAdminModel> = Vec::new(); // entity.list(db, page, entities_per_page).await;
let mut ctx = Context::new(); let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names); ctx.insert("entity_names", &entity_names);
@ -130,7 +130,7 @@ pub async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, path: w
Ok(HttpResponse::Ok().content_type("text/html").body(body)) Ok(HttpResponse::Ok().content_type("text/html").body(body))
} }
pub async fn create_get<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, _body: web::Payload, _text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> { pub async fn create_get<T: AppDataTrait>(_req: HttpRequest, data: web::Data<T>, _body: web::Payload, _text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> {
let _db = &data.get_db(); let _db = &data.get_db();
let entity_name: String = entity_name.into_inner(); let entity_name: String = entity_name.into_inner();
println!("{}", &entity_name); println!("{}", &entity_name);
@ -151,7 +151,7 @@ pub async fn create_get<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, _
Ok(HttpResponse::Ok().content_type("text/html").body(body)) Ok(HttpResponse::Ok().content_type("text/html").body(body))
} }
pub async fn create_post<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> { pub async fn create_post<T: AppDataTrait>(_req: HttpRequest, data: web::Data<T>, text: String, entity_name: web::Path<String>) -> Result<HttpResponse, Error> {
let _db = &data.get_db(); let _db = &data.get_db();
let entity_name: String = entity_name.into_inner(); let entity_name: String = entity_name.into_inner();
let actix_admin = data.get_actix_admin(); let actix_admin = data.get_actix_admin();

View File

@ -2,7 +2,8 @@ extern crate serde_derive;
use actix_admin::{ use actix_admin::{
ActixAdmin, ActixAdminViewModel, ActixAdmin, ActixAdminViewModel,
AppDataTrait as ActixAdminAppDataTrait AppDataTrait as ActixAdminAppDataTrait,
ActixAdminViewModelTrait
}; };
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};
@ -67,13 +68,6 @@ fn setup_actix_admin(
.route("/create", web::get().to(actix_admin::create_get::<AppState>)) .route("/create", web::get().to(actix_admin::create_get::<AppState>))
.route("/create", web::post().to(actix_admin::create_post::<AppState>)) .route("/create", web::post().to(actix_admin::create_post::<AppState>))
) )
// .service(
// web::scope(&format!("/{}", comment_view_model.entity_name))
// .route("/list", web::get().to(Comment::list::<AppState>))
// .route("/create", web::get().to(Comment::create_get::<AppState>))
// .route("/create", web::post().to(Comment::create_post::<AppState, comment::Model>))
// )
} }
#[actix_rt::main] #[actix_rt::main]