Skip to content
Snippets Groups Projects
Commit a6cc6944 authored by Gwan Ju's avatar Gwan Ju
Browse files

feat: Revise comment and like deletion functionality

parent cf73aa05
No related branches found
No related tags found
1 merge request!6feat: Revise comment and like deletion functionality
package umc.spring.post.config.security; package umc.spring.post.config.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import umc.spring.post.data.dto.UserInfoDto; import umc.spring.post.data.dto.UserInfoDto;
import umc.spring.post.data.entity.User;
import umc.spring.post.repository.UserRepository;
import java.util.Objects;
public class SecurityUtil { public class SecurityUtil {
......
...@@ -58,15 +58,14 @@ public class PostController { ...@@ -58,15 +58,14 @@ public class PostController {
@PostMapping("/post/likes") @PostMapping("/post/likes")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public void likeCrew(@RequestParam Long id){ public void likeCrew(@RequestParam Long id){
try { try {
postService.likeCrew(id); postService.likeCrew(id);
} }
catch (Exception e){ catch (Exception e){
System.out.println("여기");
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found");
} }
} }
@DeleteMapping ("/post/likes") @DeleteMapping ("/post/likes")
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public void dislikeCrew(@RequestParam Long id){ public void dislikeCrew(@RequestParam Long id){
......
...@@ -6,7 +6,6 @@ import lombok.Data; ...@@ -6,7 +6,6 @@ import lombok.Data;
public class CommentDto { public class CommentDto {
private Long postId; private Long postId;
private String author;
private String text; private String text;
} }
...@@ -8,6 +8,8 @@ import umc.spring.post.data.entity.Post; ...@@ -8,6 +8,8 @@ import umc.spring.post.data.entity.Post;
import java.util.Date; import java.util.Date;
import static umc.spring.post.config.security.SecurityUtil.getCurrentMemberId;
@Data @Data
@Builder @Builder
public class CommentResDto { public class CommentResDto {
...@@ -20,6 +22,8 @@ public class CommentResDto { ...@@ -20,6 +22,8 @@ public class CommentResDto {
public static CommentResDto toDTO(Comment comment){ public static CommentResDto toDTO(Comment comment){
// 내가 쓴 글에 댓글이 있는경우는 쓴 사람의 토큰이 맞을경우만 가능하네..
return CommentResDto.builder() return CommentResDto.builder()
.id(comment.getId()) .id(comment.getId())
.postId(comment.getPostId()) .postId(comment.getPostId())
......
...@@ -32,11 +32,13 @@ public class PostResDto { ...@@ -32,11 +32,13 @@ public class PostResDto {
public static PostResDto toDTO(Post post){ public static PostResDto toDTO(Post post){
List<CommentResDto> resDtos = new ArrayList<>(); List<CommentResDto> resDtos = new ArrayList<>();
post.getComments().forEach(comment -> { post.getComments().forEach(comment -> {
CommentResDto dto = CommentResDto.toDTO(comment); CommentResDto dto = CommentResDto.toDTO(comment);
resDtos.add(dto); resDtos.add(dto);
}); });
UserInfoDto userInfoDto; UserInfoDto userInfoDto;
boolean flag=false; boolean flag=false;
try { try {
......
...@@ -29,9 +29,6 @@ public class Post{ ...@@ -29,9 +29,6 @@ public class Post{
@Column(nullable = false) @Column(nullable = false)
private String body; private String body;
@Column(nullable = false)
private int likeCount;
@Column(nullable = false) @Column(nullable = false)
private String author; private String author;
......
...@@ -38,9 +38,9 @@ public class User implements UserDetails { ...@@ -38,9 +38,9 @@ public class User implements UserDetails {
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private Role role = Role.USER; private Role role = Role.USER;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER, // @OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER,
orphanRemoval = true) // orphanRemoval = true)
private List<LikeData> likes = new ArrayList<>(); // private List<LikeData> likes = new ArrayList<>();
@Override @Override
public Collection<? extends GrantedAuthority> getAuthorities() { public Collection<? extends GrantedAuthority> getAuthorities() {
......
...@@ -46,6 +46,7 @@ public class PostServiceImpl implements PostService{ ...@@ -46,6 +46,7 @@ public class PostServiceImpl implements PostService{
@Override @Override
public PostResDto upload(PostDto postDto){ public PostResDto upload(PostDto postDto){
UserInfoDto userInfoDto = getCurrentMemberId(); UserInfoDto userInfoDto = getCurrentMemberId();
Post post = new Post(); Post post = new Post();
setPost(postDto, post); setPost(postDto, post);
...@@ -82,14 +83,17 @@ public class PostServiceImpl implements PostService{ ...@@ -82,14 +83,17 @@ public class PostServiceImpl implements PostService{
Optional<Post> byId = postRepository.findById(id); Optional<Post> byId = postRepository.findById(id);
if(byId.isPresent()){ if(byId.isPresent()){
UserInfoDto userInfoDto; UserInfoDto userInfoDto;
Post post = byId.get();
try { try {
userInfoDto = getCurrentMemberId(); userInfoDto = getCurrentMemberId();
} }
catch(Exception e){ catch(Exception e){
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found");
} }
if(Objects.equals(byId.get().getUserId(), userInfoDto.getUserId())){ if(Objects.equals(post.getUserId(), userInfoDto.getUserId())){
postRepository.deleteById(id);
postRepository.deleteById(post.getId());
return true; return true;
} }
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized: You do not have permission to delete this post."); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized: You do not have permission to delete this post.");
...@@ -127,13 +131,15 @@ public class PostServiceImpl implements PostService{ ...@@ -127,13 +131,15 @@ public class PostServiceImpl implements PostService{
@Override @Override
public void likeCrew(Long id) { public void likeCrew(Long id) {
UserInfoDto userInfoDto = getCurrentMemberId(); UserInfoDto userInfoDto = getCurrentMemberId();
Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다."));
User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow(); User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow();
Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user); Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user);
if(byPostAndUser.isEmpty()){ if(byPostAndUser.isEmpty()){
LikeData likeData = new LikeData(); LikeData likeData = new LikeData();
post.getLikes().add(likeData); post.getLikes().add(likeData);
user.getLikes().add(likeData);
likeData.setUser(user); likeData.setUser(user);
likeData.setPost(post); likeData.setPost(post);
likeRepository.save(likeData); likeRepository.save(likeData);
...@@ -149,16 +155,15 @@ public class PostServiceImpl implements PostService{ ...@@ -149,16 +155,15 @@ public class PostServiceImpl implements PostService{
Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다."));
User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow(); User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow();
Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user); Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user);
if(byPostAndUser.isPresent()){ if(byPostAndUser.isPresent()){
post.getLikes().removeIf(data -> post.getLikes().removeIf(data ->
data.getPost().equals(post) data.getUser().equals(user)
); );
postRepository.save(post); postRepository.save(post);
user.getLikes().removeIf(data ->
data.getUser().equals(user)
);
userRepository.save(user);
} }
else{ else{
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "좋아요를 누른 적이 없습니다"); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "좋아요를 누른 적이 없습니다");
...@@ -180,10 +185,11 @@ public class PostServiceImpl implements PostService{ ...@@ -180,10 +185,11 @@ public class PostServiceImpl implements PostService{
@Override @Override
public void addComment(CommentDto commentDto){ public void addComment(CommentDto commentDto){
// 토큰 받은 유저가 우리 회원인지 // 토큰 받은 유저가 게시글 회원인지가 아니라!!! 우리 회원인지확인..
UserInfoDto userInfoDto = getCurrentMemberId(); UserInfoDto userInfoDto = getCurrentMemberId();
System.out.println("통과");
Comment comment = setComment(commentDto); Comment comment = setComment(commentDto);
comment.setAuthor(userInfoDto.getUserName());
comment.setUserId(userInfoDto.getUserId()); comment.setUserId(userInfoDto.getUserId());
commentRepository.save(comment); commentRepository.save(comment);
} }
...@@ -199,6 +205,7 @@ public class PostServiceImpl implements PostService{ ...@@ -199,6 +205,7 @@ public class PostServiceImpl implements PostService{
catch(Exception e){ catch(Exception e){
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found");
} }
Comment comment = option.get(); Comment comment = option.get();
if(Objects.equals(userInfoDto.getUserId(), comment.getUserId())){ if(Objects.equals(userInfoDto.getUserId(), comment.getUserId())){
Post post = comment.getPost(); Post post = comment.getPost();
...@@ -208,6 +215,7 @@ public class PostServiceImpl implements PostService{ ...@@ -208,6 +215,7 @@ public class PostServiceImpl implements PostService{
); );
postRepository.save(post); postRepository.save(post);
} }
return true; return true;
} }
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized: You do not have permission to delete this comment."); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized: You do not have permission to delete this comment.");
...@@ -221,8 +229,8 @@ public class PostServiceImpl implements PostService{ ...@@ -221,8 +229,8 @@ public class PostServiceImpl implements PostService{
Comment comment = new Comment(); Comment comment = new Comment();
Post post = postRepository.findById(commentDto.getPostId()).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); Post post = postRepository.findById(commentDto.getPostId()).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다."));
post.getComments().add(comment); post.getComments().add(comment);
comment.setPostId(commentDto.getPostId()); comment.setPostId(commentDto.getPostId());
comment.setAuthor(commentDto.getAuthor());
comment.setText(commentDto.getText()); comment.setText(commentDto.getText());
comment.setTimestamp(new Date()); comment.setTimestamp(new Date());
return comment; return comment;
...@@ -232,7 +240,6 @@ public class PostServiceImpl implements PostService{ ...@@ -232,7 +240,6 @@ public class PostServiceImpl implements PostService{
private static void setPost(PostDto postDto, Post post) { private static void setPost(PostDto postDto, Post post) {
post.setTitle(postDto.getTitle()); post.setTitle(postDto.getTitle());
post.setBody(postDto.getBody()); post.setBody(postDto.getBody());
post.setLikeCount(0);
post.setS3File(postDto.getS3File()); post.setS3File(postDto.getS3File());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment