Added team manipulation

This commit is contained in:
Andrew Lalis 2019-04-20 23:32:32 +02:00 committed by andrewlalis
parent f1add7dc84
commit 8aeb445a7c
19 changed files with 442 additions and 5 deletions

View File

@ -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<Course> 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<Course> optionalCourse = this.courseRepository.findByCode(code);
optionalCourse.ifPresent(course -> {
course.addParticipant(teachingAssistant);
this.courseRepository.save(course);
});
return "teaching_assistants/entity";
}
}

View File

@ -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;
}

View File

@ -26,7 +26,7 @@ public class Generate {
this.courseRepository.saveAll(courses);
model.addAttribute("courses", courseRepository.findAll());
return "courses";
return "redirect:/courses";
}
}

View File

@ -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<Course> 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<Course> optionalCourse = this.courseRepository.findByCode(code);
Optional<TeachingAssistant> optionalTeachingAssistant1 = this.teachingAssistantRepository.findById(id1);
Optional<TeachingAssistant> 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";
}
}

View File

@ -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<Course> optionalCourse = this.courseRepository.findByCode(courseCode);
Optional<TeachingAssistantTeam> 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<Course> optionalCourse = this.courseRepository.findByCode(courseCode);
Optional<TeachingAssistantTeam> 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";
}
}

View File

@ -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<TeachingAssistant> 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<TeachingAssistant> 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";
}
}

View File

@ -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
*/

View File

@ -114,6 +114,10 @@ public abstract class Person extends BasicEntity {
return this.courses;
}
public List<Team> 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.

View File

@ -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<TeachingAssistant, Long> {
}

View File

@ -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<TeachingAssistantTeam, Long> {
}

View File

@ -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;

View File

@ -25,12 +25,20 @@
<span th:text="${ta.getFullName()}"></span>
</td>
<td th:text="${taTeam.getGithubTeamName()}"></td>
<td>
<a th:href="@{/courses/{code}/teaching_assistant_teams/{team_id}/delete
(code=${course.getCode()}, team_id=${taTeam.getId()})}">
Delete
</a>
</td>
</tr>
</table>
</div>
<div id="sidebar">
<div class="sidebar_block">
<a th:href="@{/courses/{code}/teaching_assistant_teams/create(code=${course.getCode()})}">Create Teaching Assistant Team</a>
</div>
</div>
</body>

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head>
<title>Create Teaching Assistant Team</title>
</head>
<body>
<div id="content">
<p>
Create your course here
</p>
<form action="#" th:action="@{/courses/{code}/teaching_assistant_teams(code=${course.getCode()})}" enctype="application/x-www-form-urlencoded" method="post">
<label for="ta_team_github_team_name">Github team name:</label>
<input id="ta_team_github_team_name" type="text" name="github_team_name"/>
<label for="ta_team_member_1">Select the first Team Member:</label>
<select id="ta_team_member_1" name="id_1">
<option th:each="ta: ${course.getTeachingAssistants()}" th:value="${ta.getId()}" th:text="${ta.getFullName()}"></option>
</select>
<label for="ta_team_member_2">Select the first Team Member:</label>
<select id="ta_team_member_2" name="id_2">
<option th:each="ta: ${course.getTeachingAssistants()}" th:value="${ta.getId()}" th:text="${ta.getFullName()}"></option>
</select>
<button type="submit">Submit</button>
</form>
</div>
<div id="sidebar">
<div class="sidebar_block">
block
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,33 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head>
<title>Teaching Assistant Team</title>
</head>
<body>
<div id="content">
<h1>Teaching Assistant Team <span th:text="${teachingAssistantTeam.getId()}"></span> for <span th:text="${course.getName()}"></span></h1>
<ul>
<li>
Github Team Name: <code th:text="${teachingAssistantTeam.getGithubTeamName()}"></code>
</li>
<li>
Teaching Assistants:
<ul>
<li th:each="teachingAssistant: ${teachingAssistantTeam.getTeachingAssistants()}">
<a th:href="@{/teaching_assistants/{id}(id=${teachingAssistant.getId()})}">
<span th:text="${teachingAssistant.getFullName()}"></span>
</a>
</li>
</ul>
</li>
</ul>
</div>
<div id="sidebar">
</div>
</body>
</html>

