Skip to content

Commit

Permalink
Enable post to be deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
lidimayra committed Jan 10, 2025
1 parent 8b33fbe commit 62a986c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions myapp/src/main/java/com/example/myapp/BlogController.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ public String updatePost(@PathVariable("postId") long id, Model model, Post post

}

@DeleteMapping("/posts/{postId}")
public String deletePost() {
//TODO: logic responsible for deleting a post
return null;
@GetMapping("/posts/{postId}/delete")
public String deletePost(@PathVariable("postId") long id, Model model) {
Post recordedPost = postRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Invalid Post Id:" + id));
postRepository.delete(recordedPost);
model.addAttribute("posts", postRepository.findAll());
return "blog/index";
}
}
1 change: 1 addition & 0 deletions myapp/src/main/resources/templates/blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1>Blog</h1>
<dd>
<a th:href="@{/posts/{id}(id=${post.id})}">Show</a>
<a th:href="@{/posts/{id}/edit(id=${post.id})}">Edit</a>
<a th:href="@{/posts/{id}/delete(id=${post.id})}">Delete</a>
</dd>
</dl>

Expand Down

0 comments on commit 62a986c

Please sign in to comment.