| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- use actix_web::{get, web, HttpResponse};
- use serde::Serialize;
- use serde_json::json;
- use sqlx::PgPool;
- use uuid::Uuid;
- use crate::auth::AuthUser;
- use crate::models::exercise::{Exercise, ExerciseType};
- use crate::models::workout::Workout;
- #[derive(Serialize)]
- struct ExerciseResponse {
- id: Uuid,
- name: String,
- notes: Option<String>,
- exercise_type: ExerciseType,
- }
- impl From<Exercise> for ExerciseResponse {
- fn from(e: Exercise) -> Self {
- Self {
- id: e.id,
- name: e.name,
- notes: e.notes,
- exercise_type: e.exercise_type,
- }
- }
- }
- #[get("/workout/{id}")]
- pub async fn get_workout(
- pool: web::Data<PgPool>,
- _auth: AuthUser,
- path: web::Path<Uuid>,
- ) -> HttpResponse {
- let id = path.into_inner();
- let workout = match Workout::find_by_id(pool.get_ref(), id).await {
- Ok(Some(w)) => w,
- Ok(None) => return HttpResponse::NotFound().json(json!({"error": "Workout not found"})),
- Err(_) => {
- return HttpResponse::InternalServerError().json(json!({"error": "An error occurred"}))
- }
- };
- let exercises = match Exercise::find_by_workout(pool.get_ref(), id).await {
- Ok(e) => e,
- Err(_) => {
- return HttpResponse::InternalServerError().json(json!({"error": "An error occurred"}))
- }
- };
- let exercises: Vec<ExerciseResponse> = exercises.into_iter().map(Into::into).collect();
- HttpResponse::Ok().json(json!({
- "id": workout.id,
- "name": workout.name,
- "exercises": exercises,
- }))
- }
|