aboutsummaryrefslogtreecommitdiff
path: root/juick-spring-www/src/main/java/com/juick/www/controllers/ShowMessageController.java
blob: 62e7293bfacafd4d804824d9c04d7d000df102d6 (plain) (blame)
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
175
176
177
178
179
180
181
package com.juick.www.controllers;

import com.juick.User;
import com.juick.server.util.HttpNotFoundException;
import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
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 org.springframework.web.bind.annotation.RequestParam;

import javax.inject.Inject;

/**
 * Created by aalexeev on 11/21/16.
 */
@Controller
public class ShowMessageController {
    @Inject
    private UserService userService;
    @Inject
    private MessagesService messagesService;


    @RequestMapping("/{anything}")
    public String parseAnything(
            @PathVariable String anything,
            @RequestParam(required = false, defaultValue = "0") int before,
            @RequestParam(required = false) String tag,
            Model model) {

        if (StringUtils.isNotEmpty(tag)) {
            StringBuilder builder = new StringBuilder();

            builder.append("redirect:/").append(anything).append("/tag/").append(tag);

            if (before > 0)
                builder.append("?before=").append(before);

            return builder.toString();
        }

        if (before == 0) {
            boolean isPostNumber = WebUtils.isPostNumber(anything);
            int messageId = isPostNumber ?
                    NumberUtils.toInt(anything) : 0;

            if (isPostNumber && anything.equals(Integer.toString(messageId))) {
                if (messageId > 0) {
                    com.juick.User author = messagesService.getMessageAuthor(messageId);

                    if (author != null)
                        return "redirect:/" + author.getName() + "/" + anything;
                }

                model.addAttribute("messageId", anything);

                throw new HttpNotFoundException();
            }
        }
        return showUserMessages(anything, null, before, model);
    }

    @RequestMapping("/{userName}/tag/{tagName}")
    public String showUserMessagesByTag(
            @PathVariable String userName,
            @PathVariable String tagName,
            @RequestParam(required = false, defaultValue = "0") int before,
            Model model) {

        return showUserMessages(userName, tagName, before, model);
    }

    private String showUserMessages(
            String userName,
            String tagName,
            int before,
            Model model) {
        // Check validity of user name before quering from database
        if (WebUtils.isNotUserName(userName)) {
            model.addAttribute("userName", userName);
            return "userNotFound";
        }

        User user = userService.getUserByName(userName);
        if (user == null) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        return "views/index";
    }

    @RequestMapping("/{userName}/friends")
    public String getFriends(
            @PathVariable String userName,
            Model model) {
        // Check validity of user name before quering from database
        if (WebUtils.isNotUserName(userName)) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        User user = userService.getUserByName(userName);
        if (user == null) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        return "views/index";
    }

    @RequestMapping("/{userName}/readers")
    public String getReaders(
            @PathVariable String userName,
            Model model) {
        // Check validity of user name before quering from database
        if (WebUtils.isNotUserName(userName)) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        User user = userService.getUserByName(userName);
        if (user == null) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        return "views/index";
    }

    @RequestMapping("/{userName}/tags")
    public String geTags(
            @PathVariable String userName,
            Model model) {
        // Check validity of user name before quering from database
        if (WebUtils.isNotUserName(userName)) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        User user = userService.getUserByName(userName);
        if (user == null) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        return "views/index";
    }

    @RequestMapping("/{userName}/{postNumber}")
    public String checkShowPost(
            @PathVariable String userName,
            @PathVariable String postNumber,
            Model model) {
        // Check validity of post number before quering from database
        if (WebUtils.isNotPostNumber(postNumber)) {
            model.addAttribute("messageId", postNumber);
            throw new HttpNotFoundException();
        }

        // Check validity of user name before quering from database
        if (WebUtils.isNotUserName(userName)) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }

        User user = userService.getUserByName(userName);
        if (user == null) {
            model.addAttribute("userName", userName);
            throw new HttpNotFoundException();
        }


        return "index";
    }
}