Skip to content

Commit

Permalink
order workouts by last execution
Browse files Browse the repository at this point in the history
  • Loading branch information
avalax committed Jan 22, 2018
1 parent 6b964f2 commit 08ce58d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,29 @@ public void aFinishedWorkout_shouldBeDeleted() throws Exception {
}

@Test
public void threeFinishedWorkouts_shouldBeDisplayedByCreationDESC() throws Exception {
public void threeWorkouts_shouldBeOrderedByLastExecutionDateDESC() throws Exception {
BasicSetBuilder set = aSet().withWeight(42).withMaxReps(12);
BasicExerciseBuilder exercise = anExercise().withName("an exercise").withSet(set);
BasicWorkoutBuilder workout = aWorkout().withName("first workout").withExercise(exercise);
Workout firstWorkout = persistence.addWorkout(workout);
Workout secondWorkout = persistence.addWorkout(workout.withName("second workout"));
Workout thirdWorkout = persistence.addWorkout(workout.withName("third workout"));
FinishedWorkoutId firstFinishedWorkoutId = persistence.finishWorkout(firstWorkout);
FinishedWorkoutId secondFinishedWorkoutId = persistence.finishWorkout(secondWorkout);
FinishedWorkoutId thirdFinishedWorkoutId = persistence.finishWorkout(thirdWorkout);
persistence.updateFinishedWorkoutCreation(firstFinishedWorkoutId, "2017-12-29");
persistence.updateFinishedWorkoutCreation(secondFinishedWorkoutId, "2017-12-30");
persistence.updateFinishedWorkoutCreation(thirdFinishedWorkoutId, "2017-12-31");

activityRule.launchActivity(null);

application.hasShownWorkoutDetails(0, "third workout", "Dec 31, 2017", "Executed 1 time");
application.hasShownWorkoutDetails(1, "second workout", "Dec 30, 2017", "Executed 1 time");
application.hasShownWorkoutDetails(2, "first workout", "Dec 29, 2017", "Executed 1 time");
}

@Test
public void threeFinishedWorkouts_shouldBeOrderedByCreationDESC() throws Exception {
BasicSetBuilder set = aSet().withWeight(42).withMaxReps(12);
BasicExerciseBuilder exercise = anExercise().withName("an exercise").withSet(set);
BasicWorkoutBuilder workout = aWorkout().withName("a workout").withExercise(exercise);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import de.avalax.fitbuddy.domain.model.workout.WorkoutId;

public class SQLiteFinishedWorkoutRepository implements FinishedWorkoutRepository {
private static final String TABLE_FINISHED_WORKOUT = "finished_workout";
static final String TABLE_FINISHED_WORKOUT = "finished_workout";
private SQLiteOpenHelper sqLiteOpenHelper;
private FinishedExerciseRepository finishedExerciseRepository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import de.avalax.fitbuddy.domain.model.workout.WorkoutId;
import de.avalax.fitbuddy.domain.model.workout.WorkoutRepository;

import static de.avalax.fitbuddy.port.adapter.persistence.SQLiteFinishedWorkoutRepository.TABLE_FINISHED_WORKOUT;

public class SQLiteWorkoutRepository implements WorkoutRepository {
private static final String TABLE_WORKOUT = "workout";
private SQLiteOpenHelper sqLiteOpenHelper;
Expand Down Expand Up @@ -100,18 +102,17 @@ private Workout createWorkout(Cursor cursor) {
@Override
public List<Workout> loadAll() {
List<Workout> workoutList = new ArrayList<>();
SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase();
Cursor cursor = database.query(TABLE_WORKOUT, new String[]
{"id", "name"},
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
Workout workout = createWorkout(cursor);
workoutList.add(workout);
} while (cursor.moveToNext());
String sql = "SELECT id, name, (SELECT created FROM " + TABLE_FINISHED_WORKOUT
+ " WHERE workout_id=" + TABLE_WORKOUT + ".id ORDER BY created DESC LIMIT 1) created FROM "
+ TABLE_WORKOUT + " ORDER BY created DESC";
try (SQLiteDatabase database = sqLiteOpenHelper.getReadableDatabase()) {
try (Cursor cursor = database.rawQuery(sql, null)) {
while (cursor.moveToNext()) {
Workout workout = createWorkout(cursor);
workoutList.add(workout);
}
}
}
cursor.close();
database.close();
return workoutList;
}

Expand Down

0 comments on commit 08ce58d

Please sign in to comment.