refactor into create_or_edit

This commit is contained in:
Manuel Gugger 2022-08-01 15:01:18 +02:00
parent a222d3e01b
commit 89baa30467
6 changed files with 51 additions and 99 deletions

View File

@ -1,34 +0,0 @@
use actix_web::{error, web, Error, HttpRequest, HttpResponse};
use tera::{Context};
use crate::prelude::*;
use crate::TERA;
pub async fn create_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest,
data: web::Data<T>,
_body: web::Payload,
text: String,
) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let entity_name = E::get_entity_name();
let entity_names = &data.get_actix_admin().entity_names;
let actix_admin = data.get_actix_admin();
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let model = ActixAdminModel::from(text);
let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names);
ctx.insert("view_model", &view_model);
ctx.insert("select_lists", &E::get_select_lists(db).await);
ctx.insert("list_link", &E::get_list_link(&entity_name));
ctx.insert("model", &model);
let body = TERA
.render("create_or_edit.html", &ctx)
.map_err(|err| error::ErrorInternalServerError(err))?;
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}

View File

@ -5,6 +5,18 @@ use crate::prelude::*;
use crate::TERA; use crate::TERA;
pub async fn create_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest,
data: web::Data<T>,
_body: web::Payload,
text: String,
) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let model = ActixAdminModel::from(text);
create_or_edit_get::<T, E>(&data, db, model).await
}
pub async fn edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>( pub async fn edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest, _req: HttpRequest,
data: web::Data<T>, data: web::Data<T>,
@ -12,6 +24,12 @@ pub async fn edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
id: web::Path<i32> id: web::Path<i32>
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let db = &data.get_db(); let db = &data.get_db();
let model = E::get_entity(db, id.into_inner()).await;
create_or_edit_get::<T, E>(&data, db, model).await
}
async fn create_or_edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(data: &web::Data<T>, db: &sea_orm::DatabaseConnection, model: ActixAdminModel) -> Result<HttpResponse, Error>{
let entity_name = E::get_entity_name(); let entity_name = E::get_entity_name();
let entity_names = &data.get_actix_admin().entity_names; let entity_names = &data.get_actix_admin().entity_names;
@ -19,14 +37,12 @@ pub async fn edit_get<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
let view_model = actix_admin.view_models.get(&entity_name).unwrap(); let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let model = E::get_entity(db, id.into_inner()).await;
let mut ctx = Context::new(); let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names); ctx.insert("entity_names", &entity_names);
ctx.insert("view_model", &view_model); ctx.insert("view_model", &view_model);
ctx.insert("model", &model);
ctx.insert("select_lists", &E::get_select_lists(db).await); ctx.insert("select_lists", &E::get_select_lists(db).await);
ctx.insert("list_link", &E::get_list_link(&entity_name)); ctx.insert("list_link", &E::get_list_link(&entity_name));
ctx.insert("model", &model);
let body = TERA let body = TERA
.render("create_or_edit.html", &ctx) .render("create_or_edit.html", &ctx)

View File

@ -11,12 +11,30 @@ pub async fn create_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>
text: String, text: String,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let db = &data.get_db(); let db = &data.get_db();
let mut model = ActixAdminModel::from(text);
model = E::create_entity(db, model).await;
create_or_edit_post::<T, E>(&data, db, model).await
}
pub async fn edit_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest,
data: web::Data<T>,
text: String,
id: web::Path<i32>
) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let mut model = ActixAdminModel::from(text);
model = E::edit_entity(db, id.into_inner(), model).await;
create_or_edit_post::<T, E>(&data, db, model).await
}
async fn create_or_edit_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(data: &web::Data<T>, db: &sea_orm::DatabaseConnection, model: ActixAdminModel) -> Result<HttpResponse, Error> {
let entity_name = E::get_entity_name(); let entity_name = E::get_entity_name();
let entity_names = &data.get_actix_admin().entity_names; let entity_names = &data.get_actix_admin().entity_names;
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::from(text);
model = E::create_entity(db, model).await;
if model.has_errors() { if model.has_errors() {
let mut ctx = Context::new(); let mut ctx = Context::new();
@ -33,10 +51,10 @@ pub async fn create_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>
} }
else { else {
Ok(HttpResponse::SeeOther() Ok(HttpResponse::SeeOther()
.append_header(( .append_header((
header::LOCATION, header::LOCATION,
format!("/admin/{}/list", view_model.entity_name), format!("/admin/{}/list", view_model.entity_name),
)) ))
.finish()) .finish())
} }
} }

View File

@ -1,43 +0,0 @@
use actix_web::http::header;
use actix_web::{web, error, Error, HttpRequest, HttpResponse};
use tera::{Context};
use crate::TERA;
use crate::prelude::*;
pub async fn edit_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest,
data: web::Data<T>,
text: String,
id: web::Path<i32>
) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let entity_name = E::get_entity_name();
let actix_admin = data.get_actix_admin();
let entity_names = &data.get_actix_admin().entity_names;
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let mut model = ActixAdminModel::from(text);
model = E::edit_entity(db, id.into_inner(), model).await;
if model.has_errors() {
let mut ctx = Context::new();
ctx.insert("entity_names", &entity_names);
ctx.insert("view_model", &view_model);
ctx.insert("model", &model);
ctx.insert("select_lists", &E::get_select_lists(db).await);
ctx.insert("list_link", &E::get_list_link(&entity_name));
let body = TERA
.render("create_or_edit.html", &ctx)
.map_err(|err| error::ErrorInternalServerError(err))?;
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
else {
Ok(HttpResponse::SeeOther()
.append_header((
header::LOCATION,
format!("/admin/{}/list", view_model.entity_name),
))
.finish())
}
}

View File

@ -1,7 +1,8 @@
mod create_get; mod create_or_edit_get;
mod create_post; pub use create_or_edit_get::{create_get, edit_get};
pub use create_get::create_get;
pub use create_post::create_post; mod create_or_edit_post;
pub use create_or_edit_post::{ create_post, edit_post };
mod index; mod index;
pub use index::index; pub use index::index;
@ -11,9 +12,3 @@ pub use list::list;
mod delete; mod delete;
pub use delete::{ delete, delete_many }; pub use delete::{ delete, delete_many };
mod edit_get;
mod edit_post;
pub use edit_get::edit_get;
pub use edit_post::edit_post;

Binary file not shown.