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.java8
-rw-r--r--src/main/java/com/stileeducation/markr/service/TestResultsService.java37
-rw-r--r--src/main/java/com/stileeducation/markr/service/TestService.java12
3 files changed, 42 insertions, 15 deletions
diff --git a/src/main/java/com/stileeducation/markr/service/StudentService.java b/src/main/java/com/stileeducation/markr/service/StudentService.java
index 95ce182..e49f06a 100644
--- a/src/main/java/com/stileeducation/markr/service/StudentService.java
+++ b/src/main/java/com/stileeducation/markr/service/StudentService.java
@@ -2,6 +2,7 @@ package com.stileeducation.markr.service;
import com.stileeducation.markr.entity.Student;
import com.stileeducation.markr.repository.StudentRepository;
+import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.Optional;
@@ -15,10 +16,15 @@ public class StudentService {
this.studentRepository = studentRepository;
}
+ @Transactional
public Student findOrCreateStudent(String firstName, String lastName, String studentNumber) {
Optional<Student> optionalStudent = studentRepository.findByStudentNumber(studentNumber);
if (optionalStudent.isPresent()) {
- return optionalStudent.get();
+ Student student = optionalStudent.get();
+ // Reset transients
+ student.setCreated(false);
+ student.setUpdated(false);
+ return student;
} else {
Student student = new Student();
student.setFirstName(firstName);
diff --git a/src/main/java/com/stileeducation/markr/service/TestResultsService.java b/src/main/java/com/stileeducation/markr/service/TestResultsService.java
index a06400e..3c941dd 100644
--- a/src/main/java/com/stileeducation/markr/service/TestResultsService.java
+++ b/src/main/java/com/stileeducation/markr/service/TestResultsService.java
@@ -8,7 +8,10 @@ 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.StudentRepository;
+import com.stileeducation.markr.repository.TestRepository;
import com.stileeducation.markr.repository.TestResultRepository;
+import jakarta.transaction.Transactional;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
import org.apache.commons.math3.stat.descriptive.rank.Percentile;
import org.springframework.stereotype.Service;
@@ -29,7 +32,11 @@ public class TestResultsService {
private final TestService testService;
- public TestResultsService(TestResultRepository testResultRepository, StudentService studentService, TestService testService) {
+ public TestResultsService(TestResultRepository testResultRepository,
+ StudentRepository studentRepository,
+ TestRepository testRepository,
+ StudentService studentService,
+ TestService testService) {
this.testResultRepository = testResultRepository;
this.studentService = studentService;
this.testService = testService;
@@ -37,7 +44,7 @@ public class TestResultsService {
private static double[] calculatePercentages(List<TestResult> results, double totalMarksAvailable) {
return results.stream()
- .mapToDouble(result -> (double) result.getMarksAwarded() / totalMarksAvailable * 100)
+ .mapToDouble(result -> (double) result.getMarksObtained() / totalMarksAvailable * 100)
.toArray();
}
@@ -55,6 +62,7 @@ public class TestResultsService {
return aggregateResponseDTO;
}
+ @Transactional
public ImportResponseDTO processTestResults(MCQTestResultsDTO testResults) {
ImportResponseDTO.ImportData importData = new ImportResponseDTO.ImportData();
boolean isValid = true;
@@ -70,23 +78,29 @@ public class TestResultsService {
return createImportResponse(importData, isValid);
}
- public TestResult findOrCreateTestResult(Student student, Test test, Integer marksAwarded) {
+ @Transactional
+ public TestResult findOrCreateTestResult(Student student, Test test, Integer marksObtained) {
Optional<TestResult> optionalTestResult = testResultRepository.findByStudentAndTest(student, test);
+
if (optionalTestResult.isPresent()) {
TestResult testResult = optionalTestResult.get();
- if (marksAwarded > testResult.getMarksAwarded()) {
- testResult.setMarksAwarded(marksAwarded);
+
+ // Update marks if new marks are higher
+ if (marksObtained > testResult.getMarksObtained()) {
+ testResult.setMarksObtained(marksObtained);
+ testResult.setCreated(false);
testResult.setUpdated(true);
- testResultRepository.save(testResult);
+ return testResultRepository.save(testResult);
} else {
+ testResult.setCreated(false);
testResult.setUpdated(false);
+ return testResult;
}
- return testResult;
} else {
TestResult testResult = new TestResult();
testResult.setStudent(student);
testResult.setTest(test);
- testResult.setMarksAwarded(marksAwarded);
+ testResult.setMarksObtained(marksObtained);
testResult.setCreated(true);
return testResultRepository.save(testResult);
}
@@ -186,7 +200,8 @@ public class TestResultsService {
dto.setCount(results.size());
}
- private void processTestResult(MCQTestResultDTO mcqTestResult, ImportResponseDTO.ImportData importData) {
+ @Transactional
+ protected void processTestResult(MCQTestResultDTO mcqTestResult, ImportResponseDTO.ImportData importData) {
Student student =
studentService
.findOrCreateStudent(
@@ -228,10 +243,10 @@ public class TestResultsService {
if (isValid) {
response.setStatus("success");
- response.setMessage("Import operation completed successfully.");
+ response.setMessage("Import completed successfully");
} else {
response.setStatus("failure");
- response.setMessage("Data was invalid or processing failed.");
+ response.setMessage("Data was invalid or processing failed");
}
return response;
diff --git a/src/main/java/com/stileeducation/markr/service/TestService.java b/src/main/java/com/stileeducation/markr/service/TestService.java
index 0e109ac..f42da1a 100644
--- a/src/main/java/com/stileeducation/markr/service/TestService.java
+++ b/src/main/java/com/stileeducation/markr/service/TestService.java
@@ -2,6 +2,7 @@ package com.stileeducation.markr.service;
import com.stileeducation.markr.entity.Test;
import com.stileeducation.markr.repository.TestRepository;
+import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;
import java.util.Optional;
@@ -16,22 +17,27 @@ public class TestService {
this.testRepository = testRepository;
}
+ @Transactional
public Optional<Test> findTest(String testId) {
return testRepository.findByTestId(testId);
}
+ @Transactional
public Test findOrCreateTest(String testId, Integer marksAvailable) {
Optional<Test> optionalTest = testRepository.findByTestId(testId);
+
if (optionalTest.isPresent()) {
Test test = optionalTest.get();
- if (test.getMarksAvailable() < marksAvailable) {
+ if (marksAvailable > test.getMarksAvailable()) {
test.setMarksAvailable(marksAvailable);
+ test.setCreated(false);
test.setUpdated(true);
- testRepository.save(test);
+ return testRepository.save(test);
} else {
+ test.setCreated(false);
test.setUpdated(false);
+ return test;
}
- return test;
} else {
Test test = new Test();
test.setTestId(testId);