Improved comment and added button for generation.

This commit is contained in:
Andrew Lalis 2019-04-16 22:30:50 +02:00 committed by andrewlalis
parent ac18001da8
commit a629b70d8b
9 changed files with 115 additions and 24 deletions

View File

@ -1,17 +1,13 @@
package nl.andrewlalis.teaching_assistant_assistant; package nl.andrewlalis.teaching_assistant_assistant;
import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.PersonRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.PersonRepository;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository; import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeamRepository;
import nl.andrewlalis.teaching_assistant_assistant.util.sample_data.CourseGenerator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.List;
@SpringBootApplication @SpringBootApplication
public class TeachingAssistantAssistantApplication implements CommandLineRunner { public class TeachingAssistantAssistantApplication implements CommandLineRunner {
@ -32,13 +28,5 @@ public class TeachingAssistantAssistantApplication implements CommandLineRunner
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
System.out.println("Running startup..."); System.out.println("Running startup...");
// Generate some example courses.
CourseGenerator courseGenerator = new CourseGenerator(0, 3, 2, 10, 3);
List<Course> courses = courseGenerator.generateList(100);
this.courseRepository.saveAll(courses);
System.out.println("Course count: " + courseRepository.count());
} }
} }

View File

@ -20,20 +20,30 @@ public class Courses {
this.courseRepository = courseRepository; this.courseRepository = courseRepository;
} }
/**
* Handles requests to get the list of courses.
* @param model The view model to populate with data for the template.
* @return The name of the template that will be used with the given model to build the view.
*/
@GetMapping("/courses") @GetMapping("/courses")
public String get(Model model) { public String get(Model model) {
model.addAttribute("courses", courseRepository.findAll()); model.addAttribute("courses", courseRepository.findAll());
return "courses"; return "courses";
} }
/**
* Handles POST requests to this collection of courses. This facilitates the creation of new courses.
* @param course The course which has been submitted by a user.
* @return The entity view for the course which has just been created.
*/
@PostMapping( @PostMapping(
value = "/courses", value = "/courses",
consumes = "application/json" consumes = "application/x-www-form-urlencoded"
) )
public String post(@ModelAttribute Course course) { public String post(@ModelAttribute Course course) {
System.out.println("Object submitted: " + course); System.out.println("Object submitted: " + course);
this.courseRepository.save(course); this.courseRepository.save(course);
return "/courses/created"; return "courses/entity";
} }
} }

View File

@ -0,0 +1,24 @@
package nl.andrewlalis.teaching_assistant_assistant.controllers.courses;
import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Create {
private CourseRepository courseRepository;
protected Create(CourseRepository courseRepository) {
this.courseRepository = courseRepository;
}
@GetMapping("/courses/create")
public String get(Model model) {
Course course = new Course("no name", "no code");
model.addAttribute("course", course);
return "courses/create";
}
}

View File

@ -9,6 +9,9 @@ import org.springframework.web.bind.annotation.PathVariable;
import java.util.Optional; import java.util.Optional;
/**
* Controller for the course entity, that is, one individual course.
*/
@Controller @Controller
public class Entity { public class Entity {
@ -18,9 +21,16 @@ public class Entity {
this.courseRepository = courseRepository; this.courseRepository = courseRepository;
} }
@GetMapping("/courses/{id}") /**
public String get(@PathVariable Long id, Model model) { * Handles get requests to a course with a given code.
Optional<Course> courseOptional = this.courseRepository.findById(id); * @param code The unique course code used to identify a course entity.
* @param model The view model that will be populated with data.
* @return The template which will be used in conjunction with the model to build a view.
*/
@GetMapping("/courses/{code}")
public String get(@PathVariable String code, Model model) {
Optional<Course> courseOptional = this.courseRepository.findByCode(code);
if (courseOptional.isPresent()) { if (courseOptional.isPresent()) {
Course course = courseOptional.get(); Course course = courseOptional.get();
model.addAttribute("course", course); model.addAttribute("course", course);

View File

@ -0,0 +1,32 @@
package nl.andrewlalis.teaching_assistant_assistant.controllers.courses;
import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository;
import nl.andrewlalis.teaching_assistant_assistant.util.sample_data.CourseGenerator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Controller
public class Generate {
private CourseRepository courseRepository;
protected Generate(CourseRepository courseRepository) {
this.courseRepository = courseRepository;
}
@GetMapping("/courses/generate")
public String get(Model model) {
CourseGenerator courseGenerator = new CourseGenerator(0, 3, 2, 10, 3);
List<Course> courses = courseGenerator.generateList(5);
this.courseRepository.saveAll(courses);
model.addAttribute("courses", courseRepository.findAll());
return "courses";
}
}

View File

@ -4,9 +4,18 @@ import nl.andrewlalis.teaching_assistant_assistant.model.Course;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
/** /**
* The repository used to interact with various Course objects. * The repository used to interact with various Course objects.
*/ */
@Repository @Repository
public interface CourseRepository extends CrudRepository<Course, Long> { public interface CourseRepository extends CrudRepository<Course, Long> {
/**
* Finds a course by its unique code.
* @param code A unique code which identifies a course.
* @return An optional object that may contain the course identified by the given code.
*/
Optional<Course> findByCode(String code);
} }

View File

@ -14,7 +14,7 @@
</tr> </tr>
<tr th:each="course: ${courses}"> <tr th:each="course: ${courses}">
<td> <td>
<a th:href="@{/courses/{id}(id=${course.getId()})}" th:text="${course.getName()}"></a> <a th:href="@{/courses/{code}(code=${course.getCode()})}" th:text="${course.getName()}"></a>
</td> </td>
<td th:text="${course.getCode()}"></td> <td th:text="${course.getCode()}"></td>
<td th:text="${course.getCreatedOn()}"></td> <td th:text="${course.getCreatedOn()}"></td>
@ -24,7 +24,7 @@
<div id="sidebar"> <div id="sidebar">
<div class="sidebar_block"> <div class="sidebar_block">
Create new course <a href="/courses/create">Create new course</a>
</div> </div>
<div class="sidebar_block"> <div class="sidebar_block">
Do something else Do something else

View File

@ -1,10 +1,28 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head> <head>
<meta charset="UTF-8"> <title>Create a Course</title>
<title>Title</title>
</head> </head>
<body> <body>
<div id="content">
<p>
Create your course here
</p>
<form action="#" th:action="@{/courses}" th:object="${course}" method="post">
<label for="course_name_input">Name:</label>
<input id="course_name_input" type="text" th:field="*{name}"/>
<label for="course_code_input">Code:</label>
<input id="course_code_input" type="text" th:field="*{code}"/>
<button type="submit">Submit</button>
</form>
</div>
<div id="sidebar">
<div class="sidebar_block">
block
</div>
</div>
</body> </body>
</html> </html>

View File

@ -15,8 +15,8 @@
<div class="sidebar_block"> <div class="sidebar_block">
This is an example sidebar block. This is an example sidebar block.
</div> </div>
<div class="sidebar_block"> <div id="generate_courses_block" class="sidebar_block">
Click here to generate courses. <a href="/courses/generate">Click here to generate courses.</a>
</div> </div>
</div> </div>