diff --git a/NOTES.md b/NOTES.md index a3ae7bf..ce5eadb 100644 --- a/NOTES.md +++ b/NOTES.md @@ -134,4 +134,4 @@ As I started by quickly using a django admin template with CSS to jump start the - implement views for app - implement a static implementation for repository - \ No newline at end of file + - eventually AdminRegistry needs to become an `Arc>`, if internal data needs to change *after* creating the main state. diff --git a/src/admin/mod.rs b/src/admin/mod.rs index 8b51036..0fbf5c3 100644 --- a/src/admin/mod.rs +++ b/src/admin/mod.rs @@ -10,13 +10,10 @@ pub mod views; pub fn routes() -> Router { Router::new() .route("/", get(views::index).post(views::index_action)) - .route("/app/:app_key", get(views::list_app)) + .route("/app/:app", get(views::list_app)) + .route("/app/:app/model/:model", get(views::list_item_collection)) .route( - "/app/:app_key/models/:model_key", - get(views::list_item_collection), - ) - .route( - "/app/:app_key/models/:model_key/detail/:id", + "/app/:app/model/:model/detail/:id", get(views::item_details), ) } diff --git a/src/admin/state.rs b/src/admin/state.rs index 73a321e..5f5a752 100644 --- a/src/admin/state.rs +++ b/src/admin/state.rs @@ -65,8 +65,11 @@ impl AdminRegistry { fn model_from_internal(&self, internal_model: &internal::AdminModel) -> AdminModel { AdminModel { key: internal_model.model_key.clone(), - name: internal_model.model_key.clone(), - admin_url: "".to_owned(), + name: internal_model.name.clone(), + admin_url: format!( + "{}/app/{}/model/{}", + self.base_path, internal_model.app_key, internal_model.model_key + ), view_only: false, add_url: None, } @@ -111,6 +114,14 @@ impl AdminRegistry { self.repositories.insert(model_key, Box::new(repository)); Ok(()) } + + pub fn get_repository(&self, model_key: &str) -> Result<&Box, String> { + if let Some(repo) = self.repositories.get(model_key) { + return Ok(repo); + } else { + return Err("Couldn't find repository".to_owned()); + } + } } pub mod config { @@ -139,7 +150,7 @@ mod internal { fn from(value: super::config::AdminModelConfig) -> Self { AdminModel { app_key: value.app_key, - model_key: value.name.clone(), + model_key: slug::slugify(value.name.clone()), name: value.name, } } diff --git a/src/admin/views.rs b/src/admin/views.rs index 9e46be9..5a5e227 100644 --- a/src/admin/views.rs +++ b/src/admin/views.rs @@ -2,11 +2,13 @@ use std::sync::Arc; use axum::extract::Path; use axum::{extract::State, response::IntoResponse, Form}; +use log::info; use crate::admin::domain::{AdminApp, AdminModel}; use crate::admin::state; use crate::service::templates; use serde::{Deserialize, Serialize}; +use serde_json::Value; #[derive(Deserialize)] pub struct Question { @@ -38,6 +40,8 @@ pub struct AdminContext { pub request: AdminRequest, pub available_apps: Vec, + pub item_list: Option, + pub item: Option, } impl Default for AdminContext { @@ -56,6 +60,8 @@ impl Default for AdminContext { request: AdminRequest { path: "".to_owned(), }, + item_list: None, + item: None, } } } @@ -88,9 +94,22 @@ pub async fn list_app( // List Items renders the entire list item page. pub async fn list_item_collection( templates: State, + registry: State>, Path((app_key, model_key)): Path<(String, String)>, ) -> impl IntoResponse { - templates.render_html("admin/items/list_items.html", ()) + info!("list_item_collection {} for model {}", app_key, model_key); + let context = if let Ok(repo) = registry.get_repository(&model_key) { + // we should consider using Vec instead in get_list. + AdminContext { + item_list: Some(repo.get_list()), + ..Default::default() + } + } else { + AdminContext { + ..Default::default() + } + }; + templates.render_html("admin/items/item_list.html", context) } // Items Action is a POST to an item list. By default these are actions, that work on a list of items as input. @@ -116,3 +135,8 @@ pub async fn item_action( ) -> impl IntoResponse { "There is your answer!".to_owned() } + +pub async fn debug_view(Path(data): Path) -> impl IntoResponse { + println!("debug: {}", data); + "Debug!".to_owned() +} diff --git a/templates/admin/items/item_list.html b/templates/admin/items/item_list.html new file mode 100644 index 0000000..92c5e7e --- /dev/null +++ b/templates/admin/items/item_list.html @@ -0,0 +1,13 @@ +{% extends "admin/base.html" %} + +{% block content %} +{% if item_list %} +
    + {% for item in item_list %} +
  • {{ item.name }}
  • + {% endfor %} +
+{% else %} + No Items found. +{% endif %} +{% endblock content %} \ No newline at end of file diff --git a/templates/admin/items/list_items.html b/templates/admin/items/list_items.html deleted file mode 100644 index 82121e2..0000000 --- a/templates/admin/items/list_items.html +++ /dev/null @@ -1,5 +0,0 @@ -{% if item_list %} - {% for item in item_list %} - poop: {{ item}} - {% endfor %} -{% endif %} \ No newline at end of file