add delete post action

This commit is contained in:
Manuel Gugger 2022-05-27 14:10:35 +02:00
parent 411c8668eb
commit 7748878c26
7 changed files with 50 additions and 5 deletions

View File

@ -96,6 +96,14 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
model model
} }
async fn delete_entity(db: &DatabaseConnection, mut model: ActixAdminModel) -> ActixAdminModel {
// TODO: separate primary key from other keys
let entity = Entity::find_by_id(model.get_value::<i32>("id").unwrap()).one(db).await.unwrap().unwrap();
let delete_operation = entity.delete(db).await;
model
}
fn get_entity_name() -> String { fn get_entity_name() -> String {
Entity.table_name().to_string() Entity.table_name().to_string()
} }

View File

@ -9,6 +9,7 @@ use crate::prelude::*;
use crate::routes::list; use crate::routes::list;
use crate::routes::create_get; use crate::routes::create_get;
use crate::routes::create_post; use crate::routes::create_post;
use crate::routes::delete_post;
use crate::routes::index; use crate::routes::index;
pub struct ActixAdminBuilder { pub struct ActixAdminBuilder {
@ -42,11 +43,11 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
web::scope(&format!("/{}", E::get_entity_name())) web::scope(&format!("/{}", E::get_entity_name()))
.route("/list", web::get().to(list::<T, E>)) .route("/list", web::get().to(list::<T, E>))
.route("/create", web::get().to(create_get::<T, E>)) .route("/create", web::get().to(create_get::<T, E>))
.route("/create", web::post().to(create_post::<T, E>)), .route("/create", web::post().to(create_post::<T, E>))
.route("/delete/{id}", web::post().to(delete_post::<T, E>)),
); );
self.actix_admin.entity_names.push(E::get_entity_name()); self.actix_admin.entity_names.push(E::get_entity_name());
//let view_model_cloned = view_model.clone();
let key = E::get_entity_name(); let key = E::get_entity_name();
self.actix_admin.view_models.insert(key, view_model.clone()); self.actix_admin.view_models.insert(key, view_model.clone());
} }

View File

@ -0,0 +1,24 @@
use actix_web::http::header;
use actix_web::{web, Error, HttpRequest, HttpResponse};
use crate::prelude::*;
pub async fn delete_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
_req: HttpRequest,
data: web::Data<T>,
text: String,
) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let entity_name = E::get_entity_name();
let actix_admin = data.get_actix_admin();
let view_model = actix_admin.view_models.get(&entity_name).unwrap();
let mut admin_model = ActixAdminModel::from(text);
admin_model = E::delete_entity(db, admin_model).await;
Ok(HttpResponse::Found()
.append_header((
header::LOCATION,
format!("/admin/{}/list", view_model.entity_name),
))
.finish())
}

View File

@ -1,7 +1,6 @@
mod create_get; mod create_get;
pub use create_get::create_get;
mod create_post; mod create_post;
pub use create_get::create_get;
pub use create_post::create_post; pub use create_post::create_post;
mod index; mod index;
@ -9,3 +8,6 @@ pub use index::index;
mod list; mod list;
pub use list::list; pub use list::list;
mod delete_post;
pub use delete_post::delete_post;

View File

@ -12,7 +12,10 @@ pub trait ActixAdminViewModelTrait {
page: usize, page: usize,
entities_per_page: usize, entities_per_page: usize,
) -> Vec<ActixAdminModel>; ) -> Vec<ActixAdminModel>;
async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel; async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
async fn delete_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
fn get_entity_name() -> String; fn get_entity_name() -> String;
} }

View File

@ -7,12 +7,19 @@
{% for model_field in view_model.fields -%} {% for model_field in view_model.fields -%}
<th>{{ model_field[0] }}</th> <th>{{ model_field[0] }}</th>
{%- endfor %} {%- endfor %}
<th><!-- Delete Action --></th>
</tr> </tr>
{% for entity in entities -%} {% for entity in entities -%}
<tr> <tr>
{% for model_field in view_model.fields -%} {% for model_field in view_model.fields -%}
<td>{{ entity.values | get(key=model_field[0]) }}</td> <td>{{ entity.values | get(key=model_field[0]) }}</td>
{%- endfor %} {%- endfor %}
<td>
<form method="post" action="delete/{{ entity.values | get(key=view_model.fields[0][0]) }}">
<input type="hidden" id="{{ view_model.fields[0][0] }}" name="{{ view_model.fields[0][0] }}" value="{{ entity.values | get(key=view_model.fields[0][0]) }}">
<button type="submit" class="outline">Delete</button>
</form>
</td>
</tr> </tr>
{%- endfor %} {%- endfor %}
</table> </table>

Binary file not shown.