Added basic exercise addition and deletion

This commit is contained in:
2025-07-09 14:02:18 +02:00
parent 24c784e434
commit 0d1101f84d
27 changed files with 734 additions and 165 deletions

50
src/pages/exercises/id.rs Normal file
View File

@@ -0,0 +1,50 @@
use crate::{AppError, layouts, util::AppState};
use axum::{
Router,
extract::{Path, State},
http::HeaderMap,
response::IntoResponse,
routing::get,
};
use maud::{Markup, html};
use uuid::Uuid;
pub fn routes() -> Router<AppState> {
Router::new().route("/", get(page).delete(delete))
}
async fn page(State(state): State<AppState>, Path(id): Path<Uuid>) -> Result<Markup, AppError> {
let exercise = sqlx::query_as!(
crate::models::Exercise,
"SELECT exercise_id, name, description FROM exercises WHERE exercise_id = $1",
id
)
.fetch_one(&state.pool)
.await?;
let content = html! {
h1 { (exercise.name) }
p { (exercise.description) }
button hx-delete={ "/exercises/" (exercise.exercise_id) } hx-confirm="Are you sure that you want to delete this exercise?" class="btn btn-error" { "Delete Exercise" }
};
Ok(layouts::desktop(content, "Exercises"))
}
async fn delete(
State(state): State<AppState>,
Path(id): Path<Uuid>,
) -> Result<impl IntoResponse, AppError> {
sqlx::query_as!(
crate::models::Exercise,
"DELETE FROM exercises WHERE exercise_id = $1",
id
)
.execute(&state.pool)
.await?;
let mut headers = HeaderMap::new();
headers.insert("hx-location", "/exercises".parse().unwrap());
Ok((headers, html! {}))
}