Skip to content

Commit

Permalink
feat(backend): change name key of workouts to id
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnisDa committed Dec 8, 2024
1 parent 8d006ec commit c73804f
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 35 deletions.
35 changes: 35 additions & 0 deletions crates/migrations/src/m20241126_changes_for_issue_1113.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ UPDATE exercise SET identifier = id;
ALTER TABLE exercise RENAME COLUMN identifier TO name;
DROP INDEX "exercise__identifier__index";
CREATE INDEX "{}" ON "exercise" ("name");
UPDATE workout
SET information =
JSONB_SET(
information,
'{{exercises}}',
(
SELECT JSONB_AGG(
JSONB_SET(
exercise,
'{{id}}',
exercise->'name',
true
) - 'name'
)
FROM JSONB_ARRAY_ELEMENTS(information->'exercises') AS exercise
)
);
UPDATE workout
SET summary =
JSONB_SET(
summary,
'{{exercises}}',
(
SELECT JSONB_AGG(
JSONB_SET(
exercise,
'{{id}}',
exercise->'name',
true
) - 'name'
)
FROM JSONB_ARRAY_ELEMENTS(summary->'exercises') AS exercise
)
)
"#,
EXERCISE_NAME_INDEX
))
Expand Down
4 changes: 2 additions & 2 deletions crates/models/fitness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub struct EntityAssets {
)]
#[serde(rename_all = "snake_case")]
pub struct ProcessedExercise {
pub name: String,
pub id: String,
pub lot: ExerciseLot,
pub notes: Vec<String>,
pub sets: Vec<WorkoutSetRecord>,
Expand Down Expand Up @@ -430,7 +430,7 @@ pub struct WorkoutInformation {
)]
#[serde(rename_all = "snake_case")]
pub struct WorkoutSummaryExercise {
pub name: String,
pub id: String,
pub num_sets: usize,
pub lot: Option<ExerciseLot>,
pub best_set: Option<WorkoutSetRecord>,
Expand Down
10 changes: 5 additions & 5 deletions crates/services/fitness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl FitnessService {
for exercise in input.exercises {
let db_ex = self.exercise_details(exercise.exercise_id.clone()).await?;
summary.exercises.push(WorkoutSummaryExercise {
name: exercise.exercise_id.clone(),
id: exercise.exercise_id.clone(),
best_set: None,
lot: None,
num_sets: exercise.sets.len(),
Expand All @@ -133,7 +133,7 @@ impl FitnessService {
})
.collect(),
notes: exercise.notes,
name: exercise.exercise_id,
id: exercise.exercise_id,
});
}
let processed_exercises = information.exercises.clone();
Expand Down Expand Up @@ -574,7 +574,7 @@ impl FitnessService {
for (idx, ex) in wkt.information.exercises.iter().enumerate() {
let Some(association) = UserToEntity::find()
.filter(user_to_entity::Column::UserId.eq(&user_id))
.filter(user_to_entity::Column::ExerciseId.eq(ex.name.clone()))
.filter(user_to_entity::Column::ExerciseId.eq(ex.id.clone()))
.one(&self.0.db)
.await?
else {
Expand Down Expand Up @@ -634,8 +634,8 @@ impl FitnessService {
.unwrap();
let mut summary = db_workout.summary.clone();
let mut information = db_workout.information.clone();
summary.exercises[workout.idx].name = new_name.clone();
information.exercises[workout.idx].name = new_name.clone();
summary.exercises[workout.idx].id = new_name.clone();
information.exercises[workout.idx].id = new_name.clone();
let mut db_workout: workout::ActiveModel = db_workout.into();
db_workout.summary = ActiveValue::Set(summary);
db_workout.information = ActiveValue::Set(information);
Expand Down
2 changes: 1 addition & 1 deletion crates/utils/database/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ pub async fn calculate_user_activities_and_summary(
activity.workout_distance += workout_total.distance.to_i32().unwrap_or_default();
activity.workout_rest_time += workout_total.rest_time as i32;
for exercise in workout.information.exercises {
let db_ex = exercises.iter().find(|e| e.id == exercise.name).unwrap();
let db_ex = exercises.iter().find(|e| e.id == exercise.id).unwrap();
if user_exercises
.iter()
.find(|e| e.exercise_id == Some(db_ex.id.clone()))
Expand Down
10 changes: 5 additions & 5 deletions crates/utils/dependent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ pub async fn get_focused_workout_summary(
ss: &Arc<SupportingService>,
) -> WorkoutFocusedSummary {
let db_exercises = Exercise::find()
.filter(exercise::Column::Id.is_in(exercises.iter().map(|e| e.name.clone())))
.filter(exercise::Column::Id.is_in(exercises.iter().map(|e| e.id.clone())))
.all(&ss.db)
.await
.unwrap();
Expand All @@ -1678,7 +1678,7 @@ pub async fn get_focused_workout_summary(
let mut muscles = HashMap::new();
let mut equipments = HashMap::new();
for (idx, ex) in exercises.iter().enumerate() {
let exercise = db_exercises.iter().find(|e| e.id == ex.name).unwrap();
let exercise = db_exercises.iter().find(|e| e.id == ex.id).unwrap();
lots.entry(exercise.lot).or_insert(vec![]).push(idx);
levels.entry(exercise.level).or_insert(vec![]).push(idx);
if let Some(force) = exercise.force {
Expand Down Expand Up @@ -1947,7 +1947,7 @@ pub async fn create_or_update_workout(
ProcessedExercise {
sets,
lot: db_ex.lot,
name: db_ex.id,
id: db_ex.id,
total: Some(totals),
notes: ex.notes.clone(),
assets: ex.assets.clone(),
Expand Down Expand Up @@ -1983,7 +1983,7 @@ pub async fn create_or_update_workout(
.map(|(best_set, lot, e)| WorkoutSummaryExercise {
best_set,
lot: Some(lot),
name: e.name.clone(),
id: e.id.clone(),
num_sets: e.sets.len(),
})
.collect(),
Expand Down Expand Up @@ -2387,7 +2387,7 @@ pub fn db_workout_to_workout_input(user_workout: workout::Model) -> UserWorkoutI
.exercises
.into_iter()
.map(|e| UserExerciseInput {
exercise_id: e.name,
exercise_id: e.id,
sets: e
.sets
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions docs/includes/export-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ export interface WorkoutOrExerciseTotals {
/** An exercise that has been processed and committed to the database. */
export interface ProcessedExercise {
assets: EntityAssets | null;
id: string;
/**
* @default 'reps_and_weight'
* @type {'duration' | 'distance_and_duration' | 'reps' | 'reps_and_weight'}
*/
lot: ExerciseLot;
name: string;
notes: string[];
sets: WorkoutSetRecord[];
total: WorkoutOrExerciseTotals | null;
Expand All @@ -304,9 +304,9 @@ export interface WorkoutInformation {
/** The summary about an exercise done in a workout. */
export interface WorkoutSummaryExercise {
best_set: WorkoutSetRecord | null;
id: string;
/** @default 'reps_and_weight' */
lot: ExerciseLot | null;
name: string;
num_sets: number;
}

Expand Down
Loading

0 comments on commit c73804f

Please sign in to comment.