From e69be7b2e45184371bea9a3821f8318b7543649b Mon Sep 17 00:00:00 2001 From: oguzhekim Date: Sun, 24 Nov 2024 22:31:02 +0300 Subject: [PATCH] Add modified training programs in the response for tracking endpoints --- .../TrainingProgramController.java | 30 +++++++++---------- .../demo/services/TrainingProgramService.java | 27 ++++++++--------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/backend/demo-group7/src/main/java/com/group7/demo/controllers/TrainingProgramController.java b/backend/demo-group7/src/main/java/com/group7/demo/controllers/TrainingProgramController.java index 94e5320..314c33f 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/controllers/TrainingProgramController.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/controllers/TrainingProgramController.java @@ -60,15 +60,15 @@ public ResponseEntity deleteTrainingProgram(@PathVariable Long id, HttpSer @PostMapping("/{programId}/join") - public ResponseEntity joinTrainingProgram(@PathVariable Long programId , HttpServletRequest request) { - trainingProgramService.joinTrainingProgram(programId ,request); - return ResponseEntity.ok("User has successfully joined the training program."); + public ResponseEntity joinTrainingProgram(@PathVariable Long programId , HttpServletRequest request) { + UserTrainingProgramResponse response = trainingProgramService.joinTrainingProgram(programId ,request); + return ResponseEntity.ok(response); } @DeleteMapping("/{programId}/leave") - public ResponseEntity leaveProgram(@PathVariable Long programId, HttpServletRequest request) { - trainingProgramService.leaveTrainingProgram(programId, request); - return ResponseEntity.ok("Successfully left the training program."); + public ResponseEntity leaveProgram(@PathVariable Long programId, HttpServletRequest request) { + UserTrainingProgramResponse response = trainingProgramService.leaveTrainingProgram(programId, request); + return ResponseEntity.ok(response); } @GetMapping("/{programId}/participants") @@ -85,31 +85,31 @@ public ResponseEntity> getTrainingProgramsByTraine } @PostMapping("/{trainingProgramId}/exercises/{exerciseId}/complete") - public ResponseEntity markExerciseAsCompleted( + public ResponseEntity markExerciseAsCompleted( @PathVariable Long trainingProgramId, @PathVariable Long exerciseId, HttpServletRequest request ) { - trainingProgramService.markExerciseAsCompleted(trainingProgramId, exerciseId, request); - return ResponseEntity.ok().build(); + UserTrainingProgramResponse response = trainingProgramService.markExerciseAsCompleted(trainingProgramId, exerciseId, request); + return ResponseEntity.ok(response); } @PostMapping("/{trainingProgramId}/exercises/{exerciseId}/uncomplete") - public ResponseEntity unmarkExerciseAsCompleted( + public ResponseEntity unmarkExerciseAsCompleted( @PathVariable Long trainingProgramId, @PathVariable Long exerciseId, HttpServletRequest request) { - trainingProgramService.unmarkExerciseAsCompleted(trainingProgramId, exerciseId, request); - return ResponseEntity.ok().build(); + UserTrainingProgramResponse response = trainingProgramService.unmarkExerciseAsCompleted(trainingProgramId, exerciseId, request); + return ResponseEntity.ok(response); } @PostMapping("/{trainingProgramId}/complete") - public ResponseEntity markTrainingProgramAsCompleted( + public ResponseEntity markTrainingProgramAsCompleted( @PathVariable Long trainingProgramId, HttpServletRequest request ) { - trainingProgramService.markTrainingProgramAsCompleted(trainingProgramId, request); - return ResponseEntity.ok().build(); + UserTrainingProgramResponse response = trainingProgramService.markTrainingProgramAsCompleted(trainingProgramId, request); + return ResponseEntity.ok(response); } @GetMapping("/joined/{username}") diff --git a/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java b/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java index 8091645..8fdb67d 100644 --- a/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java +++ b/backend/demo-group7/src/main/java/com/group7/demo/services/TrainingProgramService.java @@ -127,7 +127,7 @@ public void deleteTrainingProgram(Long id, HttpServletRequest request) throws Ex } @Transactional - public void joinTrainingProgram(Long trainingProgramId , HttpServletRequest request) { + public UserTrainingProgramResponse joinTrainingProgram(Long trainingProgramId , HttpServletRequest request) { User user = authenticationService.getAuthenticatedUserInternal(request); TrainingProgram trainingProgram = trainingProgramRepository.findById(trainingProgramId) @@ -144,7 +144,7 @@ public void joinTrainingProgram(Long trainingProgramId , HttpServletRequest requ } // Initialize the progress JSON object - ObjectMapper mapper = new ObjectMapper(); + ObjectMapper objectMapper = new ObjectMapper(); Map exerciseProgress = trainingProgram.getExercises().stream() .collect(Collectors.toMap( TrainingProgramExercise::getId, // Exercise ID @@ -153,7 +153,7 @@ public void joinTrainingProgram(Long trainingProgramId , HttpServletRequest requ String progressJson; try { - progressJson = mapper.writeValueAsString(exerciseProgress); + progressJson = objectMapper.writeValueAsString(exerciseProgress); } catch (JsonProcessingException e) { throw new IllegalStateException("Failed to initialize exercise progress JSON", e); } @@ -168,7 +168,7 @@ public void joinTrainingProgram(Long trainingProgramId , HttpServletRequest requ .build(); // Save the UserTrainingProgram entity - userTrainingProgramRepository.save(userTrainingProgram); + return mapper.mapToUserTrainingProgramResponse(userTrainingProgramRepository.save(userTrainingProgram)); } @Transactional @@ -208,7 +208,7 @@ private UserTrainingProgram getOngoingUserTrainingProgram(User user, Long traini } @Transactional - public void markExerciseAsCompleted(Long trainingProgramId, Long exerciseId, HttpServletRequest request) { + public UserTrainingProgramResponse markExerciseAsCompleted(Long trainingProgramId, Long exerciseId, HttpServletRequest request) { User user = authenticationService.getAuthenticatedUserInternal(request); UserTrainingProgram userTrainingProgram = getOngoingUserTrainingProgram(user, trainingProgramId); @@ -228,13 +228,13 @@ public void markExerciseAsCompleted(Long trainingProgramId, Long exerciseId, Htt throw new IllegalStateException("Failed to update exercise progress JSON", e); } - userTrainingProgramRepository.save(userTrainingProgram); + return mapper.mapToUserTrainingProgramResponse(userTrainingProgramRepository.save(userTrainingProgram)); } @Transactional - public void unmarkExerciseAsCompleted(Long trainingProgramId, Long exerciseId, HttpServletRequest request) { + public UserTrainingProgramResponse unmarkExerciseAsCompleted(Long trainingProgramId, Long exerciseId, HttpServletRequest request) { User user = authenticationService.getAuthenticatedUserInternal(request); UserTrainingProgram userTrainingProgram = getOngoingUserTrainingProgram(user, trainingProgramId); @@ -251,15 +251,14 @@ public void unmarkExerciseAsCompleted(Long trainingProgramId, Long exerciseId, H String updatedProgressJson = objectMapper.writeValueAsString(exerciseProgress); userTrainingProgram.setExerciseProgress(updatedProgressJson); // Save the updated JSON string } catch (Exception e) { - e.printStackTrace(); - // Handle exception, possibly throw a runtime exception or return an error response + throw new IllegalStateException("Failed to update exercise progress JSON", e); } - userTrainingProgramRepository.save(userTrainingProgram); + return mapper.mapToUserTrainingProgramResponse(userTrainingProgramRepository.save(userTrainingProgram)); } @Transactional - public void markTrainingProgramAsCompleted(Long trainingProgramId, HttpServletRequest request) { + public UserTrainingProgramResponse markTrainingProgramAsCompleted(Long trainingProgramId, HttpServletRequest request) { User user = authenticationService.getAuthenticatedUserInternal(request); UserTrainingProgram userTrainingProgram = getOngoingUserTrainingProgram(user, trainingProgramId); @@ -267,11 +266,11 @@ public void markTrainingProgramAsCompleted(Long trainingProgramId, HttpServletRe // Mark the entire training program as completed userTrainingProgram.setStatus(UserTrainingProgramStatus.COMPLETED); - userTrainingProgramRepository.save(userTrainingProgram); + return mapper.mapToUserTrainingProgramResponse(userTrainingProgramRepository.save(userTrainingProgram)); } @Transactional - public void leaveTrainingProgram(Long trainingProgramId, HttpServletRequest request) { + public UserTrainingProgramResponse leaveTrainingProgram(Long trainingProgramId, HttpServletRequest request) { User user = authenticationService.getAuthenticatedUserInternal(request); UserTrainingProgram userTrainingProgram = getOngoingUserTrainingProgram(user, trainingProgramId); @@ -280,7 +279,7 @@ public void leaveTrainingProgram(Long trainingProgramId, HttpServletRequest requ // Mark the training program as left userTrainingProgram.setStatus(UserTrainingProgramStatus.LEFT); - userTrainingProgramRepository.save(userTrainingProgram); + return mapper.mapToUserTrainingProgramResponse(userTrainingProgramRepository.save(userTrainingProgram)); } }