From a629b70d8bd2af3427d76ab174a7fd617df25149 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 16 Apr 2019 22:30:50 +0200 Subject: [PATCH] Improved comment and added button for generation. --- ...TeachingAssistantAssistantApplication.java | 12 ------- .../controllers/Courses.java | 14 ++++++-- .../controllers/courses/Create.java | 24 ++++++++++++++ .../controllers/courses/Entity.java | 16 ++++++++-- .../controllers/courses/Generate.java | 32 +++++++++++++++++++ .../model/repositories/CourseRepository.java | 9 ++++++ src/main/resources/templates/courses.html | 4 +-- .../resources/templates/courses/create.html | 24 ++++++++++++-- src/main/resources/templates/index.html | 4 +-- 9 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Create.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Generate.java diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java index 2a105f5..984e6f9 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/TeachingAssistantAssistantApplication.java @@ -1,17 +1,13 @@ 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.PersonRepository; 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.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import java.util.List; - @SpringBootApplication public class TeachingAssistantAssistantApplication implements CommandLineRunner { @@ -32,13 +28,5 @@ public class TeachingAssistantAssistantApplication implements CommandLineRunner public void run(String... args) throws Exception { System.out.println("Running startup..."); - // Generate some example courses. - CourseGenerator courseGenerator = new CourseGenerator(0, 3, 2, 10, 3); - - List courses = courseGenerator.generateList(100); - this.courseRepository.saveAll(courses); - - System.out.println("Course count: " + courseRepository.count()); - } } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java index 404fa5b..6935735 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/Courses.java @@ -20,20 +20,30 @@ public class Courses { 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") public String get(Model model) { model.addAttribute("courses", courseRepository.findAll()); 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( value = "/courses", - consumes = "application/json" + consumes = "application/x-www-form-urlencoded" ) public String post(@ModelAttribute Course course) { System.out.println("Object submitted: " + course); this.courseRepository.save(course); - return "/courses/created"; + return "courses/entity"; } } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Create.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Create.java new file mode 100644 index 0000000..08376e2 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Create.java @@ -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"; + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Entity.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Entity.java index 87ecd34..8bc8d33 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Entity.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Entity.java @@ -9,6 +9,9 @@ import org.springframework.web.bind.annotation.PathVariable; import java.util.Optional; +/** + * Controller for the course entity, that is, one individual course. + */ @Controller public class Entity { @@ -18,9 +21,16 @@ public class Entity { this.courseRepository = courseRepository; } - @GetMapping("/courses/{id}") - public String get(@PathVariable Long id, Model model) { - Optional courseOptional = this.courseRepository.findById(id); + /** + * Handles get requests to a course with a given code. + * @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 courseOptional = this.courseRepository.findByCode(code); + if (courseOptional.isPresent()) { Course course = courseOptional.get(); model.addAttribute("course", course); diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Generate.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Generate.java new file mode 100644 index 0000000..c7f8ae5 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Generate.java @@ -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 courses = courseGenerator.generateList(5); + this.courseRepository.saveAll(courses); + + model.addAttribute("courses", courseRepository.findAll()); + return "courses"; + } + +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/CourseRepository.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/CourseRepository.java index 30b8a03..36c61ef 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/CourseRepository.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/CourseRepository.java @@ -4,9 +4,18 @@ import nl.andrewlalis.teaching_assistant_assistant.model.Course; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + /** * The repository used to interact with various Course objects. */ @Repository public interface CourseRepository extends CrudRepository { + + /** + * 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 findByCode(String code); } diff --git a/src/main/resources/templates/courses.html b/src/main/resources/templates/courses.html index f5df6be..d6f8562 100644 --- a/src/main/resources/templates/courses.html +++ b/src/main/resources/templates/courses.html @@ -14,7 +14,7 @@ - + @@ -24,7 +24,7 @@