Added a basic first index with header fragment, and configured the project for mysql.

This commit is contained in:
Andrew Lalis 2019-03-03 09:08:36 +01:00 committed by andrewlalis
parent a22d759399
commit 672bafbfcf
13 changed files with 199 additions and 9 deletions

View File

@ -69,10 +69,16 @@
<scope>test</scope>
</dependency>
<!-- Database Drivers -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
<build>

View File

@ -1,13 +1,29 @@
package nl.andrewlalis.teaching_assistant_assistant;
import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.repository.CrudRepository;
@SpringBootApplication
public class TeachingAssistantAssistantApplication {
public class TeachingAssistantAssistantApplication implements CommandLineRunner {
@Autowired
CourseRepository courseRepository;
public static void main(String[] args) {
SpringApplication.run(TeachingAssistantAssistantApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("Running startup...");
courseRepository.save(new Course("Intro to Information Systems", "INF-1"));
courseRepository.save(new Course("Program Correctness", "PC-002"));
System.out.println("Saved two courses.");
System.out.println("Course count: " + courseRepository.count());
}
}

View File

@ -0,0 +1,34 @@
package nl.andrewlalis.teaching_assistant_assistant.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* Configures the data source for this application.
*/
@Configuration
public class DataSourceConfig {
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static final String DB_HOST = "localhost";
private static final String DB_NAME = "teaching_assistant_assistant";
@ConfigurationProperties(prefix = "spring.datasource")
@Bean
@Primary
public DataSource getDataSource() {
return DataSourceBuilder
.create()
.url("jdbc:mysql://" + DB_HOST + '/' + DB_NAME)
.username(USERNAME)
.password(PASSWORD)
.build();
}
}

View File

@ -0,0 +1,20 @@
package nl.andrewlalis.teaching_assistant_assistant.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class RootController {
@RequestMapping(
path = "/",
produces = "text/html"
)
public String index(Model model) {
model.addAttribute("name", "JOHN");
return "index.html";
}
}

View File

@ -10,10 +10,12 @@ import java.util.Date;
* in this system should extend from BasicEntity.
*/
@MappedSuperclass
public class BasicEntity {
public abstract class BasicEntity {
@Id
@GeneratedValue
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
private Long id;
@Temporal(

View File

@ -1,9 +1,11 @@
package nl.andrewlalis.teaching_assistant_assistant.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import nl.andrewlalis.teaching_assistant_assistant.model.assignments.Assignment;
import nl.andrewlalis.teaching_assistant_assistant.model.people.Student;
import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistant;
import javax.persistence.*;
import java.util.List;
/**
* Represents a course, containing many students and teaching assistants, as well as a collection of assignments.
@ -14,15 +16,64 @@ import javax.persistence.InheritanceType;
)
public class Course extends BasicEntity {
/**
* The human-readable name for this course.
*/
@Column(unique = true, nullable = false)
private String name;
/**
* The unique identifying code for this course.
*/
@Column(unique = true, nullable = false)
private String code;
/**
* The list of students participating in this course. This is a many-to-many relationship because a course can
* contain multiple students, and a student can participate in multiple courses at once.
*/
@ManyToMany
@JoinTable(
name = "course_student",
joinColumns = @JoinColumn(name = "course_id"),
inverseJoinColumns = @JoinColumn(name = "student_id")
)
private List<Student> students;
/**
* The list of teaching assistants managing this course. Just like for students, this is a many-to-many relation.
*/
@ManyToMany
@JoinTable(
name = "course_teachingAssistant",
joinColumns = @JoinColumn(name = "course_id"),
inverseJoinColumns = @JoinColumn(name = "teachingAssistant_id")
)
private List<TeachingAssistant> teachingAssistants;
/**
* The list of assignments this course contains.
*/
@OneToMany(
fetch = FetchType.LAZY,
orphanRemoval = true
)
@JoinColumn(name = "course_id")
private List<Assignment> assignments;
/**
* Default constructor for JPA.
*/
protected Course() {}
/**
* Constructs a new Course object.
* @param name The name of the course.
* @param code The course's unique code.
*/
public Course(String name, String code) {
this.name = name;
this.code = code;
}
}

View File

@ -31,6 +31,7 @@ public class Assignment extends BasicEntity {
fetch = FetchType.EAGER,
orphanRemoval = true
)
@JoinColumn(name = "assignment_id")
private List<AssignmentSection> sections;

View File

@ -11,7 +11,7 @@ import javax.persistence.*;
@Inheritance(
strategy = InheritanceType.JOINED
)
public class Person extends BasicEntity {
public abstract class Person extends BasicEntity {
@Column(nullable = false)
private String firstName;

View File

@ -8,4 +8,10 @@ import javax.persistence.Entity;
@Entity
public class Student extends Person {
public Student(String firstName, String lastName, String emailAddress) {
super(firstName, lastName, emailAddress);
}
}

View File

@ -0,0 +1,12 @@
package nl.andrewlalis.teaching_assistant_assistant.model.repositories;
import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
/**
* The repository used to interact with various Course objects.
*/
@Repository
public interface CourseRepository extends CrudRepository<Course, Long> {
}

View File

@ -1 +1 @@
spring.jpa.hibernate.ddl-auto=create-drop

View File

@ -0,0 +1,24 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<nav th:fragment="header">
<ul>
<li>
<a href="#" th:href="@{/}">Home</a>
</li>
<li>
<a href="#" th:href="@{/courses}">Courses</a>
</li>
<li>
<a href="#" th:href="@{/students}">Students</a>
</li>
</ul>
</nav>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}">&nbsp;</div>
<div>
<p>
Welcome to the home page.
</p>
</div>
</body>
</html>