From 8aeb445a7ceb2d1bf98587280ae45f03ff9dd281 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Sat, 20 Apr 2019 23:32:32 +0200 Subject: [PATCH] Added team manipulation --- .../controllers/TeachingAssistants.java | 53 +++++++++++++ .../{Create.java => CreateCourse.java} | 4 +- .../controllers/courses/Generate.java | 2 +- .../CreateTeachingAssistantTeam.java | 75 +++++++++++++++++++ .../TeachingAssistantTeamEntity.java | 55 ++++++++++++++ .../TeachingAssistantEntity.java | 41 ++++++++++ .../model/Course.java | 8 ++ .../model/people/Person.java | 4 + .../TeachingAssistantRepository.java | 7 ++ .../TeachingAssistantTeamRepository.java | 7 ++ .../util/github/GithubManager.java | 3 + .../entity/teaching_assistant_teams.html | 10 ++- .../teaching_assistant_teams/create.html | 37 +++++++++ .../teaching_assistant_teams/entity.html | 33 ++++++++ .../courses/entity/teaching_assistants.html | 5 +- .../entity/teaching_assistants/create.html | 36 +++++++++ .../resources/templates/fragments/header.html | 1 + .../templates/teaching_assistants.html | 32 ++++++++ .../templates/teaching_assistants/entity.html | 34 +++++++++ 19 files changed, 442 insertions(+), 5 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java rename src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/{Create.java => CreateCourse.java} (88%) create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/CreateTeachingAssistantTeam.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/TeachingAssistantTeamEntity.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantRepository.java create mode 100644 src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantTeamRepository.java create mode 100644 src/main/resources/templates/courses/entity/teaching_assistant_teams/create.html create mode 100644 src/main/resources/templates/courses/entity/teaching_assistant_teams/entity.html create mode 100644 src/main/resources/templates/courses/entity/teaching_assistants/create.html create mode 100644 src/main/resources/templates/teaching_assistants.html create mode 100644 src/main/resources/templates/teaching_assistants/entity.html diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java new file mode 100644 index 0000000..821ad41 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/TeachingAssistants.java @@ -0,0 +1,53 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers; + +import nl.andrewlalis.teaching_assistant_assistant.model.Course; +import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistant; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeachingAssistantRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.Optional; + +@Controller +public class TeachingAssistants { + + private TeachingAssistantRepository teachingAssistantRepository; + private CourseRepository courseRepository; + + protected TeachingAssistants(TeachingAssistantRepository teachingAssistantRepository, CourseRepository courseRepository) { + this.teachingAssistantRepository = teachingAssistantRepository; + this.courseRepository = courseRepository; + } + + @GetMapping("/teaching_assistants") + public String get(Model model) { + model.addAttribute("teaching_assistants", teachingAssistantRepository.findAll()); + return "teaching_assistants"; + } + + @GetMapping("/courses/{code}/teaching_assistants/create") + public String getCreate(@PathVariable String code, Model model) { + model.addAttribute("teachingAssistant", new TeachingAssistant("First Name", "Last Name", "github Username", "me@example.com")); + Optional optionalCourse = this.courseRepository.findByCode(code); + optionalCourse.ifPresent(course -> model.addAttribute("course", course)); + return "courses/entity/teaching_assistants/create"; + } + + @PostMapping( + value = "/courses/{code}/teaching_assistants", + consumes = "application/x-www-form-urlencoded" + ) + public String post(@PathVariable String code, @ModelAttribute("teachingAssistant") TeachingAssistant teachingAssistant) { + Optional optionalCourse = this.courseRepository.findByCode(code); + optionalCourse.ifPresent(course -> { + course.addParticipant(teachingAssistant); + this.courseRepository.save(course); + }); + return "teaching_assistants/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/CreateCourse.java similarity index 88% rename from src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Create.java rename to src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/CreateCourse.java index 08376e2..7d8cd0d 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/Create.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/CreateCourse.java @@ -7,11 +7,11 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller -public class Create { +public class CreateCourse { private CourseRepository courseRepository; - protected Create(CourseRepository courseRepository) { + protected CreateCourse(CourseRepository courseRepository) { this.courseRepository = courseRepository; } 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 index c7f8ae5..2d5227b 100644 --- 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 @@ -26,7 +26,7 @@ public class Generate { this.courseRepository.saveAll(courses); model.addAttribute("courses", courseRepository.findAll()); - return "courses"; + return "redirect:/courses"; } } diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/CreateTeachingAssistantTeam.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/CreateTeachingAssistantTeam.java new file mode 100644 index 0000000..542528d --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/CreateTeachingAssistantTeam.java @@ -0,0 +1,75 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers.courses.entity.teaching_assistant_teams; + +import nl.andrewlalis.teaching_assistant_assistant.model.Course; +import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistant; +import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.TeachingAssistantTeam; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeachingAssistantRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.Optional; + +@Controller +public class CreateTeachingAssistantTeam { + + private CourseRepository courseRepository; + private TeachingAssistantRepository teachingAssistantRepository; + + protected CreateTeachingAssistantTeam(CourseRepository courseRepository, TeachingAssistantRepository teachingAssistantRepository) { + this.courseRepository = courseRepository; + this.teachingAssistantRepository = teachingAssistantRepository; + } + + @GetMapping("/courses/{code}/teaching_assistant_teams/create") + public String get(@PathVariable String code, Model model) { + Optional optionalCourse = this.courseRepository.findByCode(code); + optionalCourse.ifPresent(course -> model.addAttribute("course", course)); + return "courses/entity/teaching_assistant_teams/create"; + } + + @PostMapping( + value = "/courses/{code}/teaching_assistant_teams", + consumes = "application/x-www-form-urlencoded" + ) + public String post( + @PathVariable String code, + @RequestParam(value = "github_team_name") String githubTeamName, + @RequestParam(value = "id_1") long id1, + @RequestParam(value = "id_2") long id2, + Model model + ) { + TeachingAssistantTeam team = new TeachingAssistantTeam(); + team.setGithubTeamName(githubTeamName); + + Optional optionalCourse = this.courseRepository.findByCode(code); + Optional optionalTeachingAssistant1 = this.teachingAssistantRepository.findById(id1); + Optional optionalTeachingAssistant2 = this.teachingAssistantRepository.findById(id2); + + System.out.println("Course code: " + code + ", Team name: " + githubTeamName + ", TA 1: " + id1 + ", TA 2: " + id2); + + if (optionalCourse.isPresent() && optionalTeachingAssistant1.isPresent() && optionalTeachingAssistant2.isPresent()) { + System.out.println("All data available."); + Course course = optionalCourse.get(); + team.setCourse(course); + + team.addMember(optionalTeachingAssistant1.get()); + team.addMember(optionalTeachingAssistant2.get()); + + course.addTeachingAssistantTeam(team); + this.courseRepository.save(course); + + model.addAttribute("course", course); + + return "courses/entity/teaching_assistant_teams"; + } else { + System.out.println("Missing data!"); + } + + return "redirect:/courses/entity"; + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/TeachingAssistantTeamEntity.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/TeachingAssistantTeamEntity.java new file mode 100644 index 0000000..5923980 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/courses/entity/teaching_assistant_teams/TeachingAssistantTeamEntity.java @@ -0,0 +1,55 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers.courses.entity.teaching_assistant_teams; + +import nl.andrewlalis.teaching_assistant_assistant.model.Course; +import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.TeachingAssistantTeam; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.CourseRepository; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeachingAssistantTeamRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +import java.util.Optional; + +@Controller +public class TeachingAssistantTeamEntity { + + private CourseRepository courseRepository; + private TeachingAssistantTeamRepository teachingAssistantTeamRepository; + + protected TeachingAssistantTeamEntity(CourseRepository courseRepository, TeachingAssistantTeamRepository teachingAssistantTeamRepository) { + this.courseRepository = courseRepository; + this.teachingAssistantTeamRepository = teachingAssistantTeamRepository; + } + + @GetMapping("/courses/{courseCode}/teaching_assistant_teams/{teamId}") + public String get(@PathVariable String courseCode, @PathVariable long teamId, Model model) { + Optional optionalCourse = this.courseRepository.findByCode(courseCode); + Optional optionalTeachingAssistantTeam = this.teachingAssistantTeamRepository.findById(teamId); + + if (optionalCourse.isPresent() && optionalTeachingAssistantTeam.isPresent()) { + model.addAttribute("course", optionalCourse.get()); + model.addAttribute("teachingAssistantTeam", optionalTeachingAssistantTeam.get()); + } + + return "courses/entity/teaching_assistant_teams/entity"; + } + + @GetMapping("/courses/{courseCode}/teaching_assistant_teams/{teamId}/delete") + public String delete(@PathVariable String courseCode, @PathVariable long teamId) { + Optional optionalCourse = this.courseRepository.findByCode(courseCode); + Optional optionalTeachingAssistantTeam = this.teachingAssistantTeamRepository.findById(teamId); + + if (optionalCourse.isPresent() && optionalTeachingAssistantTeam.isPresent()) { + Course course = optionalCourse.get(); + TeachingAssistantTeam team = optionalTeachingAssistantTeam.get(); + course.removeTeachingAssistantTeam(team); + + this.teachingAssistantTeamRepository.delete(team); + this.courseRepository.save(course); + } + + return "redirect:/courses/entity/teaching_assistants"; + } + +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java new file mode 100644 index 0000000..99991b3 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/teaching_assistants/TeachingAssistantEntity.java @@ -0,0 +1,41 @@ +package nl.andrewlalis.teaching_assistant_assistant.controllers.teaching_assistants; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistant; +import nl.andrewlalis.teaching_assistant_assistant.model.repositories.TeachingAssistantRepository; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +import java.util.Optional; + +@Controller +public class TeachingAssistantEntity { + + private TeachingAssistantRepository teachingAssistantRepository; + + protected TeachingAssistantEntity(TeachingAssistantRepository teachingAssistantRepository) { + this.teachingAssistantRepository = teachingAssistantRepository; + } + + @GetMapping("/teaching_assistants/{id}") + public String get(@PathVariable long id, Model model) { + Optional optionalTeachingAssistant = this.teachingAssistantRepository.findById(id); + optionalTeachingAssistant.ifPresent(teachingAssistant -> model.addAttribute("teachingAssistant", teachingAssistant)); + return "teaching_assistants/entity"; + } + + @GetMapping("/teaching_assistants/{id}/delete") + public String delete(@PathVariable long id) { + Optional optionalTeachingAssistant = this.teachingAssistantRepository.findById(id); + optionalTeachingAssistant.ifPresent(teachingAssistant -> { + teachingAssistant.getCourses().forEach(course -> course.removeParticipant(teachingAssistant)); + teachingAssistant.getTeams().forEach(team -> { + team.removeMember(teachingAssistant); + }); + this.teachingAssistantRepository.delete(teachingAssistant); + }); + + return "redirect:/teaching_assistants"; + } +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java index 4e65534..97a62f3 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/Course.java @@ -108,12 +108,20 @@ public class Course extends BasicEntity { this.teachingAssistantTeams.add(team); } + public void removeTeachingAssistantTeam(TeachingAssistantTeam team) { + this.teachingAssistantTeams.remove(team); + } + public void addParticipant(Person person) { if (!this.participants.contains(person)) { this.participants.add(person); } } + public void removeParticipant(Person person) { + this.participants.remove(person); + } + /* Getters and Setters */ diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java index 7909a7f..70245af 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/people/Person.java @@ -114,6 +114,10 @@ public abstract class Person extends BasicEntity { return this.courses; } + public List getTeams() { + return this.teams; + } + /** * Determines if two Persons are equal. They are considered equal when all of the basic identifying information * about the person is the same, regardless of case. diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantRepository.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantRepository.java new file mode 100644 index 0000000..63154c9 --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantRepository.java @@ -0,0 +1,7 @@ +package nl.andrewlalis.teaching_assistant_assistant.model.repositories; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.TeachingAssistant; +import org.springframework.data.repository.CrudRepository; + +public interface TeachingAssistantRepository extends CrudRepository { +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantTeamRepository.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantTeamRepository.java new file mode 100644 index 0000000..a06552b --- /dev/null +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/model/repositories/TeachingAssistantTeamRepository.java @@ -0,0 +1,7 @@ +package nl.andrewlalis.teaching_assistant_assistant.model.repositories; + +import nl.andrewlalis.teaching_assistant_assistant.model.people.teams.TeachingAssistantTeam; +import org.springframework.data.repository.CrudRepository; + +public interface TeachingAssistantTeamRepository extends CrudRepository { +} diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/util/github/GithubManager.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/util/github/GithubManager.java index 5925a87..c314d8f 100644 --- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/util/github/GithubManager.java +++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/util/github/GithubManager.java @@ -10,6 +10,9 @@ import java.util.ArrayList; import java.util.Calendar; import java.util.List; +/** + * Encapsulates much of the github functionality that is needed. + */ public class GithubManager { private GitHub github; diff --git a/src/main/resources/templates/courses/entity/teaching_assistant_teams.html b/src/main/resources/templates/courses/entity/teaching_assistant_teams.html index aefe237..b1eb8b7 100644 --- a/src/main/resources/templates/courses/entity/teaching_assistant_teams.html +++ b/src/main/resources/templates/courses/entity/teaching_assistant_teams.html @@ -25,12 +25,20 @@ + + + Delete + + diff --git a/src/main/resources/templates/courses/entity/teaching_assistant_teams/create.html b/src/main/resources/templates/courses/entity/teaching_assistant_teams/create.html new file mode 100644 index 0000000..b37e0d9 --- /dev/null +++ b/src/main/resources/templates/courses/entity/teaching_assistant_teams/create.html @@ -0,0 +1,37 @@ + + + + Create Teaching Assistant Team + + + +
+

+ Create your course here +

+
+ + + + + + + + + + +
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/courses/entity/teaching_assistant_teams/entity.html b/src/main/resources/templates/courses/entity/teaching_assistant_teams/entity.html new file mode 100644 index 0000000..24c3041 --- /dev/null +++ b/src/main/resources/templates/courses/entity/teaching_assistant_teams/entity.html @@ -0,0 +1,33 @@ + + + + Teaching Assistant Team + + + +
+

Teaching Assistant Team for

+ +
    +
  • + Github Team Name: +
  • +
  • + Teaching Assistants: + +
  • +
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/courses/entity/teaching_assistants.html b/src/main/resources/templates/courses/entity/teaching_assistants.html index 9ca9361..12f75f0 100644 --- a/src/main/resources/templates/courses/entity/teaching_assistants.html +++ b/src/main/resources/templates/courses/entity/teaching_assistants.html @@ -14,12 +14,15 @@ + diff --git a/src/main/resources/templates/courses/entity/teaching_assistants/create.html b/src/main/resources/templates/courses/entity/teaching_assistants/create.html new file mode 100644 index 0000000..88de95a --- /dev/null +++ b/src/main/resources/templates/courses/entity/teaching_assistants/create.html @@ -0,0 +1,36 @@ + + + + Create a Course + + + +
+

+ Create your course here +

+
+ + + + + + + + + + + + + +
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/fragments/header.html b/src/main/resources/templates/fragments/header.html index 1e60e1d..5b1b3f6 100644 --- a/src/main/resources/templates/fragments/header.html +++ b/src/main/resources/templates/fragments/header.html @@ -14,6 +14,7 @@
  • Home
  • Courses
  • Students
  • +
  • Teaching Assistants
  • diff --git a/src/main/resources/templates/teaching_assistants.html b/src/main/resources/templates/teaching_assistants.html new file mode 100644 index 0000000..1084796 --- /dev/null +++ b/src/main/resources/templates/teaching_assistants.html @@ -0,0 +1,32 @@ + + + + + Teaching Assistants + + + +
    +

    All Teaching Assistants

    + + + + + + + + + +
    + + + Delete +
    +
    + + + + + \ No newline at end of file diff --git a/src/main/resources/templates/teaching_assistants/entity.html b/src/main/resources/templates/teaching_assistants/entity.html new file mode 100644 index 0000000..d9c061b --- /dev/null +++ b/src/main/resources/templates/teaching_assistants/entity.html @@ -0,0 +1,34 @@ + + + + Teaching Assistant + + + +
    +

    Teaching Assistant:

    + +
      +
    • + Email: +
    • +
    • + Github Username: +
    • +
    • + Courses: +
        +
      • + +
      • +
      +
    • +
    +
    + + + + + \ No newline at end of file