Skip to content

Commit

Permalink
Merge pull request #221 from bounswe/change-responses-for-training-pr…
Browse files Browse the repository at this point in the history
…ogram-tracking-endpoints

Add modified training programs in the response for tracking endpoints
  • Loading branch information
oguzhekim authored Nov 24, 2024
2 parents f5a37a9 + e69be7b commit 42db134
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public ResponseEntity<Void> deleteTrainingProgram(@PathVariable Long id, HttpSer


@PostMapping("/{programId}/join")
public ResponseEntity<String> joinTrainingProgram(@PathVariable Long programId , HttpServletRequest request) {
trainingProgramService.joinTrainingProgram(programId ,request);
return ResponseEntity.ok("User has successfully joined the training program.");
public ResponseEntity<UserTrainingProgramResponse> joinTrainingProgram(@PathVariable Long programId , HttpServletRequest request) {
UserTrainingProgramResponse response = trainingProgramService.joinTrainingProgram(programId ,request);
return ResponseEntity.ok(response);
}

@DeleteMapping("/{programId}/leave")
public ResponseEntity<String> leaveProgram(@PathVariable Long programId, HttpServletRequest request) {
trainingProgramService.leaveTrainingProgram(programId, request);
return ResponseEntity.ok("Successfully left the training program.");
public ResponseEntity<UserTrainingProgramResponse> leaveProgram(@PathVariable Long programId, HttpServletRequest request) {
UserTrainingProgramResponse response = trainingProgramService.leaveTrainingProgram(programId, request);
return ResponseEntity.ok(response);
}

@GetMapping("/{programId}/participants")
Expand All @@ -85,31 +85,31 @@ public ResponseEntity<List<TrainingProgramResponse>> getTrainingProgramsByTraine
}

@PostMapping("/{trainingProgramId}/exercises/{exerciseId}/complete")
public ResponseEntity<Void> markExerciseAsCompleted(
public ResponseEntity<UserTrainingProgramResponse> 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<Void> unmarkExerciseAsCompleted(
public ResponseEntity<UserTrainingProgramResponse> 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<Void> markTrainingProgramAsCompleted(
public ResponseEntity<UserTrainingProgramResponse> 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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Long, Boolean> exerciseProgress = trainingProgram.getExercises().stream()
.collect(Collectors.toMap(
TrainingProgramExercise::getId, // Exercise ID
Expand All @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -251,27 +251,26 @@ 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);

// 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);
Expand All @@ -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));
}

}

0 comments on commit 42db134

Please sign in to comment.