-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostController.java
65 lines (48 loc) · 1.97 KB
/
PostController.java
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
package com.endTerm.EndTerm.Controller;
import com.endTerm.EndTerm.model.Post;
import com.endTerm.EndTerm.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
public class PostController {
@Autowired
private PostService postService;
@RequestMapping("/")
public String getUserPost(Model model, HttpSession session) {
List<Post> posts=postService.getAllPosts();
model.addAttribute("posts",posts);
return "posts";
}
@RequestMapping(method= RequestMethod.GET,value="/posts/newpost")
public String newPost(){
return "posts/create";
}
@RequestMapping(method = RequestMethod.POST, value="/posts/create")
public String createNewPost(Post newPost, HttpSession session){
postService.createPost(newPost);
return "redirect:/";
}
@RequestMapping(method=RequestMethod.DELETE,value = "/deletePost")
public String deletePost(@RequestParam(name="postId") Integer postId){
postService.deletePost(postId);
return "redirect:/";
}
@RequestMapping(method = RequestMethod.GET, value = "/editpost")
public String editPost(@RequestParam(value = "postId") Integer postId, Model model) {
Post post = postService.getPost(postId);
model.addAttribute("post", post);
return "posts/edit";
}
@RequestMapping(method = RequestMethod.PUT, value = "/editpost")
public String editPostSubmit(@RequestParam(value = "postId") Integer postId, Post updatedPost, HttpSession session) {
updatedPost.setId(postId);
postService.updatePost(updatedPost);
return "redirect:/";
}
}