-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPostController.java
174 lines (142 loc) · 5.81 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.yen.mdblog.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yen.mdblog.entity.Po.Author;
import com.yen.mdblog.mapper.PostMapper;
import com.yen.mdblog.service.AuthorService;
import com.yen.mdblog.util.PostUtil;
import lombok.extern.log4j.Log4j2;
import com.yen.mdblog.entity.Po.Post;
import com.yen.mdblog.entity.Vo.CreatePost;
import com.yen.mdblog.repository.PostRepository;
import com.yen.mdblog.service.PostService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/posts")
@Log4j2
public class PostController {
@Autowired
PostRepository postRepository;
// TODO : implement paging with it
private final int PAGINATIONSIZE = 3; // how many posts show in a http://localhost:8080/posts/ page
@Autowired
PostService postService;
@Autowired
AuthorService authorService;
@Autowired
PostMapper postMapper;
@GetMapping("/all")
public String getPaginatedPosts(
@RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
@RequestParam(value="pageSize", defaultValue = "0" + PAGINATIONSIZE) int pageSize,
Principal principal,
Model model) {
/**
* Use pageHelper
* - https://www.796t.com/article.php?id=200769
*/
Pageable pageRequest = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC, "DateTime"));
Page<Post> postsPage = postRepository.findAll(pageRequest);
List<Post> posts = postsPage.toList();
log.info(">>> posts length = {}", posts.toArray().length);
PageInfo<Post> pageInfo = null;
//為了程式的嚴謹性,判斷非空:
if(pageNum <= 0){
pageNum = 0;
}
log.info("當前頁是:"+pageNum+"顯示條數是:"+pageSize);
//1.引入分頁外掛,pageNum是第幾頁,pageSize是每頁顯示多少條,預設查詢總數count
PageHelper.startPage(pageNum, pageSize);
//2.緊跟的查詢就是一個分頁查詢-必須緊跟.後面的其他查詢不會被分頁,除非再次呼叫PageHelper.startPage
try {
//Page<Post> postList = postRepository.findAll(pageRequest);//service查詢所有的資料的介面
List<Post> postList = postMapper.getAllPosts();//service查詢所有的資料的介面
log.info(">>> 分頁資料:" + postList.get(0).toString());
//3.使用PageInfo包裝查詢後的結果,5是連續顯示的條數,結果list型別是Page<E>
pageInfo = new PageInfo<Post>(postList, pageSize);
//4.使用model/map/modelandview等帶回前端
model.addAttribute("pageInfo",pageInfo);
}finally {
PageHelper.clearPage(); //清理 ThreadLocal 儲存的分頁引數,保證執行緒安全
}
log.info(">>> all posts count = " + posts.size());
model.addAttribute("posts", posts);
// get current user login via spring security
model.addAttribute("user", principal.getName());
Arrays.stream(pageInfo.getNavigatepageNums()).forEach(System.out::println);
model.addAttribute("user", principal.getName());
return "posts";
}
@GetMapping("/{id}")
public String getPostById(@PathVariable long id, Model model, Principal principal) {
Optional<Post> postOptional = postRepository.findById(id);
if (postOptional.isPresent()) {
model.addAttribute("post", postOptional.get());
} else {
model.addAttribute("error", "no-post");
}
model.addAttribute("user", principal.getName());
return "post";
}
@GetMapping("/create")
public String createPostForm(Model model, Principal principal){
model.addAttribute("CreatePost", new CreatePost());
model.addAttribute("user", principal.getName());
return "create_post";
}
@RequestMapping(value="/create", method= RequestMethod.POST)
public String createPost(CreatePost request, Model model, Principal principal){
log.info(">>> create post start ...");
Post post = new Post();
String authorName = principal.getName();
List<Author> authors = authorService.getAllAuthors();
List<String> names = authors.stream().map(x -> x.getName()).collect(Collectors.toList());
Author author = new Author();
if(!names.contains(authorName)){
//author.setId(request.getId()); // TODO : automate setup is (from name)
author.setName(principal.getName()); // set author name from spring security principal
authorService.saveAuthor(author);
}
author = authorService.getByName(authorName);
BeanUtils.copyProperties(request, post);
post.setId(postService.getTotalPost() + 1);
post.setAuthorId(author.getId());
post.setDateTime(LocalDateTime.now());
post.setSynopsis(PostUtil.getSynopsis(request.getContent()));
//post.setAuthor(author);
post.setDateTime(LocalDateTime.now());
//post.setAuthorId(authorId);
log.info(">>> request = " + request);
log.info(">>> post = " + post);
log.info(">>> author = " + author);
log.info(">>>> create post end ...");
postService.savePost(post);
// List<Author> authors = authorService.getAllAuthors();
// //List<Long> ids = authors.stream().map(x -> x.getId()).collect(Collectors.toList());
// List<String> names = authors.stream().map(x -> x.getName()).collect(Collectors.toList());
// if (!names.contains(author.getName())){
// authorService.saveAuthor(author);
// }
model.addAttribute("user", principal.getName());
return "success";
}
@GetMapping("/mypost")
public String getMyPost(Model model, Principal principal){
model.addAttribute("user", principal.getName());
return "header";
}
}