Added ability to edit student information.

This commit is contained in:
Andrew Lalis 2019-04-22 03:04:12 +02:00 committed by andrewlalis
parent 9c6dec0473
commit 11fb7bcc1d
5 changed files with 104 additions and 1 deletions

View File

@ -5,7 +5,9 @@ import nl.andrewlalis.teaching_assistant_assistant.model.repositories.StudentRep
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;
@ -24,4 +26,29 @@ public class StudentEntity {
optionalStudent.ifPresent(student -> model.addAttribute("student", student));
return "students/entity";
}
@GetMapping("/students/{id}/edit")
public String getEdit(@PathVariable long id, Model model) {
Optional<Student> optionalStudent = this.studentRepository.findById(id);
optionalStudent.ifPresent(student -> model.addAttribute("student", student));
return "students/entity/edit";
}
@PostMapping(
value = "/students/{id}/edit",
consumes = "application/x-www-form-urlencoded"
)
public String post(@ModelAttribute Student editedStudent, @PathVariable long id) {
Optional<Student> optionalStudent = this.studentRepository.findById(id);
optionalStudent.ifPresent(student -> {
student.setFirstName(editedStudent.getFirstName());
student.setLastName(editedStudent.getLastName());
student.setEmailAddress(editedStudent.getEmailAddress());
student.setGithubUsername(editedStudent.getGithubUsername());
student.setStudentNumber(editedStudent.getStudentNumber());
this.studentRepository.save(student);
});
return "redirect:/students/{id}";
}
}

View File

@ -94,10 +94,18 @@ public abstract class Person extends BasicEntity {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFullName() {
return this.getFirstName() + ' ' + this.getLastName();
}
@ -106,10 +114,18 @@ public abstract class Person extends BasicEntity {
return this.emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getGithubUsername() {
return this.githubUsername;
}
public void setGithubUsername(String githubUsername) {
this.githubUsername = githubUsername;
}
public List<Course> getCourses() {
return this.courses;
}

View File

@ -39,4 +39,8 @@ body {
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
.page_row {
width: 100%;
}

View File

@ -30,7 +30,9 @@
</div>
<div id="sidebar">
<div class="sidebar_block">
<a th:href="@{/students/{id}/edit(id=${student.getId()})}">Edit</a>
</div>
</div>
</body>

View File

@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="~{layouts/basic_page :: layout (~{::title}, ~{::#content}, ~{::#sidebar})}">
<head>
<title>Edit Student</title>
</head>
<body>
<div id="content">
<h1>Edit Student: <span th:text="${student.getFullName()}"></span></h1>
<form
action="#"
th:action="@{/students/{id}/edit(id=${student.getId()})}" th:object="${student}"
method="post"
>
<div class="page_row">
<label for="first_name_input">First Name:</label>
<input id="first_name_input" type="text" th:field="*{firstName}" required />
</div>
<div class="page_row">
<label for="last_name_input">Last Name:</label>
<input id="last_name_input" type="text" th:field="*{lastName}" required />
</div>
<div class="page_row">
<label for="email_input">Email Address:</label>
<input id="email_input" type="email" th:field="*{emailAddress}" required />
</div>
<div class="page_row">
<label for="github_username_input">Github Username:</label>
<input id="github_username_input" type="text" th:field="*{githubUsername}" required />
</div>
<div class="page_row">
<label for="student_number_input">Student Number:</label>
<input id="student_number_input" type="number" th:field="*{studentNumber}" required />
</div>
<div class="page_row">
<button type="submit">Submit</button>
</div>
</form>
</div>
<div id="sidebar">
</div>
</body>
</html>