convert from request to admin_model for create_post

This commit is contained in:
Manuel Gugger 2022-05-23 18:32:48 +02:00
parent 55c8a99a3c
commit 88a754eeac
4 changed files with 30 additions and 9 deletions

View File

@ -51,8 +51,8 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
impl From<ActixAdminModel> for ActiveModel { impl From<ActixAdminModel> for ActiveModel {
fn from(model: ActixAdminModel) -> Self { fn from(model: ActixAdminModel) -> Self {
ActiveModel { ActiveModel {
title: Set("test".to_string()), title: Set(model.values.get("title").unwrap().to_string()),
text: Set("test".to_string()), text: Set(model.values.get("text").unwrap().to_string()),
..Default::default() ..Default::default()
} }
} }

View File

@ -15,11 +15,19 @@ const DEFAULT_ENTITIES_PER_PAGE: usize = 5;
macro_rules! hashmap { macro_rules! hashmap {
($( $key: expr => $val: expr ),*) => {{ ($( $key: expr => $val: expr ),*) => {{
let mut map = ::std::collections::HashMap::new(); let mut map = ::std::collections::HashMap::new();
$( map.insert($key, $val); )* $( map.insert($key.to_string(), $val.to_string()); )*
map map
}} }}
} }
#[macro_export]
macro_rules! make_fields {
($($element: ident: $ty: ty),*) => {
$($element: $ty),*
}
}
// globals // globals
lazy_static! { lazy_static! {
static ref TERA: Tera = static ref TERA: Tera =
@ -54,7 +62,7 @@ pub trait ActixAdminModelTrait {
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct ActixAdminModel { pub struct ActixAdminModel {
pub values: HashMap<&'static str, String> pub values: HashMap<String, String>
} }
// ActixAdminViewModel // ActixAdminViewModel
@ -100,6 +108,22 @@ impl ActixAdmin {
} }
} }
impl From<String> for ActixAdminModel {
fn from(string: String) -> Self {
let mut hashmap = HashMap::new();
let key_values: Vec<&str> = string.split('&').collect();
for key_value in key_values {
let mut iter = key_value.splitn(2, '=');
hashmap.insert(iter.next().unwrap().to_string(), iter.next().unwrap().to_string());
}
ActixAdminModel {
// TODO: create dynamically
values: hashmap
}
}
}
pub async fn index<T: AppDataTrait>(data: web::Data<T>) -> Result<HttpResponse, Error> { pub async fn index<T: AppDataTrait>(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();
@ -166,11 +190,8 @@ pub async fn create_post<T: AppDataTrait, E: ActixAdminViewModelTrait>(_req: Htt
let actix_admin = data.get_actix_admin(); let actix_admin = data.get_actix_admin();
let view_model = actix_admin.view_models.get(&entity_name).unwrap(); let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let mut model = ActixAdminModel{ values: HashMap::new() }; let mut admin_model = ActixAdminModel::from(text);
model = E::create_entity(db, model).await; admin_model = E::create_entity(db, admin_model).await;
println!("{}", &entity_name);
println!("{}", &text);
Ok(HttpResponse::Found() Ok(HttpResponse::Found()
.append_header((header::LOCATION, format!("/admin/{}/list", view_model.entity_name))) .append_header((header::LOCATION, format!("/admin/{}/list", view_model.entity_name)))

Binary file not shown.

Binary file not shown.