summaryrefslogtreecommitdiff
path: root/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/stileeducation/markr/controller/TestResultsController.java')
-rw-r--r--src/main/java/com/stileeducation/markr/controller/TestResultsController.java58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
index 5667027..c69492d 100644
--- a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
+++ b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
@@ -1,8 +1,17 @@
package com.stileeducation.markr.controller;
import com.stileeducation.markr.dto.AggregatedTestResultsDTO;
+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.TestRepository;
+import com.stileeducation.markr.repository.TestResultRepository;
+import com.stileeducation.markr.service.StudentService;
import com.stileeducation.markr.service.TestResultsService;
+import com.stileeducation.markr.service.TestService;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -14,7 +23,20 @@ public class TestResultsController {
public static final String IMPORT_ENDPOINT = "/import";
public static final String AGGREGATE_ENDPOINT = "/results/{test-id}/aggregate";
- private final TestResultsService testResultsService;
+ @Autowired
+ private StudentService studentService;
+
+ @Autowired
+ private TestService testService;
+
+ @Autowired
+ private TestResultsService testResultsService;
+
+ @Autowired
+ private TestRepository testRepository;
+
+ @Autowired
+ private TestResultRepository testResultRepository;
public TestResultsController(TestResultsService testResultsService) {
this.testResultsService = testResultsService;
@@ -23,7 +45,39 @@ public class TestResultsController {
// TODO consider return value
@PostMapping(value = IMPORT_ENDPOINT, consumes = "text/xml+markr", produces = "application/json")
public ResponseEntity<Void> handleXmlRequest(@RequestBody MCQTestResultsDTO testResults) {
- testResultsService.importTestResults(testResults);
+
+ for (MCQTestResultDTO mcqTestResult : testResults.getMcqTestResults()) {
+ Student student = studentService
+ .findOrCreateStudent(
+ mcqTestResult.getFirstName(),
+ mcqTestResult.getLastName(),
+ mcqTestResult.getStudentNumber());
+
+ Test test = testService
+ .findOrCreateTest(
+ mcqTestResult.getTestId(),
+ mcqTestResult.getSummaryMarks().getAvailable());
+
+ if (test.getMarksAvailable() < mcqTestResult.getSummaryMarks().getAvailable()) {
+ test.setMarksAvailable(mcqTestResult.getSummaryMarks().getAvailable());
+ testRepository.save(test);
+ }
+
+ // Some edge cases to consider
+ // obtained is higher than available (assumption?)
+
+ TestResult testResult = testResultsService
+ .findOrCreateTestResult(
+ student.getId(),
+ test.getId(),
+ mcqTestResult.getSummaryMarks().getObtained());
+
+ if (testResult.getMarksAwarded() < mcqTestResult.getSummaryMarks().getObtained()) {
+ testResult.setMarksAwarded(mcqTestResult.getSummaryMarks().getObtained());
+ testResultRepository.save(testResult);
+ }
+ }
+
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}