use maud, make inventory work

This commit is contained in:
2023-05-08 22:31:01 +02:00
parent 7dd39ca2a5
commit 3264af5c65
7 changed files with 483 additions and 263 deletions

View File

@@ -1,6 +1,15 @@
use axum::{extract::Path, http::StatusCode, response::Html, routing::get, Router};
#![allow(unused_imports)]
use axum::{
extract::Path,
http::StatusCode,
response::Html,
routing::{get, post},
Router,
};
use sqlx::sqlite::SqlitePoolOptions;
use tracing_subscriber;
use futures::TryStreamExt;
use uuid::Uuid;
@@ -13,25 +22,34 @@ use crate::components::*;
use crate::models::*;
pub struct State {
pub has_active_category: bool,
pub active_category_id: Option<Uuid>,
}
impl State {
pub fn new() -> Self {
State {
has_active_category: false,
active_category_id: None,
}
}
}
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
// build our application with a route
let app = Router::new()
.route("/", get(root))
.route("/trips/", get(trips))
.route("/inventory/", get(inventory_inactive))
.route("/inventory/category/:id", get(inventory_active));
.route("/inventory/category/:id", get(inventory_active))
// .route(
// "/inventory/category/:id/items",
// post(htmx_inventory_category_items),
// );
;
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
tracing::debug!("listening on {}", addr);
@@ -64,13 +82,13 @@ async fn inventory(
active_id: Option<String>,
) -> Result<(StatusCode, Html<String>), (StatusCode, Html<String>)> {
let mut state: State = State::new();
state.has_active_category = active_id.is_some();
let active_id = active_id
.map(|id| Uuid::try_parse(&id))
.transpose()
.map_err(|e| (StatusCode::BAD_REQUEST, Html::from(e.to_string())))?;
state.active_category_id = active_id;
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect("sqlite:///home/hannes-private/sync/items/items.sqlite")
@@ -145,3 +163,44 @@ async fn trips() -> Result<(StatusCode, Html<String>), (StatusCode, Html<String>
Html::from(Root::build(TripList::build(trips).into(), TopLevelPage::Trips).to_string()),
))
}
// async fn htmx_inventory_category_items(
// Path(id): Path<String>,
// ) -> Result<(StatusCode, Html<String>), (StatusCode, Html<String>)> {
// let pool = SqlitePoolOptions::new()
// .max_connections(5)
// .connect("sqlite:///home/hannes-private/sync/items/items.sqlite")
// .await
// .unwrap();
// let items = sqlx::query(&format!(
// "SELECT
// i.id, i.name, i.description, i.weight, i.category_id
// FROM inventoryitemcategories AS c
// LEFT JOIN inventoryitems AS i
// ON i.category_id = c.id WHERE c.id = '{id}';",
// id = id,
// ))
// .fetch(&pool)
// .map_ok(|row| row.try_into())
// .try_collect::<Vec<Result<Item, models::Error>>>()
// .await
// // we have two error handling lines here. these are distinct errors
// // this one is the SQL error that may arise during the query
// .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Html::from(e.to_string())))?
// .into_iter()
// .collect::<Result<Vec<Item>, models::Error>>()
// // and this one is the model mapping error that may arise e.g. during
// // reading of the rows
// .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Html::from(e.to_string())))?;
// Ok((
// StatusCode::OK,
// Html::from(
// InventoryItemList::build(&items)
// .await
// .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, Html::from(e.to_string())))?
// .to_string(),
// ),
// ))
// }