Skip to content

Commit a4e83e4

Browse files
committed
Enable a post to be edited
- Add post/edit template - Add logic responsible for updating a post to the controller - Add a link to edit page to each post displayed in index page
1 parent ae3fa74 commit a4e83e4

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

myapp/src/main/java/com/example/myapp/BlogController.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Controller;
55
import org.springframework.ui.Model;
6-
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.bind.annotation.PostMapping;
8-
import org.springframework.web.bind.annotation.PatchMapping;
9-
import org.springframework.web.bind.annotation.DeleteMapping;
6+
import org.springframework.web.bind.annotation.*;
107

118
import java.util.List;
129

@@ -40,14 +37,27 @@ public String showPost() {
4037
}
4138

4239
@GetMapping("/posts/{postId}/edit")
43-
public String editPost() {
40+
public String editPost(@PathVariable("postId") long id, Model model) {
41+
Post post = postRepository.findById(id)
42+
.orElseThrow(() -> new IllegalArgumentException("Invalid Post Id:" + id));
43+
44+
model.addAttribute("post", post);
45+
4446
return "blog/edit";
4547
}
4648

47-
@PatchMapping("/posts/{postId}")
48-
public String updatePost() {
49-
//TODO: logic responsible for updating a post
50-
return null;
49+
@PostMapping("/posts/{postId}")
50+
public String updatePost(@PathVariable("postId") long id, Model model, Post post) {
51+
Post recordedPost = postRepository.findById(id)
52+
.orElseThrow(() -> new IllegalArgumentException("Invalid Post Id:" + id));
53+
54+
recordedPost.setTitle(post.getTitle());
55+
recordedPost.setContent(post.getContent());
56+
postRepository.save(recordedPost);
57+
58+
model.addAttribute("posts", postRepository.findAll());
59+
return "blog/index";
60+
5161
}
5262

5363
@DeleteMapping("/posts/{postId}")
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
<p>Edit Post</p>
1+
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
2+
3+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
4+
<body>
5+
<p>Edit Post</p>
6+
<form th:method="post"
7+
th:action="@{/posts/{id}(id=${post.id})}"
8+
th:object="${post}">
9+
<label for="title">Title:</label>
10+
<input type="text" name="title" size="50" th:field="${post.title}"></input><br/>
11+
<label for="content">Content:</label><br/>
12+
<textarea name="content" cols="80" rows="5" th:field="${post.content}"></textarea>
13+
<br/>
14+
<input type="submit"></input>
15+
</form>
16+
</body>
17+
</html>

myapp/src/main/resources/templates/blog/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ <h1>Blog</h1>
1010

1111
<dd>
1212
<span th:text="${post.content}">Content</span>
13+
<a th:href="@{/posts/{id}/edit(id=${post.id})}">Edit</a>
1314
</dd>
1415
</dl>
1516

0 commit comments

Comments
 (0)