Skip to content

Commit

Permalink
Merge pull request #228 from bounswe/refactor-tracking-mechanisms
Browse files Browse the repository at this point in the history
Refactor training program tracking mechanism
  • Loading branch information
oguzhekim authored Nov 25, 2024
2 parents 40a6da6 + c8b75e4 commit ef5aebd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.group7.demo.dtos.TrainingProgramResponse;
import com.group7.demo.dtos.UserTrainingProgramResponse;
import com.group7.demo.services.TrainingProgramService;
import jakarta.persistence.EntityNotFoundException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -61,14 +62,22 @@ public ResponseEntity<Void> deleteTrainingProgram(@PathVariable Long id, HttpSer

@PostMapping("/{programId}/join")
public ResponseEntity<UserTrainingProgramResponse> joinTrainingProgram(@PathVariable Long programId , HttpServletRequest request) {
UserTrainingProgramResponse response = trainingProgramService.joinTrainingProgram(programId ,request);
return ResponseEntity.ok(response);
try {
UserTrainingProgramResponse response = trainingProgramService.joinTrainingProgram(programId ,request);
return ResponseEntity.ok(response);
} catch (IllegalStateException | EntityNotFoundException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

@DeleteMapping("/{programId}/leave")
public ResponseEntity<UserTrainingProgramResponse> leaveProgram(@PathVariable Long programId, HttpServletRequest request) {
UserTrainingProgramResponse response = trainingProgramService.leaveTrainingProgram(programId, request);
return ResponseEntity.ok(response);
try {
UserTrainingProgramResponse response = trainingProgramService.leaveTrainingProgram(programId, request);
return ResponseEntity.ok(response);
} catch (IllegalStateException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

@GetMapping("/{programId}/participants")
Expand All @@ -90,26 +99,38 @@ public ResponseEntity<UserTrainingProgramResponse> markExerciseAsCompleted(
@PathVariable Long exerciseId,
HttpServletRequest request
) {
UserTrainingProgramResponse response = trainingProgramService.markExerciseAsCompleted(trainingProgramId, exerciseId, request);
return ResponseEntity.ok(response);
try{
UserTrainingProgramResponse response = trainingProgramService.markExerciseAsCompleted(trainingProgramId, exerciseId, request);
return ResponseEntity.ok(response);
} catch (IllegalStateException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

@PostMapping("/{trainingProgramId}/exercises/{exerciseId}/uncomplete")
public ResponseEntity<UserTrainingProgramResponse> unmarkExerciseAsCompleted(
@PathVariable Long trainingProgramId,
@PathVariable Long exerciseId,
HttpServletRequest request) {
UserTrainingProgramResponse response = trainingProgramService.unmarkExerciseAsCompleted(trainingProgramId, exerciseId, request);
return ResponseEntity.ok(response);
try{
UserTrainingProgramResponse response = trainingProgramService.unmarkExerciseAsCompleted(trainingProgramId, exerciseId, request);
return ResponseEntity.ok(response);
} catch (IllegalStateException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

@PostMapping("/{trainingProgramId}/complete")
public ResponseEntity<UserTrainingProgramResponse> markTrainingProgramAsCompleted(
@PathVariable Long trainingProgramId,
HttpServletRequest request
) {
UserTrainingProgramResponse response = trainingProgramService.markTrainingProgramAsCompleted(trainingProgramId, request);
return ResponseEntity.ok(response);
try {
UserTrainingProgramResponse response = trainingProgramService.markTrainingProgramAsCompleted(trainingProgramId, request);
return ResponseEntity.ok(response);
} catch (IllegalStateException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

@GetMapping("/joined/{username}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

import com.group7.demo.models.User;
import com.group7.demo.models.UserTrainingProgram;
import com.group7.demo.models.enums.UserTrainingProgramStatus;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface UserTrainingProgramRepository extends JpaRepository<UserTrainingProgram, Long> {
List<UserTrainingProgram> findAllByUserAndTrainingProgramId(User user, Long trainingProgramId);
Optional<UserTrainingProgram> findByUserIdAndTrainingProgramId(Long userId, Long programId);

boolean existsByUserAndTrainingProgramIdAndStatus(User user, Long trainingProgramId, UserTrainingProgramStatus status);
List<UserTrainingProgram> findByUserAndTrainingProgramIdAndStatus(User user, Long trainingProgramId, UserTrainingProgramStatus status);
List<UserTrainingProgram> findByUser(User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,10 @@ public UserTrainingProgramResponse joinTrainingProgram(Long trainingProgramId ,
TrainingProgram trainingProgram = trainingProgramRepository.findById(trainingProgramId)
.orElseThrow(() -> new EntityNotFoundException("Training Program not found with id: " + trainingProgramId));

// Check for all past participations
List<UserTrainingProgram> pastEntries = userTrainingProgramRepository.findAllByUserAndTrainingProgramId(user, trainingProgramId);
// Check if the user is already actively participating in this training program
boolean hasOngoingProgram = userTrainingProgramRepository.existsByUserAndTrainingProgramIdAndStatus(user, trainingProgramId, UserTrainingProgramStatus.ONGOING);

boolean hasOngoingEntry = pastEntries.stream()
.anyMatch(entry -> entry.getStatus() == UserTrainingProgramStatus.ONGOING);

if (hasOngoingEntry) {
if (hasOngoingProgram) {
throw new IllegalStateException("User is already actively participating in this training program.");
}

Expand Down Expand Up @@ -199,12 +196,15 @@ public List<UserTrainingProgramResponse> getJoinedTrainingPrograms(String userna
}

private UserTrainingProgram getOngoingUserTrainingProgram(User user, Long trainingProgramId) {
// Fetch all entries and filter the ongoing one
return userTrainingProgramRepository.findAllByUserAndTrainingProgramId(user, trainingProgramId)
.stream()
.filter(entry -> entry.getStatus() == UserTrainingProgramStatus.ONGOING)
.findFirst()
.orElseThrow(() -> new IllegalStateException("No ongoing training program found."));
List<UserTrainingProgram> ongoingPrograms = userTrainingProgramRepository.findByUserAndTrainingProgramIdAndStatus(user, trainingProgramId, UserTrainingProgramStatus.ONGOING);

if (ongoingPrograms.isEmpty()) {
throw new IllegalStateException("No ongoing training program found.");
}

// Return the first ongoing program if available
return ongoingPrograms.get(0);

}

@Transactional
Expand Down

0 comments on commit ef5aebd

Please sign in to comment.