cleaned up code

This commit is contained in:
manuel 2022-05-22 14:36:39 +02:00
parent 50098f93dd
commit 2827099708
6 changed files with 42 additions and 83 deletions

View File

@ -36,29 +36,14 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
#[async_trait(?Send)]
impl ActixAdminViewModelTrait for Entity {
async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error> {
async fn list<T: AppDataTrait>(db: DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel> {
let model = ActixAdminViewModel::from(Entity);
let entity_names = &data.get_actix_admin().entity_names;
let db = data.get_db();
let entities = Entity::list_db(db, 1, 5);
// TODO: Get ViewModel from ActixAdmin to honor individual settings
actix_admin::list_model(req, &data, model, entity_names)
let entities = Entity::list_model(db, 1, 5);
entities
}
async fn create_get<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let entity_names = &data.get_actix_admin().entity_names;
// TODO: Get ViewModel from ActixAdmin to honor individual settings
let model = ActixAdminViewModel::from(Entity);
actix_admin::create_get_model(req, &data, model, entity_names)
}
async fn create_post<T: AppDataTrait, M>(req: HttpRequest, data: web::Data<T>, post_form: web::Form<M>) -> Result<HttpResponse, Error> {
let view_model = ActixAdminViewModel::from(Entity);
let form = post_form.into_inner();
async fn create_post(db: DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel{
let new_model = ActiveModel {
title: Set("test".to_string()),
text: Set("test".to_string()),
@ -71,7 +56,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
}
#[async_trait]
impl ActixAdminModelTrait for Entity {
async fn list_db(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str> {
async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<ActixAdminModel> {
use sea_orm::{ query::* };
let paginator = Entity::find()
.order_by_asc(Column::Id)
@ -80,9 +65,20 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
.fetch_page(page - 1)
.await
.expect("could not retrieve entities");
//entities to ActixAdminModel
vec![
]
// TODO: must be dynamic
model_entities = Vec::new();
for entity in entities {
model_values = HashMap::new();
model_values.insert("title", entity.title);
model_values.insert("text", entity.text);
model_values.insert("id", entity.id);
vec.push(
ActixAdminModel {
values: model_values,
});
}
model_entities
}
fn get_fields() -> Vec<(String, ActixAdminField)> {

View File

@ -7,8 +7,8 @@ const ATTR_META_SKIP: &'static str = "skip";
pub fn get_fields_for_tokenstream(input: proc_macro::TokenStream) -> std::vec::Vec<(syn::Visibility, proc_macro2::Ident)> {
let ast: DeriveInput = syn::parse(input).unwrap();
let (vis, ty, generics) = (&ast.vis, &ast.ident, &ast.generics);
let names_struct_ident = Ident::new(&(ty.to_string() + "FieldStaticStr"), Span::call_site());
let (_vis, ty, _generics) = (&ast.vis, &ast.ident, &ast.generics);
let _names_struct_ident = Ident::new(&(ty.to_string() + "FieldStaticStr"), Span::call_site());
let fields = filter_fields(match ast.data {
syn::Data::Struct(ref s) => &s.fields,

View File

@ -1,17 +1,11 @@
use actix_web::{error, web, Responder, get, post, route, Error, HttpRequest, HttpResponse};
use actix_web::{error, web, Error, HttpRequest, HttpResponse};
use lazy_static::lazy_static;
use sea_orm::{ DatabaseConnection, ModelTrait, ConnectionTrait};
use sea_orm::{ DatabaseConnection};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use actix_web::http::header;
use tera::{Context, Tera};
use std::any::Any;
use std::convert::From;
use async_trait::async_trait;
use sea_orm::ActiveValue::Set;
use sea_orm::{ConnectOptions };
use sea_orm::{entity::*, query::*};
use sea_orm::EntityTrait;
pub use actix_admin_macros::DeriveActixAdminModel;
@ -45,21 +39,20 @@ pub trait AppDataTrait {
// ActixAdminModel
#[async_trait]
pub trait ActixAdminModelTrait: Clone {
async fn list_db(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str>;
async fn list_model(db: &DatabaseConnection, page: usize, posts_per_page: usize) -> Vec<&str>;
fn get_fields() -> Vec<(String, ActixAdminField)>;
}
#[derive(Clone, Debug, Serialize)]
pub struct ActixAdminModel {
values: HashMap<&'static str, &'static str>
}
// ActixAdminViewModel
#[async_trait(?Send)]
pub trait ActixAdminViewModelTrait {
async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error>;
async fn create_get<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>) -> Result<HttpResponse, Error>;
async fn create_post<T: AppDataTrait, M>(req: HttpRequest, data: web::Data<T>, post_form: web::Form<M>) -> Result<HttpResponse, Error>;
async fn list(db: DatabaseConnection, page: usize, entities_per_page: usize) -> Vec<ActixAdminModel>;
async fn create_post(db: DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
}
#[derive(Clone, Debug, Serialize)]
@ -112,22 +105,16 @@ pub async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse,
pub async fn list<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, path: web::Path<String>) -> Result<HttpResponse, Error> {
let entity_name: String = path.into_inner();
let actix_admin = data.get_actix_admin();
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let view_model: &ActixAdminViewModel = actix_admin.view_models.get(&entity_name).unwrap();
let entity_names = &data.get_actix_admin().entity_names;
let db = data.get_db();
//let entities: Vec<&str> = Vec::new(); // E::list_db(db, 1, 5);
// TODO: Get ViewModel from ActixAdmin to honor individual settings
list_model(req, &data, view_model.clone(), entity_names)
}
pub fn list_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_model: ActixAdminViewModel, entity_names: &Vec<String>) -> Result<HttpResponse, Error> {
let params = web::Query::<Params>::from_query(req.query_string()).unwrap();
let page = params.page.unwrap_or(1);
let entities_per_page = params.entities_per_page.unwrap_or(DEFAULT_ENTITIES_PER_PAGE);
let entities: Vec<&str> = Vec::new(); // view_model.get_entities()
let db = data.get_db();
let entities: Vec<ActixAdminModel> = view_model.list(db, page, entities_per_page);
let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names);
@ -143,20 +130,16 @@ pub fn list_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_m
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
pub async fn create_get<T: AppDataTrait>(req: HttpRequest, data: web::Data<T>, mut body: web::Payload, text: String, entity_name: web::Path<(String)>) -> impl Responder {
let db = &data.get_db();
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 entity_name: String = entity_name.into_inner();
println!("{}", &entity_name);
let entity_names = &data.get_actix_admin().entity_names;
// TODO: Get ViewModel from ActixAdmin to honor individual settings
let actix_admin = data.get_actix_admin();
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
create_get_model(req, &data, view_model.clone(), entity_names)
}
pub fn create_get_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_model: ActixAdminViewModel, entity_names: &Vec<String>) -> Result<HttpResponse, Error> {
let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names);
ctx.insert("view_model", &view_model);
@ -168,28 +151,16 @@ pub fn create_get_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>,
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> {
let db = &data.get_db();
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 entity_name: String = entity_name.into_inner();
let actix_admin = data.get_actix_admin();
let entity_names = &actix_admin.entity_names;
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
println!("{}", &entity_name);
println!("{}", &text);
//println!("{}", &body.);
// let new_model = ActiveModel {
// title: Set("test".to_string()),
// text: Set("test".to_string()),
// ..Default::default()
// };
// let insert_operation = M::insert(new_model).exec(data.get_db()).await;
create_post_model(req, &data, view_model.clone())
}
pub fn create_post_model<T: AppDataTrait>(req: HttpRequest, data: &web::Data<T>, view_model: ActixAdminViewModel) -> Result<HttpResponse, Error> {
Ok(HttpResponse::Found()
.append_header((header::LOCATION, format!("/admin/{}/list", view_model.entity_name)))
.finish())

View File

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

View File

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

View File

@ -1,8 +1,8 @@
extern crate serde_derive;
use actix_admin::{
ActixAdmin, ActixAdminViewModel, ActixAdminViewModelTrait,
AppDataTrait as ActixAdminAppDataTrait, ActixAdminModelTrait
ActixAdmin, ActixAdminViewModel,
AppDataTrait as ActixAdminAppDataTrait
};
use actix_session::{CookieSession, Session};
use actix_web::{web, App, HttpResponse, HttpServer, middleware};
@ -10,15 +10,13 @@ use azure_auth::{AppDataTrait as AzureAuthAppDataTrait, AzureAuth, UserInfo};
use oauth2::basic::BasicClient;
use oauth2::RedirectUrl;
use sea_orm::{ConnectOptions, DatabaseConnection};
use sea_orm::{entity::*, query::*};
use sea_orm::EntityTrait;
//use sea_orm::{entity::*, query::*};
use std::env;
use std::time::Duration;
use tera::{Context, Tera};
use std::sync::Arc;
mod entity;
use entity::{Comment, Post, comment, post};
use entity::{Post};
#[derive(Debug, Clone)]
pub struct AppState {
@ -60,8 +58,6 @@ async fn index(session: Session, data: web::Data<AppState>) -> HttpResponse {
// TODO: Generate this with a Macro accepting Tuples of (Entity, viewmodel)
fn setup_actix_admin(
actix_admin: &ActixAdmin,
post_view_model: &ActixAdminViewModel,
//comment_view_model: &ActixAdminViewModel,
) -> actix_web::Scope {
actix_admin
.create_scope::<AppState>()
@ -134,9 +130,7 @@ async fn main() {
.route("/", web::get().to(index))
.service(azure_auth.clone().create_scope::<AppState>())
.service(setup_actix_admin(
&actix_admin,
&post_view_model,
//&comment_view_model,
&actix_admin
))
.wrap(middleware::Logger::default())
})