From cecd4e01041eb5d1d6a62b0eb7bc897bfe074123 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Mon, 6 Nov 2023 11:14:04 -0800 Subject: [PATCH] dancer management --- src/api.rs | 51 ++++++++++++++++++++++++++++- src/lib/models.rs | 2 +- src/main.rs | 2 ++ src/management.rs | 20 ++++++++--- templates/components/dancer.rs.html | 8 +++++ templates/dancers.rs.html | 47 +++++++++++++++++++++++--- templates/drinks.rs.html | 14 +++++--- 7 files changed, 129 insertions(+), 15 deletions(-) create mode 100644 templates/components/dancer.rs.html diff --git a/src/api.rs b/src/api.rs index 71b245b..a186e8d 100644 --- a/src/api.rs +++ b/src/api.rs @@ -9,7 +9,7 @@ use axum::{ }; use cm_lib::{ hx_request::HxRequest, - models::{NewDrink, Shift}, + models::{Dancer, NewDancer, NewDrink, Shift}, render, schema::shifts, }; @@ -24,6 +24,7 @@ use crate::AppState; pub(crate) fn router() -> axum::Router { axum::Router::new() .route("/drinks", post(add_drink)) + .route("/dancers", post(add_dancer)) .route("/shifts/open", post(open_shift)) .route("/shifts/:id/close", post(close_shift)) .route("/ada/updates", get(ada_subscribe)) @@ -128,6 +129,54 @@ async fn add_drink( ) } +#[derive(Deserialize, Debug)] +struct DancerForm { + stage_name: String, + name: String, +} + +impl<'a> DancerForm { + fn as_new_dancer(&'a self) -> NewDancer<'a> { + NewDancer { + stage_name: &self.stage_name, + name: &self.name, + } + } +} + +async fn add_dancer( + State(state): State, + Form(form): Form, +) -> impl IntoResponse { + let mut conn = state.connection.get().await.unwrap(); + + let inserted: Dancer = conn + .transaction(|mut conn| { + use cm_lib::schema::dancers::dsl::*; + + async move { + diesel::insert_into(dancers) + .values(form.as_new_dancer()) + .execute(&mut conn) + .await + .unwrap(); + + dancers + .order(id.desc()) + .select(Dancer::as_select()) + .first(&mut conn) + .await + } + .scope_boxed() + }) + .await + .optional() + .unwrap() + .unwrap(); + + render!(crate::templates::components::dancer_html, inserted) +} + async fn ada_subscribe( State(state): State, ) -> Sse>> { diff --git a/src/lib/models.rs b/src/lib/models.rs index 1d148dd..1e41f63 100644 --- a/src/lib/models.rs +++ b/src/lib/models.rs @@ -21,7 +21,7 @@ pub struct Drink { pub time: chrono::NaiveDateTime, } -#[derive(Identifiable, Queryable, Selectable, Debug)] +#[derive(Identifiable, Queryable, Selectable, Debug, Clone)] #[diesel(table_name = crate::schema::dancers)] #[diesel(check_for_backend(diesel::mysql::Mysql))] pub struct Dancer { diff --git a/src/main.rs b/src/main.rs index ef60524..ff8799d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![feature(async_closure)] + mod api; mod management; mod sse_handler; diff --git a/src/management.rs b/src/management.rs index 3d6d363..11292c0 100644 --- a/src/management.rs +++ b/src/management.rs @@ -1,5 +1,7 @@ -use axum::{response::IntoResponse, routing::get}; -use cm_lib::render; +use axum::{extract::State, response::IntoResponse, routing::get}; +use cm_lib::{models::Dancer, render}; +use diesel::{QueryDsl, SelectableHelper}; +use diesel_async::RunQueryDsl; use crate::AppState; @@ -13,6 +15,16 @@ async fn home() -> impl IntoResponse { render!(crate::templates::management_html) } -async fn dancers() -> impl IntoResponse { - render!(crate::templates::dancers_html) +async fn dancers(State(state): State) -> impl IntoResponse { + use cm_lib::schema::dancers::dsl::*; + + let mut conn = state.connection.get().await.unwrap(); + + let dancers_vec = dancers + .select(Dancer::as_select()) + .load(&mut conn) + .await + .unwrap(); + + render!(crate::templates::dancers_html, dancers_vec) } diff --git a/templates/components/dancer.rs.html b/templates/components/dancer.rs.html new file mode 100644 index 0000000..619f967 --- /dev/null +++ b/templates/components/dancer.rs.html @@ -0,0 +1,8 @@ +@use cm_lib::models::Dancer; + +@(dancer: Dancer) + + + @dancer.name + @dancer.stage_name + diff --git a/templates/dancers.rs.html b/templates/dancers.rs.html index 3f72189..16eeb90 100644 --- a/templates/dancers.rs.html +++ b/templates/dancers.rs.html @@ -1,7 +1,9 @@ @use super::base_html; @use super::components::return_to_main_html; +@use super::components::dancer_html; +@use cm_lib::models::Dancer; -@() +@(dancers: Vec) @:base_html({ @@ -9,8 +11,45 @@ @:return_to_main_html() -
- I have no dancers to show you :( -
+
+
+
+ + +
+
+ + +
+ +
+
+ + + + + + + + + + @for dancer in dancers.into_iter() { + + @:dancer_html(dancer) + + } + +
NameStage Name
}) diff --git a/templates/drinks.rs.html b/templates/drinks.rs.html index b6cd2a0..0bd3421 100644 --- a/templates/drinks.rs.html +++ b/templates/drinks.rs.html @@ -10,7 +10,7 @@ @:return_to_main_html() -
+
@@ -33,15 +33,19 @@
- - - + + +
-