From 1abae6d1e41659ebe3230352c58e0cf0550f19eb Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 26 Jul 2024 11:43:56 +1000 Subject: Add test not found handling to /aggregate endpoint --- .../stileeducation/markr/service/TestResultsService.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/main/java/com/stileeducation/markr/service/TestResultsService.java') diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java index 8dba13f..a06400e 100644 --- a/src/main/java/com/stileeducation/markr/service/TestResultsService.java +++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java @@ -7,6 +7,7 @@ import com.stileeducation.markr.dto.MCQTestResultsDTO; import com.stileeducation.markr.entity.Student; import com.stileeducation.markr.entity.Test; import com.stileeducation.markr.entity.TestResult; +import com.stileeducation.markr.exception.TestNotFoundException; import com.stileeducation.markr.repository.TestResultRepository; import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation; import org.apache.commons.math3.stat.descriptive.rank.Percentile; @@ -43,12 +44,13 @@ public class TestResultsService { public AggregateResponseDTO aggregateTestResults(String testId) { AggregateResponseDTO aggregateResponseDTO = new AggregateResponseDTO(); - testService.findTest(testId).ifPresent(test -> { - List testResults = findAllByTestId(testId); - if (!testResults.isEmpty()) { - populateAggregateResponse(aggregateResponseDTO, test, testResults); - } - }); + Test test = testService.findTest(testId) + .orElseThrow(() -> new TestNotFoundException("Test with ID " + testId + " not found")); + + List testResults = findAllByTestId(testId); + if (!testResults.isEmpty()) { + populateAggregateResponse(aggregateResponseDTO, test, testResults); + } return aggregateResponseDTO; } -- cgit v1.2.3