summaryrefslogtreecommitdiff
path: root/src/main/java/com/stileeducation/markr/controller/TestResultsController.java
blob: c713df6ecf176913740163e806bd4b49834b2581 (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
package com.stileeducation.markr.controller;

import com.stileeducation.markr.dto.AggregateResponseDTO;
import com.stileeducation.markr.dto.ImportResponseDTO;
import com.stileeducation.markr.dto.MCQTestResultsDTO;
import com.stileeducation.markr.service.TestResultsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
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";

  private final TestResultsService testResultsService;

  public TestResultsController(TestResultsService testResultsService) {
    this.testResultsService = testResultsService;
  }

  @PostMapping(value = IMPORT_ENDPOINT, consumes = "text/xml+markr", produces = "application/json")
  public ResponseEntity<ImportResponseDTO> postTestResults(@Validated @RequestBody MCQTestResultsDTO testResults) {
    ImportResponseDTO response = testResultsService.processTestResults(testResults);
    if ("failure".equals(response.getStatus())) {
      return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>(response, HttpStatus.OK);
  }

  @GetMapping(value = AGGREGATE_ENDPOINT, produces = "application/json")
  public AggregateResponseDTO getAggregatedResults(@PathVariable("test-id") String testId) {

    // Should return 404 if test is not found.

    return testResultsService.aggregateTestResults(testId);
  }

  @ExceptionHandler(MethodArgumentNotValidException.class)
  public ResponseEntity<ImportResponseDTO> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
    ImportResponseDTO response = new ImportResponseDTO();
    response.setStatus("error");
    response.setMessage("Invalid payload");
    return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
  }

  @ExceptionHandler(HttpMessageNotReadableException.class)
  public ResponseEntity<ImportResponseDTO> handleHttpMessageNotReadableException(HttpMessageNotReadableException ex) {
    ImportResponseDTO response = new ImportResponseDTO();
    response.setStatus("error");
    response.setMessage("Invalid XML payload");
    return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
  }

  @ExceptionHandler(Exception.class)
  public ResponseEntity<ImportResponseDTO> handleGenericException(Exception ex) {
    ImportResponseDTO response = new ImportResponseDTO();
    response.setStatus("error");
    response.setMessage("An unexpected error occurred");
    return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
  }
}