33import com .raysmond .blog .Constants ;
44import com .raysmond .blog .error .NotFoundException ;
55import com .raysmond .blog .models .Post ;
6- import com .raysmond .blog .services .PostService ;
7- import com . raysmond . blog . services . TagService ;
8- import com . raysmond . blog . services . UserService ;
6+ import com .raysmond .blog .services .* ;
7+ import org . slf4j . Logger ;
8+ import org . slf4j . LoggerFactory ;
99import org .springframework .beans .factory .annotation .Autowired ;
1010import org .springframework .stereotype .Controller ;
1111import org .springframework .ui .Model ;
1212import org .springframework .web .bind .annotation .PathVariable ;
1313import org .springframework .web .bind .annotation .RequestMapping ;
1414
15+ import javax .servlet .http .HttpServletRequest ;
1516import java .util .List ;
1617import java .util .Set ;
1718
2526@ RequestMapping ("posts" )
2627public class PostController {
2728
29+ Logger logger = LoggerFactory .getLogger (PostController .class );
30+
2831 @ Autowired
2932 private PostService postService ;
3033
3134 @ Autowired
3235 private UserService userService ;
3336
37+ @ Autowired
38+ private VisitService visitService ;
39+
40+ @ Autowired
41+ private LikeService likeService ;
42+
43+ @ Autowired
44+ private RequestProcessorService requestProcessorService ;
45+
46+
3447 @ RequestMapping (value = "archive" , method = GET )
3548 public String archive (Model model ){
3649 model .addAttribute ("posts" , postService .getArchivePosts ());
@@ -39,26 +52,14 @@ public String archive(Model model){
3952 }
4053
4154 @ RequestMapping (value = "{permalink}" , method = GET )
42- public String show (@ PathVariable String permalink , Model model ){
43- Post post = null ;
44-
45- try {
46- post = this .postService .getPublishedPostByPermalink (permalink );
47- } catch (NotFoundException ex ){
48- if (permalink .matches ("\\ d+" )) {
49- if (this .userService .currentUser ().isAdmin ()) {
50- post = this .postService .getPost (Long .valueOf (permalink ));
51- } else {
52- post = this .postService .getPublishedPost (Long .valueOf (permalink ));
53- }
54- } else if (permalink .toLowerCase ().trim ().equals (Constants .PROJECTS_PAGE_PERMALINK )) {
55- post = this .postService .createProjectsPage ();
56- }
57- }
58-
59- if (post == null ) {
60- throw new NotFoundException ("Post with permalink " + permalink + " is not found" );
61- }
55+ public String show (@ PathVariable String permalink , Model model , HttpServletRequest request ){
56+ Post post = this .postService .findPostByPermalink (permalink );
57+
58+ logger .debug (String .format ("ACCESS %s from IP: %s" , permalink , this .requestProcessorService .getRealIp (request )));
59+
60+ this .visitService .saveVisit (this .requestProcessorService .getRealIp (request ), post );
61+ post .setVisitsCount (this .visitService .getUniqueVisitsCount (post ));
62+ post .setSympathyCount (this .likeService .getTotalLikesByPost (post ));
6263
6364 model .addAttribute ("post" , post );
6465 model .addAttribute ("tags" , this .postService .getPostTags (post ));
0 commit comments