implement initial delete_many

This commit is contained in:
Manuel Gugger 2022-08-01 14:39:14 +02:00
parent d0cc19d65c
commit a222d3e01b
10 changed files with 56 additions and 53 deletions

View File

@ -142,9 +142,7 @@ pub fn derive_crud_fns(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
}
async fn delete_entity(db: &DatabaseConnection, id: i32) -> bool {
// TODO: separate primary key from other keys
let entity = Entity::find_by_id(id).one(db).await.unwrap().unwrap();
let result = entity.delete(db).await;
let result = Entity::delete_by_id(id).exec(db).await;
match result {
Ok(_) => true,

View File

@ -3,7 +3,7 @@ use std::collections::HashMap;
use crate::prelude::*;
use crate::routes::{create_get, create_post, delete_post, delete_many_post, edit_get, edit_post, index, list};
use crate::routes::{create_get, create_post, delete, delete_many, edit_get, edit_post, index, list};
pub struct ActixAdminBuilder {
pub scopes: Vec<actix_web::Scope>,
@ -42,8 +42,8 @@ impl ActixAdminBuilderTrait for ActixAdminBuilder {
.route("/create", web::post().to(create_post::<T, E>))
.route("/edit/{id}", web::get().to(edit_get::<T, E>))
.route("/edit/{id}", web::post().to(edit_post::<T, E>))
.route("/delete/{id}", web::post().to(delete_post::<T, E>))
.route("/delete_many", web::post().to(delete_many_post::<T, E>))
.route("/delete", web::delete().to(delete_many::<T, E>))
.route("/delete/{id}", web::delete().to(delete::<T, E>))
);
self.actix_admin.entity_names.push(E::get_entity_name());

View File

@ -32,7 +32,7 @@ pub async fn create_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
else {
Ok(HttpResponse::Found()
Ok(HttpResponse::SeeOther()
.append_header((
header::LOCATION,
format!("/admin/{}/list", view_model.entity_name),

View File

@ -0,0 +1,44 @@
use actix_web::{web, Error, HttpRequest, HttpResponse};
use actix_web::http::header;
use crate::prelude::*;
pub async fn delete<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 _result = E::delete_entity(db, id.into_inner()).await;
Ok(HttpResponse::Ok()
.finish())
}
pub async fn delete_many<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 entity_ids: Vec<i32> = text
.split("&")
.filter(|id| !id.is_empty())
.map(|id_str| id_str.replace("ids=", "").parse::<i32>().unwrap()
).collect();
// TODO: implement delete_many
for id in entity_ids {
let _result = E::delete_entity(db, id).await;
}
Ok(HttpResponse::SeeOther()
.append_header((
header::LOCATION,
format!("/admin/{}/list?render_partial=true", entity_name),
))
.finish())
}

View File

@ -1,21 +0,0 @@
use actix_web::{web, Error, HttpRequest, HttpResponse};
use serde::{Deserialize};
use actix_web::http::header;
use crate::prelude::*;
pub async fn delete_many_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();
println!("{:?}", text);
Ok(HttpResponse::Found()
.append_header((
header::LOCATION,
format!("/admin/{}/list?render_partial=true", entity_name),
))
.finish())
}

View File

@ -1,17 +0,0 @@
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,
id: web::Path<i32>
) -> Result<HttpResponse, Error> {
let db = &data.get_db();
let _result = E::delete_entity(db, id.into_inner()).await;
Ok(HttpResponse::Ok()
.finish())
}

View File

@ -33,7 +33,7 @@ pub async fn edit_post<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
Ok(HttpResponse::Ok().content_type("text/html").body(body))
}
else {
Ok(HttpResponse::Found()
Ok(HttpResponse::SeeOther()
.append_header((
header::LOCATION,
format!("/admin/{}/list", view_model.entity_name),

View File

@ -9,10 +9,8 @@ pub use index::index;
mod list;
pub use list::list;
mod delete_post;
mod delete_many_post;
pub use delete_post::delete_post;
pub use delete_many_post::delete_many_post;
mod delete;
pub use delete::{ delete, delete_many };
mod edit_get;
mod edit_post;

View File

@ -12,7 +12,7 @@
</summary>
<ul role="listbox">
<li><a href="create">Create</a></li>
<li><a href="#" hx-post="delete_many">Delete Selected</a></li>
<li><a href="#" hx-delete="delete">Delete Selected</a></li>
</ul>
</details>
</div>
@ -32,7 +32,8 @@
<thead>
<tr>
<th>
<div class="htmx-indicator">{% include "spinner.svg" %}</div>
<input type="checkbox">
<!-- <div class="htmx-indicator">{% include "spinner.svg" %}</div>-->
</th>
<th>{{ view_model.primary_key | title }}</th>
{% for model_field in view_model.fields -%}
@ -54,7 +55,7 @@
{%- endfor %}
<td>
<a href="edit/{{ entity.primary_key }}">&#9998;</a>
<a hx-post="delete/{{ entity.primary_key }}">&#128465;</a>
<a hx-delete="delete/{{ entity.primary_key }}">&#128465;</a>
</td>
</tr>
{%- endfor %}

Binary file not shown.