summaryrefslogtreecommitdiff
path: root/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
blob: 1376ecbe5572cb91e780ef0ed962b376190ef298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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.*;

@RestController
@RequestMapping("/")
public class TestResultsController {

    public static final String IMPORT_ENDPOINT = "/import";
    public static final String AGGREGATE_ENDPOINT = "/results/{test-id}/aggregate";

    @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;
    }

    // TODO consider return value
    @PostMapping(
            value = IMPORT_ENDPOINT,
            consumes = "text/xml+markr",
            produces = "application/json")
    public ResponseEntity<Void> handleXmlRequest(@RequestBody MCQTestResultsDTO 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);
    }

    @GetMapping(
            value = AGGREGATE_ENDPOINT,
            produces = "application/json")
    public AggregatedTestResultsDTO getAggregatedResults(@PathVariable("test-id") String testId) {
        return testResultsService.aggregateTestResults(testId);
    }

}