From a6cc6944dabc7f313ec9593504f6820f595a0ebb Mon Sep 17 00:00:00 2001 From: kkj6235 <kkj6235@ajou.ac.kr> Date: Sat, 25 Nov 2023 15:57:30 +0900 Subject: [PATCH] feat: Revise comment and like deletion functionality --- .../post/config/security/SecurityUtil.java | 5 --- .../post/controller/PostController.java | 3 +- .../umc/spring/post/data/dto/CommentDto.java | 1 - .../spring/post/data/dto/CommentResDto.java | 4 +++ .../umc/spring/post/data/dto/PostResDto.java | 2 ++ .../umc/spring/post/data/entity/Post.java | 3 -- .../umc/spring/post/data/entity/User.java | 6 ++-- .../spring/post/service/PostServiceImpl.java | 31 ++++++++++++------- 8 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/main/java/umc/spring/post/config/security/SecurityUtil.java b/src/main/java/umc/spring/post/config/security/SecurityUtil.java index 030ac5f..bf6cd95 100644 --- a/src/main/java/umc/spring/post/config/security/SecurityUtil.java +++ b/src/main/java/umc/spring/post/config/security/SecurityUtil.java @@ -1,15 +1,10 @@ package umc.spring.post.config.security; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Controller; 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 { diff --git a/src/main/java/umc/spring/post/controller/PostController.java b/src/main/java/umc/spring/post/controller/PostController.java index c2b532c..f063048 100644 --- a/src/main/java/umc/spring/post/controller/PostController.java +++ b/src/main/java/umc/spring/post/controller/PostController.java @@ -58,15 +58,14 @@ public class PostController { @PostMapping("/post/likes") @ResponseStatus(HttpStatus.OK) public void likeCrew(@RequestParam Long id){ - try { postService.likeCrew(id); } catch (Exception e){ - System.out.println("여기"); throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); } } + @DeleteMapping ("/post/likes") @ResponseStatus(HttpStatus.OK) public void dislikeCrew(@RequestParam Long id){ diff --git a/src/main/java/umc/spring/post/data/dto/CommentDto.java b/src/main/java/umc/spring/post/data/dto/CommentDto.java index 3499c67..1eb2f28 100644 --- a/src/main/java/umc/spring/post/data/dto/CommentDto.java +++ b/src/main/java/umc/spring/post/data/dto/CommentDto.java @@ -6,7 +6,6 @@ import lombok.Data; public class CommentDto { private Long postId; - private String author; private String text; } diff --git a/src/main/java/umc/spring/post/data/dto/CommentResDto.java b/src/main/java/umc/spring/post/data/dto/CommentResDto.java index ef005b5..1e80c48 100644 --- a/src/main/java/umc/spring/post/data/dto/CommentResDto.java +++ b/src/main/java/umc/spring/post/data/dto/CommentResDto.java @@ -8,6 +8,8 @@ import umc.spring.post.data.entity.Post; import java.util.Date; +import static umc.spring.post.config.security.SecurityUtil.getCurrentMemberId; + @Data @Builder public class CommentResDto { @@ -20,6 +22,8 @@ public class CommentResDto { public static CommentResDto toDTO(Comment comment){ + // 내가 쓴 글에 댓글이 있는경우는 쓴 사람의 토큰이 맞을경우만 가능하네.. + return CommentResDto.builder() .id(comment.getId()) .postId(comment.getPostId()) diff --git a/src/main/java/umc/spring/post/data/dto/PostResDto.java b/src/main/java/umc/spring/post/data/dto/PostResDto.java index 34bc73f..7c7721b 100644 --- a/src/main/java/umc/spring/post/data/dto/PostResDto.java +++ b/src/main/java/umc/spring/post/data/dto/PostResDto.java @@ -32,11 +32,13 @@ public class PostResDto { public static PostResDto toDTO(Post post){ List<CommentResDto> resDtos = new ArrayList<>(); + post.getComments().forEach(comment -> { CommentResDto dto = CommentResDto.toDTO(comment); resDtos.add(dto); }); UserInfoDto userInfoDto; + boolean flag=false; try { diff --git a/src/main/java/umc/spring/post/data/entity/Post.java b/src/main/java/umc/spring/post/data/entity/Post.java index b475be3..b257159 100644 --- a/src/main/java/umc/spring/post/data/entity/Post.java +++ b/src/main/java/umc/spring/post/data/entity/Post.java @@ -29,9 +29,6 @@ public class Post{ @Column(nullable = false) private String body; - @Column(nullable = false) - private int likeCount; - @Column(nullable = false) private String author; diff --git a/src/main/java/umc/spring/post/data/entity/User.java b/src/main/java/umc/spring/post/data/entity/User.java index 06b3d16..4810d7b 100644 --- a/src/main/java/umc/spring/post/data/entity/User.java +++ b/src/main/java/umc/spring/post/data/entity/User.java @@ -38,9 +38,9 @@ public class User implements UserDetails { @Enumerated(EnumType.STRING) private Role role = Role.USER; - @OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER, - orphanRemoval = true) - private List<LikeData> likes = new ArrayList<>(); +// @OneToMany(mappedBy = "user", cascade = CascadeType.ALL,fetch = FetchType.EAGER, +// orphanRemoval = true) +// private List<LikeData> likes = new ArrayList<>(); @Override public Collection<? extends GrantedAuthority> getAuthorities() { diff --git a/src/main/java/umc/spring/post/service/PostServiceImpl.java b/src/main/java/umc/spring/post/service/PostServiceImpl.java index 403d3b4..da11950 100644 --- a/src/main/java/umc/spring/post/service/PostServiceImpl.java +++ b/src/main/java/umc/spring/post/service/PostServiceImpl.java @@ -46,6 +46,7 @@ public class PostServiceImpl implements PostService{ @Override public PostResDto upload(PostDto postDto){ + UserInfoDto userInfoDto = getCurrentMemberId(); Post post = new Post(); setPost(postDto, post); @@ -82,14 +83,17 @@ public class PostServiceImpl implements PostService{ Optional<Post> byId = postRepository.findById(id); if(byId.isPresent()){ UserInfoDto userInfoDto; + Post post = byId.get(); try { userInfoDto = getCurrentMemberId(); } catch(Exception e){ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); } - if(Objects.equals(byId.get().getUserId(), userInfoDto.getUserId())){ - postRepository.deleteById(id); + if(Objects.equals(post.getUserId(), userInfoDto.getUserId())){ + + postRepository.deleteById(post.getId()); + return true; } throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized: You do not have permission to delete this post."); @@ -127,13 +131,15 @@ public class PostServiceImpl implements PostService{ @Override public void likeCrew(Long id) { UserInfoDto userInfoDto = getCurrentMemberId(); + Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); + User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow(); + Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user); if(byPostAndUser.isEmpty()){ LikeData likeData = new LikeData(); post.getLikes().add(likeData); - user.getLikes().add(likeData); likeData.setUser(user); likeData.setPost(post); likeRepository.save(likeData); @@ -149,16 +155,15 @@ public class PostServiceImpl implements PostService{ Post post = postRepository.findById(id).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); User user = userRepository.findByLoginId(userInfoDto.getLoginId()).orElseThrow(); Optional<LikeData> byPostAndUser = likeRepository.findByPostAndUser(post, user); + if(byPostAndUser.isPresent()){ + post.getLikes().removeIf(data -> - data.getPost().equals(post) + data.getUser().equals(user) ); + postRepository.save(post); - user.getLikes().removeIf(data -> - data.getUser().equals(user) - ); - userRepository.save(user); } else{ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "좋아요를 누른 적이 없습니다"); @@ -180,10 +185,11 @@ public class PostServiceImpl implements PostService{ @Override public void addComment(CommentDto commentDto){ - // 토큰 받은 유저가 우리 회원인지 - + // 토큰 받은 유저가 게시글 회원인지가 아니라!!! 우리 회원인지확인.. UserInfoDto userInfoDto = getCurrentMemberId(); + System.out.println("통과"); Comment comment = setComment(commentDto); + comment.setAuthor(userInfoDto.getUserName()); comment.setUserId(userInfoDto.getUserId()); commentRepository.save(comment); } @@ -199,6 +205,7 @@ public class PostServiceImpl implements PostService{ catch(Exception e){ throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "token not found"); } + Comment comment = option.get(); if(Objects.equals(userInfoDto.getUserId(), comment.getUserId())){ Post post = comment.getPost(); @@ -208,6 +215,7 @@ public class PostServiceImpl implements PostService{ ); postRepository.save(post); } + return true; } throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unauthorized: You do not have permission to delete this comment."); @@ -221,8 +229,8 @@ public class PostServiceImpl implements PostService{ Comment comment = new Comment(); Post post = postRepository.findById(commentDto.getPostId()).orElseThrow(() -> new RuntimeException("id가 존재하지 않습니다.")); post.getComments().add(comment); + comment.setPostId(commentDto.getPostId()); - comment.setAuthor(commentDto.getAuthor()); comment.setText(commentDto.getText()); comment.setTimestamp(new Date()); return comment; @@ -232,7 +240,6 @@ public class PostServiceImpl implements PostService{ private static void setPost(PostDto postDto, Post post) { post.setTitle(postDto.getTitle()); post.setBody(postDto.getBody()); - post.setLikeCount(0); post.setS3File(postDto.getS3File()); } } -- GitLab