add delete post action
This commit is contained in:
parent
411c8668eb
commit
7748878c26
@ -96,6 +96,14 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
|
||||
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 {
|
||||
Entity.table_name().to_string()
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use crate::prelude::*;
|
||||
use crate::routes::list;
|
||||
use crate::routes::create_get;
|
||||
use crate::routes::create_post;
|
||||
use crate::routes::delete_post;
|
||||
use crate::routes::index;
|
||||
|
||||
pub struct ActixAdminBuilder {
|
||||
@ -42,11 +43,11 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
|
||||
web::scope(&format!("/{}", E::get_entity_name()))
|
||||
.route("/list", web::get().to(list::<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());
|
||||
//let view_model_cloned = view_model.clone();
|
||||
let key = E::get_entity_name();
|
||||
self.actix_admin.view_models.insert(key, view_model.clone());
|
||||
}
|
||||
|
24
actix_admin/src/routes/delete_post.rs
Normal file
24
actix_admin/src/routes/delete_post.rs
Normal 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())
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
mod create_get;
|
||||
pub use create_get::create_get;
|
||||
|
||||
mod create_post;
|
||||
pub use create_get::create_get;
|
||||
pub use create_post::create_post;
|
||||
|
||||
mod index;
|
||||
@ -9,3 +8,6 @@ pub use index::index;
|
||||
|
||||
mod list;
|
||||
pub use list::list;
|
||||
|
||||
mod delete_post;
|
||||
pub use delete_post::delete_post;
|
@ -12,7 +12,10 @@ pub trait ActixAdminViewModelTrait {
|
||||
page: usize,
|
||||
entities_per_page: usize,
|
||||
) -> Vec<ActixAdminModel>;
|
||||
|
||||
async fn create_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
|
||||
async fn delete_entity(db: &DatabaseConnection, model: ActixAdminModel) -> ActixAdminModel;
|
||||
|
||||
fn get_entity_name() -> String;
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,19 @@
|
||||
{% for model_field in view_model.fields -%}
|
||||
<th>{{ model_field[0] }}</th>
|
||||
{%- endfor %}
|
||||
<th><!-- Delete Action --></th>
|
||||
</tr>
|
||||
{% for entity in entities -%}
|
||||
<tr>
|
||||
{% for model_field in view_model.fields -%}
|
||||
<td>{{ entity.values | get(key=model_field[0]) }}</td>
|
||||
{%- 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>
|
||||
{%- endfor %}
|
||||
</table>
|
||||
|
BIN
database.db-wal
BIN
database.db-wal
Binary file not shown.
Loading…
Reference in New Issue
Block a user