Skip to content

Commit 94de40d

Browse files
committed
Posts urls refactoring
1 parent b17b103 commit 94de40d

6 files changed

Lines changed: 37 additions & 37 deletions

File tree

src/main/java/alexp/blog/controller/PostsController.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public String showPostsList(@RequestParam(value = "page", defaultValue = "0") In
3838
return "search by tag: TODO";
3939
}
4040

41-
@RequestMapping(value = "/post", method = RequestMethod.GET)
42-
public String showPost(@RequestParam("id") Long postId, ModelMap model) {
41+
@RequestMapping(value = "/posts/{postId}", method = RequestMethod.GET)
42+
public String showPost(@PathVariable("postId") Long postId, ModelMap model) {
4343
Post post = postService.getPost(postId);
4444

4545
if (post == null)
@@ -55,7 +55,7 @@ public String showPost(@RequestParam("id") Long postId, ModelMap model) {
5555
}
5656

5757
@PreAuthorize("hasRole('ROLE_ADMIN')")
58-
@RequestMapping(value = "/post/create", method = RequestMethod.GET)
58+
@RequestMapping(value = "/posts/create", method = RequestMethod.GET)
5959
public String showCreatePostForm(ModelMap model) {
6060
model.addAttribute("post", new PostEditDto());
6161

@@ -65,7 +65,7 @@ public String showCreatePostForm(ModelMap model) {
6565
}
6666

6767
@PreAuthorize("hasRole('ROLE_ADMIN')")
68-
@RequestMapping(value = "/post/create", method = RequestMethod.POST)
68+
@RequestMapping(value = "/posts/create", method = RequestMethod.POST)
6969
public String createPost(ModelMap model, @Valid @ModelAttribute("post") PostEditDto post, BindingResult result) {
7070
if (result.hasErrors()) {
7171
model.addAttribute("edit", false);
@@ -79,8 +79,8 @@ public String createPost(ModelMap model, @Valid @ModelAttribute("post") PostEdit
7979
}
8080

8181
@PreAuthorize("hasRole('ROLE_ADMIN')")
82-
@RequestMapping(value = "/post/edit", method = RequestMethod.GET)
83-
public String showEditPostForm(@RequestParam("id") Long postId, ModelMap model) {
82+
@RequestMapping(value = "/posts/{postId}/edit", method = RequestMethod.GET)
83+
public String showEditPostForm(@PathVariable("postId") Long postId, ModelMap model) {
8484
PostEditDto post = postService.getEditablePost(postId);
8585

8686
if (post == null)
@@ -94,9 +94,9 @@ public String showEditPostForm(@RequestParam("id") Long postId, ModelMap model)
9494
}
9595

9696
@PreAuthorize("hasRole('ROLE_ADMIN')")
97-
@RequestMapping(value = "/post/edit", method = RequestMethod.POST)
97+
@RequestMapping(value = "/posts/{postId}/edit", method = RequestMethod.POST)
9898
public String showEditPostForm(ModelMap model, @Valid @ModelAttribute("post") PostEditDto post, BindingResult result,
99-
@RequestParam("id") Long postId) {
99+
@PathVariable("postId") Long postId) {
100100
post.setId(postId);
101101

102102
if (result.hasErrors()) {
@@ -107,6 +107,6 @@ public String showEditPostForm(ModelMap model, @Valid @ModelAttribute("post") Po
107107

108108
postService.updatePost(post);
109109

110-
return "redirect:/post?id=" + postId;
110+
return "redirect:/posts/" + postId;
111111
}
112112
}

src/main/webapp/WEB-INF/templates/editpost.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
<div class="col-sm-8">
9595
<h2 th:text="${edit} ? 'Edit post' : 'Create post'"></h2>
9696

97-
<form th:action="${edit} ? @{/post/edit(id=${post.id})} : @{/post/create}" th:object="${post}" method="post" id="postForm">
97+
<form th:action="${edit} ? @{|/posts/${post.id}/edit|} : @{/posts/create}" th:object="${post}" method="post" id="postForm">
9898
<div th:if="${#fields.hasErrors('*')}">
9999
<ul class="list-no-indent">
100100
<li class="error-line" th:each="err : ${#fields.errors('*')}" th:text="${err}"></li>

src/main/webapp/WEB-INF/templates/layouts/blog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ <h1>Blog</h1>
6767

6868
<div class="user-menu">
6969
<div sec:authorize="hasRole('ROLE_ADMIN')">
70-
<a th:href="@{/post/create}" role="button" class="btn btn-primary">
70+
<a th:href="@{/posts/create}" role="button" class="btn btn-primary">
7171
<span class="glyphicon glyphicon-pencil"></span> Create post
7272
</a>
7373
</div>

src/main/webapp/WEB-INF/templates/post.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ <h2 class="post-title" th:text="${post.title}"></h2>
125125
<div class="post-content" th:utext="${post.fullPostTextHtml()}"></div>
126126

127127
<div class="post-actions">
128-
<a sec:authorize="hasRole('ROLE_ADMIN')" th:href="@{/post/edit(id=${post.id})}">edit</a>
128+
<a sec:authorize="hasRole('ROLE_ADMIN')" th:href="@{|/posts/${post.id}/edit|}">edit</a>
129129
</div>
130130
</div>
131131

src/main/webapp/WEB-INF/templates/posts.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h2 th:text="${tag}"></h2>
1919
</div>
2020

2121
<div class="post" th:each="post, iterStat : ${postsPage.getContent()}">
22-
<a class="h2 post-title" th:text="${post.title}" th:href="@{/post(id=${post.id})}"></a>
22+
<a class="h2 post-title" th:text="${post.title}" th:href="@{|/posts/${post.id}|}"></a>
2323
<p class="post-date" th:text="${#dates.format(post.dateTime, 'MMM dd, yyyy HH:mm')}"></p>
2424

2525
<div class="post-tags">
@@ -29,13 +29,13 @@ <h2 th:text="${tag}"></h2>
2929
<div class="post-content" th:utext="${post.hasShortTextPart()}? ${post.shortTextPartHtml()} : ${post.fullPostTextHtml()}"></div>
3030

3131
<div th:if="${post.hasShortTextPart()}">
32-
<a class="lead" th:href="@{/post(id=${post.id})}">Continue &rarr;</a>
32+
<a class="lead" th:href="@{|/posts/${post.id}|}">Continue &rarr;</a>
3333
</div>
3434

3535
<div class="post-actions">
36-
<a th:href="@{/post(id=${post.id})} + '#comments'"
36+
<a th:href="@{|/posts/${post.id}#comments|}"
3737
th:text="${post.comments.size() == 0} ? 'comments' : (${post.comments.size()} + ' comment' + (${post.comments.size() &gt; 1} ? 's' : ''))"></a>
38-
<a sec:authorize="hasRole('ROLE_ADMIN')" th:href="@{/post/edit(id=${post.id})}">edit</a>
38+
<a sec:authorize="hasRole('ROLE_ADMIN')" th:href="@{|/posts/${post.id}/edit|}">edit</a>
3939
</div>
4040
</div>
4141

src/test/java/alexp/blog/controller/PostsControllerIT.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void shouldShowPostsPage() throws Exception {
3636
@Test
3737
@ExpectedDatabase("data.xml")
3838
public void shouldShowPostPage() throws Exception {
39-
mockMvc.perform(get("/post?id=1"))
39+
mockMvc.perform(get("/posts/1"))
4040
.andExpect(status().isOk())
4141
.andExpect(view().name("post"))
4242
.andExpect(model().attributeDoesNotExist("comment"));
@@ -45,7 +45,7 @@ public void shouldShowPostPage() throws Exception {
4545
@Test
4646
@ExpectedDatabase("data.xml")
4747
public void shouldShowCommentFormWhenAuthorized() throws Exception {
48-
mockMvc.perform(get("/post?id=1").with(userBob()))
48+
mockMvc.perform(get("/posts/1").with(userBob()))
4949
.andExpect(status().isOk())
5050
.andExpect(view().name("post"))
5151
.andExpect(model().attribute("comment", instanceOf(Comment.class)));
@@ -54,14 +54,14 @@ public void shouldShowCommentFormWhenAuthorized() throws Exception {
5454
@Test
5555
@ExpectedDatabase("data.xml")
5656
public void shouldReturn404WhenPostNotExists() throws Exception {
57-
mockMvc.perform(get("/post?id=999"))
57+
mockMvc.perform(get("/posts/999"))
5858
.andExpect(status().isNotFound());
5959
}
6060

6161
@Test
6262
@ExpectedDatabase("data.xml")
6363
public void shouldShowCreatePostPageIfAdmin() throws Exception {
64-
mockMvc.perform(get("/post/create").with(userAdmin()))
64+
mockMvc.perform(get("/posts/create").with(userAdmin()))
6565
.andExpect(status().isOk())
6666
.andExpect(view().name("editpost"))
6767
.andExpect(model().attribute("post", instanceOf(PostEditDto.class)))
@@ -71,21 +71,21 @@ public void shouldShowCreatePostPageIfAdmin() throws Exception {
7171
@Test
7272
@ExpectedDatabase("data.xml")
7373
public void shouldDenyCreatePostIfNotAdmin() throws Exception {
74-
mockMvc.perform(get("/post/create"))
74+
mockMvc.perform(get("/posts/create"))
7575
.andExpect(status().isFound())
7676
.andExpect(redirectedUrlPattern("**/login"));
7777

78-
mockMvc.perform(get("/post/create").with(userBob()))
78+
mockMvc.perform(get("/posts/create").with(userBob()))
7979
.andExpect(status().isForbidden());
8080

81-
mockMvc.perform(post("/post/create").with(userBob()).with(csrf()))
81+
mockMvc.perform(post("/posts/create").with(userBob()).with(csrf()))
8282
.andExpect(status().isForbidden());
8383
}
8484

8585
@Test
8686
@ExpectedDatabase("data.xml")
8787
public void shouldReturnAddPostFormWithErrorsWhenSubmittedInvalidPost() throws Exception {
88-
mockMvc.perform(post("/post/create").with(userAdmin()).with(csrf())
88+
mockMvc.perform(post("/posts/create").with(userAdmin()).with(csrf())
8989
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
9090
.andExpect(status().isOk())
9191
.andExpect(model().attributeHasFieldErrors("post", "title"))
@@ -95,7 +95,7 @@ public void shouldReturnAddPostFormWithErrorsWhenSubmittedInvalidPost() throws E
9595
String title = "post title";
9696
String text = "too short";
9797

98-
mockMvc.perform(post("/post/create").with(userAdmin()).with(csrf())
98+
mockMvc.perform(post("/posts/create").with(userAdmin()).with(csrf())
9999
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
100100
.param("title", title)
101101
.param("text", text))
@@ -114,7 +114,7 @@ public void shouldAddPosts() throws Exception {
114114
String text = "new post short text===cut===new post full text Lorem ipsum";
115115
String tags = "c++, java, hello world";
116116

117-
mockMvc.perform(post("/post/create").with(userAdmin()).with(csrf())
117+
mockMvc.perform(post("/posts/create").with(userAdmin()).with(csrf())
118118
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
119119
.param("title", title)
120120
.param("text", text)
@@ -126,7 +126,7 @@ public void shouldAddPosts() throws Exception {
126126
String text2 = "new post 2 text Lorem ipsum dolor sit amet, consectetur adipiscing elit";
127127
String tags2 = "java";
128128

129-
mockMvc.perform(post("/post/create").with(userAdmin()).with(csrf())
129+
mockMvc.perform(post("/posts/create").with(userAdmin()).with(csrf())
130130
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
131131
.param("title", title)
132132
.param("text", text2)
@@ -139,7 +139,7 @@ public void shouldAddPosts() throws Exception {
139139
@Test
140140
@ExpectedDatabase("data.xml")
141141
public void shouldShowEditPostPageIfAdmin() throws Exception {
142-
mockMvc.perform(get("/post/edit?id=1").with(userAdmin()))
142+
mockMvc.perform(get("/posts/1/edit").with(userAdmin()))
143143
.andExpect(status().isOk())
144144
.andExpect(view().name("editpost"))
145145
.andExpect(model().attribute("post", hasProperty("id", is(Matchers.equalTo(1L)))))
@@ -149,21 +149,21 @@ public void shouldShowEditPostPageIfAdmin() throws Exception {
149149
@Test
150150
@ExpectedDatabase("data.xml")
151151
public void shouldDenyEditPostIfNotAdmin() throws Exception {
152-
mockMvc.perform(get("/post/edit?id=1"))
152+
mockMvc.perform(get("/posts/1/edit"))
153153
.andExpect(status().isFound())
154154
.andExpect(redirectedUrlPattern("**/login"));
155155

156-
mockMvc.perform(get("/post/edit?id=1").with(userBob()))
156+
mockMvc.perform(get("/posts/1/edit").with(userBob()))
157157
.andExpect(status().isForbidden());
158158

159-
mockMvc.perform(post("/post/edit?id=1").with(userBob()).with(csrf()))
159+
mockMvc.perform(post("/posts/1/edit").with(userBob()).with(csrf()))
160160
.andExpect(status().isForbidden());
161161
}
162162

163163
@Test
164164
@ExpectedDatabase("data.xml")
165165
public void shouldReturnEditPostFormWithErrorsWhenSubmittedInvalidPost() throws Exception {
166-
mockMvc.perform(post("/post/edit?id=1").with(userAdmin()).with(csrf())
166+
mockMvc.perform(post("/posts/1/edit").with(userAdmin()).with(csrf())
167167
.contentType(MediaType.APPLICATION_FORM_URLENCODED))
168168
.andExpect(status().isOk())
169169
.andExpect(model().attributeHasFieldErrors("post", "title"))
@@ -174,7 +174,7 @@ public void shouldReturnEditPostFormWithErrorsWhenSubmittedInvalidPost() throws
174174
String title = "post title";
175175
String text = "too short";
176176

177-
mockMvc.perform(post("/post/edit?id=1").with(userAdmin()).with(csrf())
177+
mockMvc.perform(post("/posts/1/edit").with(userAdmin()).with(csrf())
178178
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
179179
.param("title", title)
180180
.param("text", text))
@@ -194,25 +194,25 @@ public void shouldEditPosts() throws Exception {
194194
String text = "edited Lorem ipsum dolor sit amet, consectetur adipiscing elit";
195195
String tags = "c, c++, c#";
196196

197-
mockMvc.perform(post("/post/edit?id=1").with(userAdmin()).with(csrf())
197+
mockMvc.perform(post("/posts/1/edit").with(userAdmin()).with(csrf())
198198
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
199199
.param("title", title)
200200
.param("text", text)
201201
.param("tags", tags))
202202
.andExpect(status().isFound())
203203
.andExpect(model().hasNoErrors())
204-
.andExpect(view().name("redirect:/post?id=1"));
204+
.andExpect(view().name("redirect:/posts/1"));
205205

206206
String text2 = "edited short===cut===edited Lorem ipsum dolor sit amet, consectetur adipiscing elit";
207207
String tags2 = "c++, meow";
208208

209-
mockMvc.perform(post("/post/edit?id=2").with(userAdmin()).with(csrf())
209+
mockMvc.perform(post("/posts/2/edit").with(userAdmin()).with(csrf())
210210
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
211211
.param("title", title)
212212
.param("text", text2)
213213
.param("tags", tags2))
214214
.andExpect(status().isFound())
215215
.andExpect(model().hasNoErrors())
216-
.andExpect(view().name("redirect:/post?id=2"));
216+
.andExpect(view().name("redirect:/posts/2"));
217217
}
218218
}

0 commit comments

Comments
 (0)