summaryrefslogtreecommitdiff
path: root/src/main/java/com/stileeducation/markr/service
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/stileeducation/markr/service')
-rw-r--r--src/main/java/com/stileeducation/markr/service/StudentService.java1
-rw-r--r--src/main/java/com/stileeducation/markr/service/TestResultsService.java94
-rw-r--r--src/main/java/com/stileeducation/markr/service/TestService.java12
3 files changed, 93 insertions, 14 deletions
diff --git a/src/main/java/com/stileeducation/markr/service/StudentService.java b/src/main/java/com/stileeducation/markr/service/StudentService.java
index 4af4d63..3ce28c0 100644
--- a/src/main/java/com/stileeducation/markr/service/StudentService.java
+++ b/src/main/java/com/stileeducation/markr/service/StudentService.java
@@ -22,6 +22,7 @@ public class StudentService {
student.setFirstName(firstName);
student.setLastName(lastName);
student.setStudentNumber(studentNumber);
+ student.setCreated(true);
return studentRepository.save(student);
}
}
diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java
index 95b42f3..51efe95 100644
--- a/src/main/java/com/stileeducation/markr/service/TestResultsService.java
+++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java
@@ -1,11 +1,12 @@
package com.stileeducation.markr.service;
-import com.stileeducation.markr.dto.AggregatedTestResultsDTO;
+import com.stileeducation.markr.dto.AggregateResponseDTO;
+import com.stileeducation.markr.dto.ImportResponseDTO;
+import com.stileeducation.markr.dto.MCQTestResultDTO;
+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.repository.StudentRepository;
-import com.stileeducation.markr.repository.TestRepository;
import com.stileeducation.markr.repository.TestResultRepository;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
import org.apache.commons.math3.stat.descriptive.rank.Percentile;
@@ -19,27 +20,34 @@ import java.util.Optional;
public class TestResultsService {
public static final boolean IS_BIAS_CORRECTED = false;
+
@Autowired
private TestResultRepository testResultRepository;
@Autowired
- private StudentRepository studentRepository;
+ private StudentService studentService;
@Autowired
- private TestRepository testRepository;
-
- public TestResult findOrCreateTestResult(Long studentId, Long testId, Integer marksAwarded) {
- Student student = studentRepository.findById(studentId).orElseThrow(() -> new RuntimeException("Student not found"));
- Test test = testRepository.findById(testId).orElseThrow(() -> new RuntimeException("Test not found"));
+ private TestService testService;
+ public TestResult findOrCreateTestResult(Student student, Test test, Integer marksAwarded) {
Optional<TestResult> optionalTestResult = testResultRepository.findByStudentAndTest(student, test);
if (optionalTestResult.isPresent()) {
- return optionalTestResult.get();
+ TestResult testResult = optionalTestResult.get();
+ if (marksAwarded > testResult.getMarksAwarded()) {
+ testResult.setMarksAwarded(marksAwarded);
+ testResult.setUpdated(true);
+ testResultRepository.save(testResult);
+ } else {
+ testResult.setUpdated(false);
+ }
+ return testResult;
} else {
TestResult testResult = new TestResult();
testResult.setStudent(student);
testResult.setTest(test);
testResult.setMarksAwarded(marksAwarded);
+ testResult.setCreated(true);
return testResultRepository.save(testResult);
}
}
@@ -126,10 +134,10 @@ public class TestResultsService {
return new Percentile().evaluate(marks, 75.0);
}
- public AggregatedTestResultsDTO aggregateTestResults(String testId) {
+ public AggregateResponseDTO aggregateTestResults(String testId) {
List<TestResult> testResults = findAllByTestId(testId);
- AggregatedTestResultsDTO results = new AggregatedTestResultsDTO();
+ AggregateResponseDTO results = new AggregateResponseDTO();
results.setMean(calculateMeanOfTestResults(testResults));
results.setStddev(calculateStandardDeviationOfTestResults(testResults));
@@ -143,4 +151,66 @@ public class TestResultsService {
return results;
}
+ public ImportResponseDTO processTestResults(MCQTestResultsDTO testResults) {
+ ImportResponseDTO.ImportData importData = new ImportResponseDTO.ImportData();
+ boolean isValid = true;
+
+ for (MCQTestResultDTO mcqTestResult : testResults.getMcqTestResults()) {
+ try {
+
+ Student student = studentService
+ .findOrCreateStudent(
+ mcqTestResult.getFirstName(),
+ mcqTestResult.getLastName(),
+ mcqTestResult.getStudentNumber());
+
+ if (student.isCreated()) {
+ importData.incrementStudentsCreated();
+ }
+ if (student.isUpdated()) {
+ importData.incrementStudentsUpdated();
+ }
+
+ Test test = testService
+ .findOrCreateTest(
+ mcqTestResult.getTestId(),
+ mcqTestResult.getSummaryMarks().getAvailable());
+
+ if (test.isCreated()) {
+ importData.incrementTestsCreated();
+ }
+ if (test.isUpdated()) {
+ importData.incrementTestsUpdated();
+ }
+
+ TestResult testResult =
+ findOrCreateTestResult(
+ student,
+ test,
+ mcqTestResult.getSummaryMarks().getObtained());
+
+ if (testResult.isCreated()) {
+ importData.incrementTestResultsCreated();
+ }
+ if (testResult.isUpdated()) {
+ importData.incrementTestResultsUpdated();
+ }
+ } catch (Exception e) {
+ isValid = false;
+ }
+ }
+
+ ImportResponseDTO response = new ImportResponseDTO();
+ response.setData(importData);
+
+ if (isValid) {
+ response.setStatus("success");
+ response.setMessage("Import operation completed successfully.");
+ } else {
+ response.setStatus("failure");
+ response.setMessage("Data was invalid or processing failed.");
+ }
+
+ return response;
+ }
} \ No newline at end of file
diff --git a/src/main/java/com/stileeducation/markr/service/TestService.java b/src/main/java/com/stileeducation/markr/service/TestService.java
index f3ba98c..f3231b9 100644
--- a/src/main/java/com/stileeducation/markr/service/TestService.java
+++ b/src/main/java/com/stileeducation/markr/service/TestService.java
@@ -16,13 +16,21 @@ public class TestService {
public Test findOrCreateTest(String testId, Integer marksAvailable) {
Optional<Test> optionalTest = testRepository.findByTestId(testId);
if (optionalTest.isPresent()) {
- return optionalTest.get();
+ Test test = optionalTest.get();
+ if (test.getMarksAvailable() < marksAvailable) {
+ test.setMarksAvailable(marksAvailable);
+ test.setUpdated(true);
+ testRepository.save(test);
+ } else {
+ test.setUpdated(false);
+ }
+ return test;
} else {
Test test = new Test();
test.setTestId(testId);
test.setMarksAvailable(marksAvailable);
+ test.setCreated(true);
return testRepository.save(test);
}
}
-
}