From 50a6ece0d87a4f3c0b645a4cd4bae674f33571e1 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 24 Jan 2023 22:43:40 +0100 Subject: [PATCH] Added ExerciseController --- gymboard-api/sample_data/countries.csv | 5 +++- .../controller/ExerciseController.java | 22 +++++++++++++++++ .../gymboard_api/service/ExerciseService.java | 24 +++++++++++++++++++ .../src/pages/gym/GymSubmissionPage.vue | 12 +++++++--- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/controller/ExerciseController.java create mode 100644 gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/service/ExerciseService.java diff --git a/gymboard-api/sample_data/countries.csv b/gymboard-api/sample_data/countries.csv index 0329caa..e13d59c 100644 --- a/gymboard-api/sample_data/countries.csv +++ b/gymboard-api/sample_data/countries.csv @@ -1,3 +1,6 @@ us,United States nl,Netherlands -de,Germany \ No newline at end of file +de,Germany +uk,United Kingdom +dk,Denmark +mx,Mexico \ No newline at end of file diff --git a/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/controller/ExerciseController.java b/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/controller/ExerciseController.java new file mode 100644 index 0000000..70ce9da --- /dev/null +++ b/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/controller/ExerciseController.java @@ -0,0 +1,22 @@ +package nl.andrewlalis.gymboard_api.controller; + +import nl.andrewlalis.gymboard_api.controller.dto.ExerciseResponse; +import nl.andrewlalis.gymboard_api.service.ExerciseService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +public class ExerciseController { + private final ExerciseService exerciseService; + + public ExerciseController(ExerciseService exerciseService) { + this.exerciseService = exerciseService; + } + + @GetMapping(path = "/exercises") + public List getExercises() { + return exerciseService.getExercises(); + } +} diff --git a/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/service/ExerciseService.java b/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/service/ExerciseService.java new file mode 100644 index 0000000..4c201cb --- /dev/null +++ b/gymboard-api/src/main/java/nl/andrewlalis/gymboard_api/service/ExerciseService.java @@ -0,0 +1,24 @@ +package nl.andrewlalis.gymboard_api.service; + +import nl.andrewlalis.gymboard_api.controller.dto.ExerciseResponse; +import nl.andrewlalis.gymboard_api.dao.exercise.ExerciseRepository; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +public class ExerciseService { + private final ExerciseRepository exerciseRepository; + + public ExerciseService(ExerciseRepository exerciseRepository) { + this.exerciseRepository = exerciseRepository; + } + + @Transactional(readOnly = true) + public List getExercises() { + return exerciseRepository.findAll(Sort.by("shortName")) + .stream().map(ExerciseResponse::new).toList(); + } +} diff --git a/gymboard-app/src/pages/gym/GymSubmissionPage.vue b/gymboard-app/src/pages/gym/GymSubmissionPage.vue index 1751a8a..4d9f87b 100644 --- a/gymboard-app/src/pages/gym/GymSubmissionPage.vue +++ b/gymboard-app/src/pages/gym/GymSubmissionPage.vue @@ -4,7 +4,7 @@
import {onMounted, ref, Ref} from 'vue'; -import {Gym} from 'src/api/gymboard-api'; +import {Exercise, getExercises, Gym} from 'src/api/gymboard-api'; import {getGymFromRoute} from 'src/router/gym-routing'; import SlimForm from 'components/SlimForm.vue'; @@ -64,6 +64,7 @@ import SlimForm from 'components/SlimForm.vue'; // const props = defineProps(); const gym: Ref = ref(); +const exercises: Ref | undefined> = ref>(); let submissionModel = ref({ exercise: null, weight: null, @@ -72,7 +73,7 @@ let submissionModel = ref({ date: new Date().toLocaleDateString('en-CA') }); const weightUnits = ['Kg', 'Lbs']; -const exercises = ['Bench Press', 'Squat', 'Deadlift']; +const exercisesF = ['Bench Press', 'Squat', 'Deadlift']; // TODO: Make it possible to pass the gym to this via props instead. onMounted(async () => { @@ -81,6 +82,11 @@ onMounted(async () => { } catch (error) { console.error(error); } + try { + exercises.value = await getExercises(); + } catch (error) { + console.error(error); + } }); function onSubmitted() {