diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-07-25 12:10:25 +1000 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-07-25 12:10:25 +1000 |
| commit | 98f6bffd91a83fd2fee5481255604b302d59dd59 (patch) | |
| tree | f54d0292c97dc250f7b5c02b4db518a85ba6f713 /src/main/java/com | |
| parent | 7429ce2c63f467cc6e0f742c644b53dcdd5a635b (diff) | |
Fix formatting and add some clarifying comments
Diffstat (limited to 'src/main/java/com')
6 files changed, 333 insertions, 316 deletions
diff --git a/src/main/java/com/stileeducation/markr/config/WebConfig.java b/src/main/java/com/stileeducation/markr/config/WebConfig.java index 6d9c1a0..365e1af 100644 --- a/src/main/java/com/stileeducation/markr/config/WebConfig.java +++ b/src/main/java/com/stileeducation/markr/config/WebConfig.java @@ -10,8 +10,10 @@ import java.util.List; @Configuration public class WebConfig implements WebMvcConfigurer { - @Override - public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { - converters.add(new XmlMarkrMessageConverter()); - } + @Override + public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { + + // Register custom message converter to support text/xml+mark content type + converters.add(new XmlMarkrMessageConverter()); + } } diff --git a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java index c69492d..1376ecb 100644 --- a/src/main/java/com/stileeducation/markr/controller/TestResultsController.java +++ b/src/main/java/com/stileeducation/markr/controller/TestResultsController.java @@ -20,70 +20,75 @@ import org.springframework.web.bind.annotation.*; @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); - } + 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; } - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } + // 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); - } + @GetMapping( + value = AGGREGATE_ENDPOINT, + produces = "application/json") + public AggregatedTestResultsDTO getAggregatedResults(@PathVariable("test-id") String testId) { + return testResultsService.aggregateTestResults(testId); + } } diff --git a/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java b/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java index 04207a1..bc84b72 100644 --- a/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java +++ b/src/main/java/com/stileeducation/markr/converter/XmlMarkrMessageConverter.java @@ -13,31 +13,34 @@ import javax.xml.transform.Source; import java.util.Collections; import java.util.List; +/** + * Custom Message Converter for handling the text/xml+mark content type + */ public class XmlMarkrMessageConverter extends AbstractXmlHttpMessageConverter<MCQTestResultsDTO> { - public static final MediaType MEDIA_TYPE = new MediaType("text", "xml+markr"); - - @Override - protected MCQTestResultsDTO readFromSource(Class<? extends MCQTestResultsDTO> clazz, HttpHeaders headers, Source source) throws Exception { - JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class); - Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); - return (MCQTestResultsDTO) unmarshaller.unmarshal(source); - } - - @Override - protected void writeToResult(MCQTestResultsDTO testResultsDTO, HttpHeaders headers, Result result) throws Exception { - JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class); - Marshaller marshaller = jaxbContext.createMarshaller(); - marshaller.marshal(testResultsDTO, result); - } - - @Override - protected boolean supports(Class<?> clazz) { - return MCQTestResultsDTO.class.isAssignableFrom(clazz); - } - - @Override - public List<MediaType> getSupportedMediaTypes() { - return Collections.singletonList(MEDIA_TYPE); - } + public static final MediaType MEDIA_TYPE = new MediaType("text", "xml+markr"); + + @Override + protected MCQTestResultsDTO readFromSource(Class<? extends MCQTestResultsDTO> clazz, HttpHeaders headers, Source source) throws Exception { + JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class); + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); + return (MCQTestResultsDTO) unmarshaller.unmarshal(source); + } + + @Override + protected void writeToResult(MCQTestResultsDTO testResultsDTO, HttpHeaders headers, Result result) throws Exception { + JAXBContext jaxbContext = JAXBContext.newInstance(MCQTestResultsDTO.class); + Marshaller marshaller = jaxbContext.createMarshaller(); + marshaller.marshal(testResultsDTO, result); + } + + @Override + protected boolean supports(Class<?> clazz) { + return MCQTestResultsDTO.class.isAssignableFrom(clazz); + } + + @Override + public List<MediaType> getSupportedMediaTypes() { + return Collections.singletonList(MEDIA_TYPE); + } } diff --git a/src/main/java/com/stileeducation/markr/entity/Student.java b/src/main/java/com/stileeducation/markr/entity/Student.java index 9ef2091..4e69eab 100644 --- a/src/main/java/com/stileeducation/markr/entity/Student.java +++ b/src/main/java/com/stileeducation/markr/entity/Student.java @@ -10,83 +10,87 @@ import java.util.Set; @Table(name = "students") public class Student { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(name = "first_name", nullable = false) - private String firstName; - - @Column(name = "last_name", nullable = false) - private String lastName; - - @Column(name = "student_number", nullable = false, unique = true) - private String studentNumber; - - @OneToMany(mappedBy = "student", cascade = CascadeType.ALL, orphanRemoval = true) - private Set<TestResult> testResults = new HashSet<>(); - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public String getStudentNumber() { - return studentNumber; - } - - public void setStudentNumber(String studentNumber) { - this.studentNumber = studentNumber; - } - - public Set<TestResult> getTestResults() { - return testResults; - } - - public void setTestResults(Set<TestResult> testResults) { - this.testResults = testResults; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Student student = (Student) o; - return Objects.equals(id, student.id) && Objects.equals(firstName, student.firstName) && Objects.equals(lastName, student.lastName) && Objects.equals(studentNumber, student.studentNumber) && Objects.equals(testResults, student.testResults); - } - - @Override - public int hashCode() { - return Objects.hash(id, firstName, lastName, studentNumber, testResults); - } - - @Override - public String toString() { - return "Student{" + - "id=" + id + - ", firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + - ", studentNumber='" + studentNumber + '\'' + - ", testResults=" + testResults + - '}'; - } + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "first_name", nullable = false) + private String firstName; + + @Column(name = "last_name", nullable = false) + private String lastName; + + @Column(name = "student_number", nullable = false, unique = true) + private String studentNumber; + + @OneToMany(mappedBy = "student", cascade = CascadeType.ALL, orphanRemoval = true) + private Set<TestResult> testResults = new HashSet<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getStudentNumber() { + return studentNumber; + } + + public void setStudentNumber(String studentNumber) { + this.studentNumber = studentNumber; + } + + public Set<TestResult> getTestResults() { + return testResults; + } + + public void setTestResults(Set<TestResult> testResults) { + this.testResults = testResults; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Student student = (Student) o; + return Objects.equals(id, student.id) && + Objects.equals(firstName, student.firstName) && + Objects.equals(lastName, student.lastName) && + Objects.equals(studentNumber, student.studentNumber) && + Objects.equals(testResults, student.testResults); + } + + @Override + public int hashCode() { + return Objects.hash(id, firstName, lastName, studentNumber, testResults); + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", studentNumber='" + studentNumber + '\'' + + ", testResults=" + testResults + + '}'; + } }
\ No newline at end of file diff --git a/src/main/java/com/stileeducation/markr/entity/Test.java b/src/main/java/com/stileeducation/markr/entity/Test.java index 3729e1b..615d0e1 100644 --- a/src/main/java/com/stileeducation/markr/entity/Test.java +++ b/src/main/java/com/stileeducation/markr/entity/Test.java @@ -10,71 +10,74 @@ import java.util.Set; @Table(name = "tests") public class Test { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @Column(name = "test_id", nullable = false, unique = true) - private String testId; - - @Column(name = "marks_available", nullable = false) - private Integer marksAvailable; - - @OneToMany(mappedBy = "test", cascade = CascadeType.ALL, orphanRemoval = true) - private Set<TestResult> testResults = new HashSet<>(); - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getTestId() { - return testId; - } - - public void setTestId(String testId) { - this.testId = testId; - } - - public Integer getMarksAvailable() { - return marksAvailable; - } - - public void setMarksAvailable(Integer marksAvailable) { - this.marksAvailable = marksAvailable; - } - - public Set<TestResult> getTestResults() { - return testResults; - } - - public void setTestResults(Set<TestResult> testResults) { - this.testResults = testResults; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - Test test = (Test) o; - return Objects.equals(id, test.id) && Objects.equals(testId, test.testId) && Objects.equals(marksAvailable, test.marksAvailable) && Objects.equals(testResults, test.testResults); - } - - @Override - public int hashCode() { - return Objects.hash(id, testId, marksAvailable, testResults); - } - - @Override - public String toString() { - return "Test{" + - "id=" + id + - ", testId='" + testId + '\'' + - ", marksAvailable=" + marksAvailable + - ", testResults=" + testResults + - '}'; - } + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "test_id", nullable = false, unique = true) + private String testId; + + @Column(name = "marks_available", nullable = false) + private Integer marksAvailable; + + @OneToMany(mappedBy = "test", cascade = CascadeType.ALL, orphanRemoval = true) + private Set<TestResult> testResults = new HashSet<>(); + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTestId() { + return testId; + } + + public void setTestId(String testId) { + this.testId = testId; + } + + public Integer getMarksAvailable() { + return marksAvailable; + } + + public void setMarksAvailable(Integer marksAvailable) { + this.marksAvailable = marksAvailable; + } + + public Set<TestResult> getTestResults() { + return testResults; + } + + public void setTestResults(Set<TestResult> testResults) { + this.testResults = testResults; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Test test = (Test) o; + return Objects.equals(id, test.id) && + Objects.equals(testId, test.testId) && + Objects.equals(marksAvailable, test.marksAvailable) && + Objects.equals(testResults, test.testResults); + } + + @Override + public int hashCode() { + return Objects.hash(id, testId, marksAvailable, testResults); + } + + @Override + public String toString() { + return "Test{" + + "id=" + id + + ", testId='" + testId + '\'' + + ", marksAvailable=" + marksAvailable + + ", testResults=" + testResults + + '}'; + } } diff --git a/src/main/java/com/stileeducation/markr/entity/TestResult.java b/src/main/java/com/stileeducation/markr/entity/TestResult.java index 0d68f62..26b2346 100644 --- a/src/main/java/com/stileeducation/markr/entity/TestResult.java +++ b/src/main/java/com/stileeducation/markr/entity/TestResult.java @@ -8,83 +8,83 @@ import java.util.Objects; @Table(name = "test_results") public class TestResult { - public TestResult() { - } - - public TestResult(Long id, Student student, Test test, Integer marksAwarded) { - this.id = id; - this.student = student; - this.test = test; - this.marksAwarded = marksAwarded; - } - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "student_id", nullable = false) - private Student student; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "test_id", nullable = false) - private Test test; - - @Column(name = "marks_awarded", nullable = false) - private Integer marksAwarded; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Student getStudent() { - return student; - } - - public void setStudent(Student student) { - this.student = student; - } - - public Test getTest() { - return test; - } - - public void setTest(Test test) { - this.test = test; - } - - public Integer getMarksAwarded() { - return marksAwarded; - } - - public void setMarksAwarded(Integer marksAwarded) { - this.marksAwarded = marksAwarded; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - TestResult that = (TestResult) o; - return Objects.equals(id, that.id) && Objects.equals(student, that.student) && Objects.equals(test, that.test) && Objects.equals(marksAwarded, that.marksAwarded); - } - - @Override - public int hashCode() { - return Objects.hash(id, student, test, marksAwarded); - } - - @Override - public String toString() { - return "TestResult{" + - "id=" + id + - ", student=" + student + - ", test=" + test + - ", marksAwarded=" + marksAwarded + - '}'; - } + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "student_id", nullable = false) + private Student student; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "test_id", nullable = false) + private Test test; + @Column(name = "marks_awarded", nullable = false) + private Integer marksAwarded; + + public TestResult() { + } + + public TestResult(Long id, Student student, Test test, Integer marksAwarded) { + this.id = id; + this.student = student; + this.test = test; + this.marksAwarded = marksAwarded; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student student) { + this.student = student; + } + + public Test getTest() { + return test; + } + + public void setTest(Test test) { + this.test = test; + } + + public Integer getMarksAwarded() { + return marksAwarded; + } + + public void setMarksAwarded(Integer marksAwarded) { + this.marksAwarded = marksAwarded; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + TestResult that = (TestResult) o; + return Objects.equals(id, that.id) && + Objects.equals(student, that.student) && + Objects.equals(test, that.test) && + Objects.equals(marksAwarded, that.marksAwarded); + } + + @Override + public int hashCode() { + return Objects.hash(id, student, test, marksAwarded); + } + + @Override + public String toString() { + return "TestResult{" + + "id=" + id + + ", student=" + student + + ", test=" + test + + ", marksAwarded=" + marksAwarded + + '}'; + } }
\ No newline at end of file |
