Repository navigation
Expand file tree
/
Copy pathPostController.java
More file actions
126 lines (101 loc) · 4.54 KB
/
Copy pathPostController.java
File metadata and controls
126 lines (101 loc) · 4.54 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.raysmond.blog.admin.controllers;
import com.raysmond.blog.forms.PostForm;
import com.raysmond.blog.models.Post;
import com.raysmond.blog.models.support.PostFormat;
import com.raysmond.blog.models.support.PostStatus;
import com.raysmond.blog.models.support.PostType;
import com.raysmond.blog.repositories.PostRepository;
import com.raysmond.blog.repositories.UserRepository;
import com.raysmond.blog.services.PostService;
import com.raysmond.blog.utils.DTOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.Valid;
import java.security.Principal;
import static org.springframework.web.bind.annotation.RequestMethod.*;
/**
* @author Raysmond
*/
@Controller("adminPostController")
@RequestMapping("admin/posts")
public class PostController {
private static final int PAGE_SIZE = 20;
@Autowired
private PostRepository postRepository;
@Autowired
private PostService postService;
@Autowired
private UserRepository userRepository;
@GetMapping("")
public String index(@RequestParam(defaultValue = "0") int page, Model model) {
Page<Post> posts = postRepository.findAll(new PageRequest(page, PAGE_SIZE, Sort.Direction.DESC, "id"));
model.addAttribute("totalPages", posts.getTotalPages());
model.addAttribute("page", page);
model.addAttribute("posts", posts);
return "admin/posts/index";
}
@GetMapping("new")
public String newPost(Model model) {
PostForm postForm = DTOUtil.map(new Post(), PostForm.class);
postForm.setPostTags("");
model.addAttribute("postForm", postForm);
model.addAttribute("postFormats", PostFormat.values());
model.addAttribute("postTypes", PostType.values());
model.addAttribute("postStatus", PostStatus.values());
return "admin/posts/new";
}
@RequestMapping(value = "{postId:[0-9]+}/edit")
public String editPost(@PathVariable Long postId, Model model) {
Post post = postRepository.findOne(postId);
PostForm postForm = DTOUtil.map(post, PostForm.class);
postForm.setPostTags(postService.getTagNames(post.getTags()));
model.addAttribute("post", post);
model.addAttribute("postForm", postForm);
model.addAttribute("postFormats", PostFormat.values());
model.addAttribute("postTypes", PostType.values());
model.addAttribute("postStatus", PostStatus.values());
return "admin/posts/edit";
}
@RequestMapping(value = "{postId:[0-9]+}/delete", method = {DELETE, POST})
public String deletePost(@PathVariable Long postId) {
postService.deletePost(postRepository.findOne(postId));
return "redirect:/admin/posts";
}
@RequestMapping(value = "", method = POST)
public String create(Principal principal, @Valid PostForm postForm, Errors errors, Model model) {
if (errors.hasErrors()) {
model.addAttribute("postFormats", PostFormat.values());
model.addAttribute("postStatus", PostStatus.values());
return "admin/posts/new";
} else {
Post post = DTOUtil.map(postForm, Post.class);
post.setUser(userRepository.findByEmail(principal.getName()));
post.setTags(postService.parseTagNames(postForm.getPostTags()));
postService.createPost(post);
return "redirect:/admin/posts";
}
}
@RequestMapping(value = "{postId:[0-9]+}", method = {PUT, POST})
public String update(@PathVariable Long postId, @Valid PostForm postForm, Errors errors, Model model) {
if (errors.hasErrors()) {
model.addAttribute("postFormats", PostFormat.values());
model.addAttribute("postStatus", PostStatus.values());
return "admin/posts_edit";
} else {
Post post = postRepository.findOne(postId);
DTOUtil.mapTo(postForm, post);
post.setTags(postService.parseTagNames(postForm.getPostTags()));
postService.updatePost(post);
return "redirect:/admin/posts";
}
}
}