summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/stileeducation/markr/entity/Student.java92
-rw-r--r--src/main/java/com/stileeducation/markr/entity/Test.java80
-rw-r--r--src/main/java/com/stileeducation/markr/entity/TestResult.java80
-rw-r--r--src/main/java/com/stileeducation/markr/repository/StudentRepository.java10
-rw-r--r--src/main/java/com/stileeducation/markr/repository/TestRepository.java10
-rw-r--r--src/main/java/com/stileeducation/markr/repository/TestResultRepository.java19
-rw-r--r--src/main/resources/application.properties10
7 files changed, 301 insertions, 0 deletions
diff --git a/src/main/java/com/stileeducation/markr/entity/Student.java b/src/main/java/com/stileeducation/markr/entity/Student.java
new file mode 100644
index 0000000..9ef2091
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/entity/Student.java
@@ -0,0 +1,92 @@
+package com.stileeducation.markr.entity;
+
+import jakarta.persistence.*;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+@Entity
+@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 +
+ '}';
+ }
+} \ 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
new file mode 100644
index 0000000..3729e1b
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/entity/Test.java
@@ -0,0 +1,80 @@
+package com.stileeducation.markr.entity;
+
+import jakarta.persistence.*;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+@Entity
+@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 +
+ '}';
+ }
+}
diff --git a/src/main/java/com/stileeducation/markr/entity/TestResult.java b/src/main/java/com/stileeducation/markr/entity/TestResult.java
new file mode 100644
index 0000000..cb7cea5
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/entity/TestResult.java
@@ -0,0 +1,80 @@
+package com.stileeducation.markr.entity;
+
+import jakarta.persistence.*;
+
+import java.util.Objects;
+
+@Entity
+@Table(name = "test_results")
+public class TestResult {
+
+ @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 +
+ '}';
+ }
+} \ No newline at end of file
diff --git a/src/main/java/com/stileeducation/markr/repository/StudentRepository.java b/src/main/java/com/stileeducation/markr/repository/StudentRepository.java
new file mode 100644
index 0000000..2d27644
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/repository/StudentRepository.java
@@ -0,0 +1,10 @@
+package com.stileeducation.markr.repository;
+
+import com.stileeducation.markr.entity.Student;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.Optional;
+
+public interface StudentRepository extends JpaRepository<Student, Long> {
+ Optional<Student> findByStudentNumber(String studentNumber);
+}
diff --git a/src/main/java/com/stileeducation/markr/repository/TestRepository.java b/src/main/java/com/stileeducation/markr/repository/TestRepository.java
new file mode 100644
index 0000000..f30ae0b
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/repository/TestRepository.java
@@ -0,0 +1,10 @@
+package com.stileeducation.markr.repository;
+
+import com.stileeducation.markr.entity.Test;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+import java.util.Optional;
+
+public interface TestRepository extends JpaRepository<Test, Long> {
+ Optional<Test> findByTestId(String testId);
+}
diff --git a/src/main/java/com/stileeducation/markr/repository/TestResultRepository.java b/src/main/java/com/stileeducation/markr/repository/TestResultRepository.java
new file mode 100644
index 0000000..5810f8e
--- /dev/null
+++ b/src/main/java/com/stileeducation/markr/repository/TestResultRepository.java
@@ -0,0 +1,19 @@
+package com.stileeducation.markr.repository;
+
+import com.stileeducation.markr.entity.Student;
+import com.stileeducation.markr.entity.Test;
+import com.stileeducation.markr.entity.TestResult;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
+import java.util.Optional;
+
+public interface TestResultRepository extends JpaRepository<TestResult, Long> {
+
+ Optional<TestResult> findByStudentAndTest(Student student, Test test);
+
+ @Query("SELECT tr FROM TestResult tr WHERE tr.test.testId = :testId")
+ List<TestResult> findAllByTestId(@Param("testId") String testId);
+}
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 7e171c2..788b1a5 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -1 +1,11 @@
spring.application.name=markr
+
+# H2 Database configuration
+spring.datasource.url=jdbc:h2:mem:testdb
+spring.datasource.driverClassName=org.h2.Driver
+spring.datasource.username=sa
+spring.datasource.password=password
+spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
+spring.h2.console.enabled=true
+spring.h2.console.path=/h2-console
+spring.jpa.hibernate.ddl-auto=update