finalized exercise creation endpoint
This commit is contained in:
2
gen.ts
2
gen.ts
@@ -47,6 +47,7 @@ const data: MuscleGroup[] = [
|
|||||||
muscles: [
|
muscles: [
|
||||||
{ colloquial_name: "Biceps", scientific_name: "Biceps brachii" },
|
{ colloquial_name: "Biceps", scientific_name: "Biceps brachii" },
|
||||||
{ colloquial_name: "Brachialis", scientific_name: "Brachialis" },
|
{ colloquial_name: "Brachialis", scientific_name: "Brachialis" },
|
||||||
|
// TODO: add all 3 tricep heads, since you can easily bias them
|
||||||
{ colloquial_name: "Triceps", scientific_name: "Triceps brachii" },
|
{ colloquial_name: "Triceps", scientific_name: "Triceps brachii" },
|
||||||
{ colloquial_name: "Brachioradialis", scientific_name: "Brachioradialis" },
|
{ colloquial_name: "Brachioradialis", scientific_name: "Brachioradialis" },
|
||||||
{ colloquial_name: "Forearm Flexors/Extensors", scientific_name: "Flexor and extensor muscles of the forearm" }
|
{ colloquial_name: "Forearm Flexors/Extensors", scientific_name: "Flexor and extensor muscles of the forearm" }
|
||||||
@@ -56,6 +57,7 @@ const data: MuscleGroup[] = [
|
|||||||
major_group: "Core",
|
major_group: "Core",
|
||||||
group_name: "Abdominals",
|
group_name: "Abdominals",
|
||||||
muscles: [
|
muscles: [
|
||||||
|
// TODO: add upper and lower core muscles
|
||||||
{ colloquial_name: "Abs", scientific_name: "Rectus abdominis" },
|
{ colloquial_name: "Abs", scientific_name: "Rectus abdominis" },
|
||||||
{ colloquial_name: "Transverse Abs", scientific_name: "Transversus abdominis" },
|
{ colloquial_name: "Transverse Abs", scientific_name: "Transversus abdominis" },
|
||||||
{ colloquial_name: "External Obliques", scientific_name: "Obliquus externus abdominis" },
|
{ colloquial_name: "External Obliques", scientific_name: "Obliquus externus abdominis" },
|
||||||
|
|||||||
@@ -1,16 +1,11 @@
|
|||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use crate::*;
|
use crate::*;
|
||||||
use anyhow::bail;
|
|
||||||
use axum::{
|
use axum::{
|
||||||
async_trait,
|
async_trait,
|
||||||
extract::{FromRef, FromRequestParts},
|
extract::{FromRef, FromRequestParts},
|
||||||
http::{header, request::Parts},
|
http::{header, request::Parts},
|
||||||
};
|
};
|
||||||
|
|
||||||
use chrono::{DateTime, Utc};
|
|
||||||
use jwt::VerifyWithKey;
|
use jwt::VerifyWithKey;
|
||||||
use sqlx::types::Uuid;
|
|
||||||
use util::auth::JWTClaims;
|
use util::auth::JWTClaims;
|
||||||
|
|
||||||
pub struct JWT(JWTClaims);
|
pub struct JWT(JWTClaims);
|
||||||
|
|||||||
@@ -28,6 +28,12 @@ pub struct Exercise {
|
|||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
pub struct Muscle {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub is_primary: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[utoipa::path(post, path = "/create", responses((status = OK, body = String)), tag = super::EXERCISES_TAG)]
|
#[utoipa::path(post, path = "/create", responses((status = OK, body = String)), tag = super::EXERCISES_TAG)]
|
||||||
pub async fn create(
|
pub async fn create(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
@@ -39,10 +45,37 @@ pub async fn create(
|
|||||||
.await?
|
.await?
|
||||||
.is_admin;
|
.is_admin;
|
||||||
|
|
||||||
query!(
|
let muscles: Vec<Muscle> = body
|
||||||
|
.primary_muscles
|
||||||
|
.iter()
|
||||||
|
.map(|id| -> anyhow::Result<Muscle> {
|
||||||
|
let uuid = Uuid::parse_str(id)?;
|
||||||
|
Ok(Muscle {
|
||||||
|
id: uuid,
|
||||||
|
is_primary: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.chain(
|
||||||
|
body.secondary_muscles
|
||||||
|
.iter()
|
||||||
|
.map(|id| -> anyhow::Result<Muscle> {
|
||||||
|
let uuid = Uuid::parse_str(id)?;
|
||||||
|
Ok(Muscle {
|
||||||
|
id: uuid,
|
||||||
|
is_primary: false,
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.collect::<Result<Vec<Muscle>, _>>()
|
||||||
|
.context("Failed to parse muscle ids")?;
|
||||||
|
|
||||||
|
let mut tx = state.db.begin().await?;
|
||||||
|
|
||||||
|
let exercise = query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO exercises (name, exercise_type, official, author_id, description)
|
INSERT INTO exercises (name, exercise_type, official, author_id, description)
|
||||||
VALUES ($1, $2, $3, $4, $5)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
RETURNING id
|
||||||
"#,
|
"#,
|
||||||
body.name,
|
body.name,
|
||||||
body.exercise_type as ExerciseType,
|
body.exercise_type as ExerciseType,
|
||||||
@@ -50,8 +83,24 @@ pub async fn create(
|
|||||||
user,
|
user,
|
||||||
body.description
|
body.description
|
||||||
)
|
)
|
||||||
.fetch_one(&*state.db)
|
.fetch_one(&mut *tx)
|
||||||
|
// .fetch_one(&*state.db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
todo!()
|
query!(
|
||||||
|
r#"
|
||||||
|
INSERT INTO exercise_muscle_relations (exercise_id, muscle_id, is_primary)
|
||||||
|
SELECT * FROM UNNEST($1::uuid[], $2::uuid[], $3::boolean[])
|
||||||
|
"#,
|
||||||
|
&vec![exercise.id; muscles.len()],
|
||||||
|
&muscles.iter().map(|m| m.id).collect::<Vec<Uuid>>(),
|
||||||
|
&muscles.iter().map(|m| m.is_primary).collect::<Vec<bool>>()
|
||||||
|
)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
// .execute(&*state.db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
tx.commit().await?;
|
||||||
|
|
||||||
|
Ok(exercise.id.to_string())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user