From 11fb7bcc1dce811783c09597e887f1cb1a7c6dbc Mon Sep 17 00:00:00 2001
From: Andrew Lalis
Date: Mon, 22 Apr 2019 03:04:12 +0200
Subject: [PATCH] Added ability to edit student information.
---
.../controllers/students/StudentEntity.java | 27 ++++++++++
.../model/people/Person.java | 16 ++++++
src/main/resources/static/css/style.css | 4 ++
.../resources/templates/students/entity.html | 4 +-
.../templates/students/entity/edit.html | 54 +++++++++++++++++++
5 files changed, 104 insertions(+), 1 deletion(-)
create mode 100644 src/main/resources/templates/students/entity/edit.html
diff --git a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java
index db914de..b0768c9 100644
--- a/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java
+++ b/src/main/java/nl/andrewlalis/teaching_assistant_assistant/controllers/students/StudentEntity.java
@@ -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 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 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}";
+ }
}
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 70245af..c4fea05 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
@@ -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 getCourses() {
return this.courses;
}
diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css
index e1d6b8e..a0338e8 100644
--- a/src/main/resources/static/css/style.css
+++ b/src/main/resources/static/css/style.css
@@ -39,4 +39,8 @@ body {
table, th, td {
border: 1px solid black;
border-collapse: collapse;
+}
+
+.page_row {
+ width: 100%;
}
\ No newline at end of file
diff --git a/src/main/resources/templates/students/entity.html b/src/main/resources/templates/students/entity.html
index 4576c60..a21baeb 100644
--- a/src/main/resources/templates/students/entity.html
+++ b/src/main/resources/templates/students/entity.html
@@ -30,7 +30,9 @@
+
+
+
+
+
+
diff --git a/src/main/resources/templates/students/entity/edit.html b/src/main/resources/templates/students/entity/edit.html
new file mode 100644
index 0000000..8dd4cff
--- /dev/null
+++ b/src/main/resources/templates/students/entity/edit.html
@@ -0,0 +1,54 @@
+
+
+