-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPostController.java
More file actions
69 lines (58 loc) · 2.07 KB
/
PostController.java
File metadata and controls
69 lines (58 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.example.devSns.controller;
import com.example.devSns.entity.PostEntity;
import com.example.devSns.service.PostService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/posts")
public class PostController {
private final PostService postService;
// 생성자
public PostController(PostService postService) {
this.postService = postService;
}
// HTTP GET /posts
@GetMapping
public String list(Model model) {
model.addAttribute("posts", postService.getAllPosts());
return "list";
}
// HTTP GET /posts/new
@GetMapping("/new")
public String form(Model model) {
model.addAttribute("post", new PostEntity());
return "form";
}
// HTTP POST /posts
@PostMapping
public String create(@ModelAttribute PostEntity postEntity) {
postService.createPost(postEntity);
return "redirect:/posts"; // 글 생성 후 돌아올 주소
}
// HTTP GET /posts/{id}
@GetMapping("/{id}")
public String detail(@PathVariable Long id, Model model) {
model.addAttribute("post", postService.getPost(id));
return "detail";
}
// HTTP GET /posts/{id}/edit
@GetMapping("/{id}/edit")
public String editForm(@PathVariable Long id, Model model) {
PostEntity postEntity = postService.getPost(id);
model.addAttribute("post", postEntity);
return "edit";
}
// HTTP POST /posts/{id}/update
@PostMapping("/{id}/update")
public String update(@PathVariable Long id, @ModelAttribute PostEntity updatedPost) {
postService.updatePost(id, updatedPost);
return "redirect:/posts/" + id; // 수정 후 돌아올 주소
}
// HTTP POST /posts/{id}/delete
@PostMapping("/{id}/delete")
public String delete(@PathVariable Long id) {
postService.deletePost(id);
return "redirect:/posts"; // 삭제 후 돌아올 주소
}
}