View File

@ -14,12 +14,15 @@
<a th:href="@{/teaching_assistants/{id}(id=${teachingAssistant.getId()})}"><span th:text="${teachingAssistant.getFullName()}"></span></a>
</td>
<td th:text="${teachingAssistant.getEmailAddress()}"></td>
<td th:text="${teachingAssistant.getId()}"></td>
</tr>
</table>
</div>
<div id="sidebar">
<div class="sidebar_block">
<a th:href="@{/courses/{code}/teaching_assistants/create(code=${course.getCode()})}">Add Teaching Assistant</a>
</div>
</div>
</body>

View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head>
<title>Create a Course</title>
</head>
<body>
<div id="content">
<p>
Create your course here
</p>
<form action="#" th:action="@{/courses/{code}/teaching_assistants(code=${course.getCode()})}" th:object="${teachingAssistant}" method="post">
<label for="ta_first_name_input">First Name:</label>
<input id="ta_first_name_input" type="text" th:field="*{firstName}"/>
<label for="ta_last_name_input">Last Name:</label>
<input id="ta_last_name_input" type="text" th:field="*{lastName}"/>
<label for="ta_github_username_input">Github Username:</label>
<input id="ta_github_username_input" type="text" th:field="*{githubUsername}"/>
<label for="ta_email_input">Email Address:</label>
<input id="ta_email_input" type="email" th:field="*{emailAddress}"/>
<button type="submit">Submit</button>
</form>
</div>
<div id="sidebar">
<div class="sidebar_block">
block
</div>
</div>
</body>
</html>

View File

@ -14,6 +14,7 @@
<li><a href="/" th:href="@{/}">Home</a>
<li><a href="/courses" th:href="@{/courses}">Courses</a>
<li><a href="/students" th:href="@{/students}">Students</a></li>
<li><a href="/teaching_assistants" th:href="@{/teaching_assistants}">Teaching Assistants</a></li>
</ul>
</nav>

View File

@ -0,0 +1,32 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head>
<meta charset="UTF-8">
<title>Teaching Assistants</title>
</head>
<body>
<div id="content">
<h1>All Teaching Assistants</h1>
<table>
<tr th:each="teachingAssistant: ${teaching_assistants}">
<td>
<a th:href="@{/teaching_assistants/{id}(id=${teachingAssistant.getId()})}"><span th:text="${teachingAssistant.getFullName()}"></span></a>
</td>
<td th:text="${teachingAssistant.getGithubUsername()}"></td>
<td th:text="${teachingAssistant.getEmailAddress()}"></td>
<td th:text="${teachingAssistant.getId()}"></td>
<td>
<a th:href="@{/teaching_assistants/{id}/delete(id=${teachingAssistant.getId()})}">Delete</a>
</td>
</tr>
</table>
</div>
<div id="sidebar">
</div>
</body>
</html>

View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head>
<title>Teaching Assistant</title>
</head>
<body>
<div id="content">
<h1>Teaching Assistant: <span th:text="${teachingAssistant.getFullName()}"></span></h1>
<ul>
<li>
Email: <code th:text="${teachingAssistant.getEmailAddress()}"></code>
</li>
<li>
Github Username: <code th:text="${teachingAssistant.getGithubUsername()}"></code>
</li>
<li>
Courses:
<ul>
<li th:each="course: ${teachingAssistant.getCourses()}">
<a th:href="@{/courses/{code}(code=${course.getCode()})}"><span th:text="${course.getName()}"></span></a>
</li>
</ul>
</li>
</ul>
</div>
<div id="sidebar">
</div>
</body>
</html>