Repository navigation
Expand file tree
/
Copy pathPostController.java
More file actions
55 lines (42 loc) · 1.61 KB
/
Copy pathPostController.java
File metadata and controls
55 lines (42 loc) · 1.61 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
package com.raysmond.blog.controllers;
import com.raysmond.blog.error.NotFoundException;
import com.raysmond.blog.models.Post;
import com.raysmond.blog.services.PostService;
import com.raysmond.blog.services.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Set;
import static org.springframework.web.bind.annotation.RequestMethod.*;
/**
* @author Raysmond<i@raysmond.com>
*/
@Controller
@RequestMapping("posts")
public class PostController {
@Autowired
private PostService postService;
@RequestMapping(value = "archive", method = GET)
public String archive(Model model){
model.addAttribute("posts", postService.getArchivePosts());
return "posts/archive";
}
@RequestMapping(value = "{permalink}", method = GET)
public String show(@PathVariable String permalink, Model model){
Post post = null;
try{
post = postService.getPublishedPostByPermalink(permalink);
} catch (NotFoundException ex){
if (permalink.matches("\\d+"))
post = postService.getPost(Long.valueOf(permalink));
}
if (post == null)
throw new NotFoundException("Post with permalink " + permalink + " is not found");
model.addAttribute("post", post);
model.addAttribute("tags", postService.getPostTags(post));
return "posts/show";
}
}