Skip to content

Commit fe088a0

Browse files
committed
hide post
1 parent 87fc1cc commit fe088a0

28 files changed

Lines changed: 586 additions & 136 deletions

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import alexp.blog.model.Post;
55
import alexp.blog.service.CommentService;
66
import alexp.blog.service.PostService;
7+
import alexp.blog.service.UserService;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.security.access.prepost.PreAuthorize;
910
import org.springframework.stereotype.Controller;
@@ -20,6 +21,9 @@ public class CommentController {
2021
@Autowired
2122
private PostService postService;
2223

24+
@Autowired
25+
private UserService userService;
26+
2327
@Autowired
2428
private CommentService commentService;
2529

@@ -30,6 +34,9 @@ public String showComments(@PathVariable("postId") Long postId, ModelMap model)
3034
if (post == null)
3135
throw new ResourceNotFoundException();
3236

37+
if (post.isHidden() && !userService.isAdmin())
38+
throw new ResourceNotFoundException();
39+
3340
List<Comment> comments = commentService.getPostComments(post);
3441

3542
model.addAttribute("comments", comments);
@@ -50,6 +57,9 @@ public String showComments(@PathVariable("postId") Long postId, ModelMap model)
5057
if (post == null)
5158
return "post not found";
5259

60+
if (post.isHidden() && !userService.isAdmin())
61+
return "post not found";
62+
5363
commentService.saveNewComment(comment, post);
5464

5565
return "ok";

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public String showPost(@PathVariable("postId") Long postId, ModelMap model) {
4545
if (post == null)
4646
throw new ResourceNotFoundException();
4747

48+
if (post.isHidden() && !userService.isAdmin())
49+
throw new ResourceNotFoundException();
50+
4851
model.addAttribute("post", post);
4952

5053
if (userService.isAuthenticated()) {
@@ -95,7 +98,7 @@ public String showEditPostForm(@PathVariable("postId") Long postId, ModelMap mod
9598

9699
@PreAuthorize("hasRole('ROLE_ADMIN')")
97100
@RequestMapping(value = "/posts/{postId}/edit", method = RequestMethod.POST)
98-
public String showEditPostForm(ModelMap model, @Valid @ModelAttribute("post") PostEditDto post, BindingResult result,
101+
public String updatePost(ModelMap model, @Valid @ModelAttribute("post") PostEditDto post, BindingResult result,
99102
@PathVariable("postId") Long postId) {
100103
post.setId(postId);
101104

@@ -109,4 +112,20 @@ public String showEditPostForm(ModelMap model, @Valid @ModelAttribute("post") Po
109112

110113
return "redirect:/posts/" + postId;
111114
}
115+
116+
@PreAuthorize("hasRole('ROLE_ADMIN')")
117+
@RequestMapping(value = "/posts/{postId}/hide", method = RequestMethod.POST)
118+
public @ResponseBody String hidePost(@PathVariable("postId") Long postId) {
119+
postService.setPostVisibility(postId, true);
120+
121+
return "ok";
122+
}
123+
124+
@PreAuthorize("hasRole('ROLE_ADMIN')")
125+
@RequestMapping(value = "/posts/{postId}/unhide", method = RequestMethod.POST)
126+
public @ResponseBody String unhidePost(@PathVariable("postId") Long postId) {
127+
postService.setPostVisibility(postId, false);
128+
129+
return "ok";
130+
}
112131
}

src/main/java/alexp/blog/model/Post.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class Post {
3434
@Temporal(TemporalType.TIMESTAMP)
3535
private Date dateTime;
3636

37+
@Column(nullable = false)
38+
private boolean hidden = false;
39+
3740
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
3841
@JoinTable(name = "posts_tags",
3942
joinColumns = @JoinColumn(name = "post_id", referencedColumnName = "id"),
@@ -115,4 +118,12 @@ public List<Comment> getComments() {
115118
public void setComments(List<Comment> comments) {
116119
this.comments = comments;
117120
}
121+
122+
public boolean isHidden() {
123+
return hidden;
124+
}
125+
126+
public void setHidden(boolean hidden) {
127+
this.hidden = hidden;
128+
}
118129
}

src/main/java/alexp/blog/model/User.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import javax.persistence.*;
77
import javax.validation.constraints.Size;
88
import java.util.ArrayList;
9-
import java.util.Collection;
109
import java.util.List;
1110

1211
@Entity
@@ -108,6 +107,16 @@ public void setComments(List<Comment> comments) {
108107
this.comments = comments;
109108
}
110109

110+
public boolean hasRole(String role) {
111+
role = role.toUpperCase();
112+
113+
if (!role.startsWith("ROLE_"))
114+
role = "ROLE_" + role;
115+
116+
final String finalRole = role;
117+
return getRoles().stream().anyMatch(r -> r.getName().equals(finalRole));
118+
}
119+
111120
@Override
112121
public String toString() {
113122
return "User{" +
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package alexp.blog.repository;
22

33
import alexp.blog.model.Post;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
46
import org.springframework.data.jpa.repository.JpaRepository;
57

68
public interface PostRepository extends JpaRepository<Post, Long> {
9+
10+
Page<Post> findByHiddenFalseOrHiddenIsNull(Pageable pageable);
711
}

src/main/java/alexp/blog/service/PostService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ public interface PostService {
1515
Post saveNewPost(PostEditDto postEditDto);
1616

1717
Post updatePost(PostEditDto postEditDto);
18+
19+
void setPostVisibility(Long postId, boolean hide);
1820
}

src/main/java/alexp/blog/service/PostServiceImpl.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ public class PostServiceImpl implements PostService
2222
private PostRepository postRepository;
2323

2424
@Autowired
25-
TagRepository tagRepository;
25+
private TagRepository tagRepository;
26+
27+
@Autowired
28+
private UserService userService;
2629

2730
@Override
2831
public Page<Post> getPostsPage(int pageNumber, int pageSize) {
29-
return postRepository.findAll(new PageRequest(pageNumber, pageSize, Sort.Direction.DESC, "dateTime"));
32+
PageRequest pageRequest = new PageRequest(pageNumber, pageSize, Sort.Direction.DESC, "dateTime");
33+
34+
if (userService.isAdmin())
35+
return postRepository.findAll(pageRequest);
36+
37+
return postRepository.findByHiddenFalseOrHiddenIsNull(pageRequest);
3038
}
3139

3240
@Override
@@ -71,6 +79,15 @@ public Post updatePost(PostEditDto postEditDto) {
7179
return post;
7280
}
7381

82+
@Override
83+
public void setPostVisibility(Long postId, boolean hide) {
84+
Post post = getPost(postId);
85+
86+
post.setHidden(hide);
87+
88+
postRepository.saveAndFlush(post);
89+
}
90+
7491
private PostEditDto convertToPostEditDto(Post post) {
7592
PostEditDto postEditDto = new PostEditDto();
7693

src/main/java/alexp/blog/service/UserService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public interface UserService extends UserDetailsService {
1919

2020
boolean isAuthenticated();
2121

22+
boolean isAdmin();
23+
2224
User currentUser();
2325

2426
}

src/main/java/alexp/blog/service/UserServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ public boolean isAuthenticated() {
9696
return auth != null && !(auth instanceof AnonymousAuthenticationToken) && auth.isAuthenticated();
9797
}
9898

99+
@Override
100+
public boolean isAdmin() {
101+
User user = currentUser();
102+
103+
return user != null && user.hasRole("ROLE_ADMIN");
104+
}
105+
99106
@Override
100107
public User currentUser() {
101108
if (!isAuthenticated())

0 commit comments

Comments
 (0)