refactor: update

This commit is contained in:
Gabor Körber 2024-02-14 13:23:10 +01:00
parent 1c6317808a
commit b3278bff62
2 changed files with 40 additions and 1 deletions

View File

@ -15,7 +15,10 @@ pub fn routes() -> Router<AppState> {
"/app/:app/model/:model/add", "/app/:app/model/:model/add",
get(views::new_item).post(views::create_item), get(views::new_item).post(views::create_item),
) )
.route("/app/:app/model/:model/change/:id", get(views::change_item)) .route(
"/app/:app/model/:model/change/:id",
get(views::change_item).patch(views::update_item),
)
.route( .route(
"/app/:app/model/:model/detail/:id", "/app/:app/model/:model/detail/:id",
get(views::view_item_details), get(views::view_item_details),

View File

@ -239,6 +239,7 @@ pub async fn create_item(
templates.render_html("admin/items/item_create.jinja", context) templates.render_html("admin/items/item_create.jinja", context)
} }
/// Change is the GET version.
pub async fn change_item( pub async fn change_item(
templates: State<templates::Templates>, templates: State<templates::Templates>,
registry: State<Arc<state::AdminRegistry>>, registry: State<Arc<state::AdminRegistry>>,
@ -270,6 +271,41 @@ pub async fn change_item(
templates.render_html("admin/items/item_change.jinja", context) templates.render_html("admin/items/item_change.jinja", context)
} }
pub async fn update_item(
templates: State<templates::Templates>,
registry: State<Arc<state::AdminRegistry>>,
headers: HeaderMap,
Path((app_key, model_key, id)): Path<(String, String, String)>,
Form(form): Form<Value>,
) -> impl IntoResponse {
let context = if let Ok(repo) = registry.get_repository(&app_key, &model_key) {
let mut repo = repo.lock().await;
let admin_model = registry
.get_model(&app_key, &model_key)
.expect("Admin Model not found?");
let key: LookupKey = id.into();
let result = repo.update(&admin_model, key, form).await;
AdminContext {
base: base_template(&headers),
available_apps: registry.get_apps(),
item_info: Some(repo.info(&admin_model).await),
item_list: repo.list(&admin_model).await,
item: result,
item_model: Some(admin_model),
..Default::default()
}
} else {
AdminContext {
base: base_template(&headers),
available_apps: registry.get_apps(),
..Default::default()
}
};
templates.render_html("admin/items/item_change.jinja", context)
}
// Item Action allows running an action on one single dataset. // Item Action allows running an action on one single dataset.
pub async fn item_action( pub async fn item_action(
Path((app_key, model_key, model_id)): Path<(String, String, String)>, Path((app_key, model_key, model_id)): Path<(String, String, String)>,