Skip to content

Commit a5e3af3

Browse files
author
qtipee
committed
Some fixes + code review
1 parent f8f1cc4 commit a5e3af3

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

jamais404/src/main/java/com/jamais404/Handle404Controller.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,53 @@ public class Handle404Controller implements ErrorController {
3131
private PageRepository pageRepository;
3232

3333
/**
34-
* Handles every route different from the ones registered in the HomeController.
34+
* Handles every route different from the ones registered.
35+
* This is the way of not throwing a 404 error.
3536
*
3637
* @param model
3738
* @param request
3839
* @return
3940
*/
4041
@RequestMapping("/error")
41-
public String handleError(Model model, HttpServletRequest request,
42-
Authentication authentication,
42+
public String handleError(Model model, HttpServletRequest request, Authentication authentication,
4343
@RequestParam(value = "msg", defaultValue = "") String msg) {
4444
model.addAttribute("active", authentication.getName());
4545

4646
// Gets the URI that triggered the 404 error (to avoid /error)
4747
String originalUri = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI).toString();
48+
// Fetches the user pages names (sorted by alphabetical order)
4849
String parsedUri = originalUri.replace("/", "");
50+
4951
model.addAttribute("url", parsedUri);
5052

5153
Page page = pageRepository.findByName(parsedUri);
5254

55+
// The page is already found by a user
5356
if (page != null) {
5457
String ownerUsername = page.getOwner().getUsername();
5558
Timestamp datetime = page.getDatetime();
59+
// Parses the page timestamp into a more readable format
5660
String stringDatetime = TimeStampTools.timeStampToString(datetime);
5761

5862
model.addAttribute("datetime", stringDatetime);
5963

64+
// Page found the current user
6065
if (ownerUsername.equals(authentication.getName())) {
6166
model.addAttribute("username", "you");
6267
model.addAttribute("userLink", authentication.getName());
6368
} else {
69+
// Page found another user than the current one
6470
model.addAttribute("username", ownerUsername);
6571
model.addAttribute("userLink", ownerUsername);
6672
}
6773

74+
// Fetches the page comments (sorted by DESC timestamps)
6875
List<Comment> comments = page.getComments()
6976
.stream()
7077
.parallel()
7178
.sorted((c1, c2) -> - c1.getDatetime().compareTo(c2.getDatetime()))
7279
.collect(Collectors.toList());
73-
80+
7481
model.addAttribute("comments", comments);
7582

7683
// Message - from the submission of a comment - to display in a toast
@@ -80,6 +87,7 @@ public String handleError(Model model, HttpServletRequest request,
8087
return "already_found404";
8188
}
8289

90+
// New page found ; added to the current user
8391
User owner = userRepository.findByUsername(authentication.getName());
8492

8593
page = new Page();
@@ -92,6 +100,7 @@ public String handleError(Model model, HttpServletRequest request,
92100

93101
@Override
94102
public String getErrorPath() {
103+
// Cannot omit this overriden method
95104
return "getErrorPath";
96105
}
97106
}

jamais404/src/main/java/com/jamais404/HomeController.java

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public String home(Model model, Authentication authentication) {
4545
*/
4646
@PostMapping(value = "/search")
4747
public ModelAndView search(@RequestParam String query, Authentication authentication) {
48+
// Trims the slashes ; they cause troubles with the URLs
4849
String redirectUrl = "redirect:/" + query.replace("/", "");
4950

5051
return new ModelAndView(redirectUrl);
@@ -61,6 +62,7 @@ public String user(Model model, @RequestParam String username, Authentication au
6162

6263
User user = userRepository.findByUsername(username);
6364

65+
// Fetches the user pages names (sorted by alphabetical order)
6466
Set<String> pagesNames = user.getPages()
6567
.stream()
6668
.parallel()
@@ -79,8 +81,10 @@ public String user(Model model, @RequestParam String username, Authentication au
7981
public ModelAndView comment(Model model, @RequestParam String text, @RequestParam String url, Authentication authentication) {
8082
User user = userRepository.findByUsername(authentication.getName());
8183
Page page = pageRepository.findByName(url);
84+
// If the user has modified the HTML or JS before submitting a comment
8285
String msg = "Do not modify the code !";
8386

87+
// The page must exist (be found) in order to add a comment to it
8488
if (page != null) {
8589
Comment comment = new Comment();
8690
comment.setText(text);
@@ -91,6 +95,7 @@ public ModelAndView comment(Model model, @RequestParam String text, @RequestPara
9195
msg = "Comment added !";
9296
}
9397

98+
// Refreshes the page and shows a message in a toast
9499
String redirectUrl = "redirect:" + url + "?msg=" + msg;
95100

96101
return new ModelAndView(redirectUrl);

jamais404/src/main/java/com/jamais404/auth/validator/UserValidator.java

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public boolean supports(Class<?> aClass) {
3232
public void validate(Object o, Errors errors) {
3333
User user = (User) o;
3434

35-
3635
// Email
3736
ValidationUtils.rejectIfEmptyOrWhitespace(errors, EMAIL_STRING, NOT_EMPTY_STRING);
3837
Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(user.getEmail());

jamais404/src/main/resources/templates/already_found404.html

+18-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h2 class="subtitle">Comments</h2>
4646

4747
<ul class="comments">
4848
<li th:each="comment : ${comments}">
49-
<p class="text comment-info"><a th:href="@{'user?username=' + ${comment.user.username}}" class="pink-text text-darken-2" th:utext="${comment.user.username + ' '}"></a><span th:utext="${comment.datetime}"></span></p>
49+
<p class="text comment-info"><a th:href="@{'user?username=' + ${comment.user.username}}" class="pink-text text-darken-2" th:utext="${comment.user.username + ' '}"></a><span class="datetime" th:utext="${comment.datetime}"></span></p>
5050
<p class="text comment-text grey-text" th:utext="${comment.text}"></p>
5151
</li>
5252
</ul>
@@ -103,6 +103,23 @@ <h2 class="subtitle">Don't give up !</h2>
103103
}
104104
})
105105
</script>
106+
107+
<script>
108+
document.addEventListener('DOMContentLoaded', (evt) => {
109+
document.querySelectorAll('.datetime').forEach(datetime => {
110+
// Formats the comments timestamps to more readable dates and times
111+
let cutDatetime = datetime.innerHTML.match(/\S+/g) || []
112+
let date = cutDatetime[0]
113+
let time = cutDatetime[1]
114+
115+
date = date.split('-').reverse().join('.')
116+
time = time.substring(0, time.indexOf('.'))
117+
time = time.substring(0, time.length - 3)
118+
119+
datetime.innerHTML = 'on ' + date + ' at ' + time
120+
})
121+
})
122+
</script>
106123

107124
<footer></footer>
108125

jamais404/target/classes/templates/already_found404.html

+18-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h2 class="subtitle">Comments</h2>
4646

4747
<ul class="comments">
4848
<li th:each="comment : ${comments}">
49-
<p class="text comment-info"><a th:href="@{'user?username=' + ${comment.user.username}}" class="pink-text text-darken-2" th:utext="${comment.user.username + ' '}"></a><span th:utext="${comment.datetime}"></span></p>
49+
<p class="text comment-info"><a th:href="@{'user?username=' + ${comment.user.username}}" class="pink-text text-darken-2" th:utext="${comment.user.username + ' '}"></a><span class="datetime" th:utext="${comment.datetime}"></span></p>
5050
<p class="text comment-text grey-text" th:utext="${comment.text}"></p>
5151
</li>
5252
</ul>
@@ -103,6 +103,23 @@ <h2 class="subtitle">Don't give up !</h2>
103103
}
104104
})
105105
</script>
106+
107+
<script>
108+
document.addEventListener('DOMContentLoaded', (evt) => {
109+
document.querySelectorAll('.datetime').forEach(datetime => {
110+
// Formats the comments timestamps to more readable dates and times
111+
let cutDatetime = datetime.innerHTML.match(/\S+/g) || []
112+
let date = cutDatetime[0]
113+
let time = cutDatetime[1]
114+
115+
date = date.split('-').reverse().join('.')
116+
time = time.substring(0, time.indexOf('.'))
117+
time = time.substring(0, time.length - 3)
118+
119+
datetime.innerHTML = 'on ' + date + ' at ' + time
120+
})
121+
})
122+
</script>
106123

107124
<footer></footer>
108125

0 commit comments

Comments
 (0)