dancer management

main
Zynh0722 2023-11-06 11:14:04 -08:00
parent eabd1f559a
commit cecd4e0104
7 changed files with 129 additions and 15 deletions

View File

@ -9,7 +9,7 @@ use axum::{
}; };
use cm_lib::{ use cm_lib::{
hx_request::HxRequest, hx_request::HxRequest,
models::{NewDrink, Shift}, models::{Dancer, NewDancer, NewDrink, Shift},
render, render,
schema::shifts, schema::shifts,
}; };
@ -24,6 +24,7 @@ use crate::AppState;
pub(crate) fn router() -> axum::Router<AppState> { pub(crate) fn router() -> axum::Router<AppState> {
axum::Router::new() axum::Router::new()
.route("/drinks", post(add_drink)) .route("/drinks", post(add_drink))
.route("/dancers", post(add_dancer))
.route("/shifts/open", post(open_shift)) .route("/shifts/open", post(open_shift))
.route("/shifts/:id/close", post(close_shift)) .route("/shifts/:id/close", post(close_shift))
.route("/ada/updates", get(ada_subscribe)) .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<AppState>,
Form(form): Form<DancerForm>,
) -> 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( async fn ada_subscribe(
State(state): State<AppState>, State(state): State<AppState>,
) -> Sse<impl Stream<Item = Result<Event, BroadcastStreamRecvError>>> { ) -> Sse<impl Stream<Item = Result<Event, BroadcastStreamRecvError>>> {

View File

@ -21,7 +21,7 @@ pub struct Drink {
pub time: chrono::NaiveDateTime, pub time: chrono::NaiveDateTime,
} }
#[derive(Identifiable, Queryable, Selectable, Debug)] #[derive(Identifiable, Queryable, Selectable, Debug, Clone)]
#[diesel(table_name = crate::schema::dancers)] #[diesel(table_name = crate::schema::dancers)]
#[diesel(check_for_backend(diesel::mysql::Mysql))] #[diesel(check_for_backend(diesel::mysql::Mysql))]
pub struct Dancer { pub struct Dancer {

View File

@ -1,3 +1,5 @@
#![feature(async_closure)]
mod api; mod api;
mod management; mod management;
mod sse_handler; mod sse_handler;

View File

@ -1,5 +1,7 @@
use axum::{response::IntoResponse, routing::get}; use axum::{extract::State, response::IntoResponse, routing::get};
use cm_lib::render; use cm_lib::{models::Dancer, render};
use diesel::{QueryDsl, SelectableHelper};
use diesel_async::RunQueryDsl;
use crate::AppState; use crate::AppState;
@ -13,6 +15,16 @@ async fn home() -> impl IntoResponse {
render!(crate::templates::management_html) render!(crate::templates::management_html)
} }
async fn dancers() -> impl IntoResponse { async fn dancers(State(state): State<AppState>) -> impl IntoResponse {
render!(crate::templates::dancers_html) 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)
} }

View File

@ -0,0 +1,8 @@
@use cm_lib::models::Dancer;
@(dancer: Dancer)
<tr>
<td>@dancer.name</td>
<td>@dancer.stage_name</td>
</tr>

View File

@ -1,7 +1,9 @@
@use super::base_html; @use super::base_html;
@use super::components::return_to_main_html; @use super::components::return_to_main_html;
@use super::components::dancer_html;
@use cm_lib::models::Dancer;
@() @(dancers: Vec<Dancer>)
@:base_html({ @:base_html({
@ -9,8 +11,45 @@
@:return_to_main_html() @:return_to_main_html()
</div> </div>
<div> <form hx-post="/api/dancers" hx-target="next #dancer_list" hx-swap="afterbegin">
I have no dancers to show you :( <div class="flex w-3/5">
<div class="relative">
<input type="text" id="name" name="name"
class="block px-2.5 pb-2.5 pt-4 w-full text-sm text-gray-900 bg-transparent rounded-lg border-2 border-gray-300 appearance-none focus:outline-none focus:ring-0 focus:border-blue-600 peer"
placeholder=" " />
<label for="name"
class="absolute text-sm text-gray-500 duration-300 transform -translate-y-4 scale-75 top-2 z-10 origin-[0] bg-white px-2 peer-focus:px-2 peer-focus:text-blue-600 peer-placeholder-shown:scale-100 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:top-1/2 peer-focus:top-2 peer-focus:scale-75 peer-focus:-translate-y-4 left-1">
Name
</label>
</div> </div>
<div class="relative">
<input type="text" id="stage_name" name="stage_name"
class="block px-2.5 pb-2.5 pt-4 w-full text-sm text-gray-900 bg-transparent rounded-lg border-2 border-gray-300 appearance-none focus:outline-none focus:ring-0 focus:border-blue-600 peer"
placeholder=" " />
<label for="stage_name"
class="absolute text-sm text-gray-500 duration-300 transform -translate-y-4 scale-75 top-2 z-10 origin-[0] bg-white px-2 peer-focus:px-2 peer-focus:text-blue-600 peer-placeholder-shown:scale-100 peer-placeholder-shown:-translate-y-1/2 peer-placeholder-shown:top-1/2 peer-focus:top-2 peer-focus:scale-75 peer-focus:-translate-y-4 left-1">
Stage Name
</label>
</div>
<button type="submit"
class="focus:outline-none text-white bg-green-700 hover:bg-green-800 focus:ring-4 focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5">+</button>
</div>
</form>
<table>
<thead>
<tr>
<th>Name</th>
<th>Stage Name</th>
</tr>
</thead>
<tbody id="dancer_list">
@for dancer in dancers.into_iter() {
@:dancer_html(dancer)
}
</tbody>
</table>
}) })

View File

@ -10,7 +10,7 @@
@:return_to_main_html() @:return_to_main_html()
</div> </div>
<form hx-swap="outerHTML" hx-post="/api/drinks" class="h-full flex items-center justify-center gap-28"> <form hx-post="/api/drinks" class="h-full flex items-center justify-center gap-28">
<fieldset class="flex flex-col text-4xl text-center overflow-hidden border border-black rounded-2xl"> <fieldset class="flex flex-col text-4xl text-center overflow-hidden border border-black rounded-2xl">
<input hidden required type="radio" id="two" name="price" value="2" class="peer/two" /> <input hidden required type="radio" id="two" name="price" value="2" class="peer/two" />
<label class="p-4 hover:bg-blue-300 peer-checked/two:bg-blue-400" for="two">$2</label> <label class="p-4 hover:bg-blue-300 peer-checked/two:bg-blue-400" for="two">$2</label>
@ -33,15 +33,19 @@
<div> <div>
<label for="quantity" class="block mb-2 text-sm font-medium text-gray-900">Quantity: </label> <label for="quantity" class="block mb-2 text-sm font-medium text-gray-900">Quantity: </label>
<div class="flex flex-col"> <div class="flex flex-col">
<button type="button" onclick="this.nextElementSibling.stepUp()" class="py-4 w-16 text-2xl font-medium text-gray-900 focus:outline-none bg-white rounded-t-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-200">+1</button> <button type="button" onclick="this.nextElementSibling.stepUp()"
<input type="number" id="quantity" name="quantity" step="1" value="1" class="bg-gray-50 border border-gray-300 text-gray-900 w-16 text-center text-3xl focus:ring-blue-500 focus:border-blue-500 block p-2.5"/> class="py-4 w-16 text-2xl font-medium text-gray-900 focus:outline-none bg-white rounded-t-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-200">+1</button>
<button type="button" onclick="this.previousElementSibling.stepDown()" class="py-4 w-16 text-2xl font-medium text-gray-900 focus:outline-none bg-white rounded-b-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-200">-1</button> <input type="number" id="quantity" name="quantity" step="1" value="1"
class="bg-gray-50 border border-gray-300 text-gray-900 w-16 text-center text-3xl focus:ring-blue-500 focus:border-blue-500 block p-2.5" />
<button type="button" onclick="this.previousElementSibling.stepDown()"
class="py-4 w-16 text-2xl font-medium text-gray-900 focus:outline-none bg-white rounded-b-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-200">-1</button>
</div> </div>
</div> </div>
<input type="hidden" id="shift_id" name="shift_id" value="@shift_id" /> <input type="hidden" id="shift_id" name="shift_id" value="@shift_id" />
</div> </div>
<button type="submit" class="focus:outline-none text-white bg-green-700 hover:bg-green-800 focus:ring-4 focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5 dark:bg-green-600 dark:hover:bg-green-700 dark:focus:ring-green-800"> <button type="submit"
class="focus:outline-none text-white bg-green-700 hover:bg-green-800 focus:ring-4 focus:ring-green-300 font-medium rounded-lg text-sm px-5 py-2.5 dark:bg-green-600 dark:hover:bg-green-700 dark:focus:ring-green-800">
Submit Submit
</button> </button>
</div> </div>