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 030ac5ff3585ea81106b0cab6178cf6037afd910..bf6cd95c7e74ab7ac357e9c8b6d07b401d17443f 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 c2b532ca330a941db183da1d34c781a619242d45..f06304886d5403efc83485507f392da9f1ef4456 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 3499c67839ae7e8b763d3ca06f19f10d6d3b844c..1eb2f286350465adcb64e37e9a39da190cea5154 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 ef005b5319526eee7d55e865baf06362f0b9c7b6..1e80c48fbba97c5ade45f4893ece20132f10e3c4 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 34bc73f08738958b99f4d90b462899c731cdbe7d..7c7721b4601300433ed882368356c5e84201a823 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 b475be375fe9ea550bdb1ea7776a7179424e578a..b257159b522bde67cf1a0bc38003fe17b06b73b9 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 06b3d16ff78fd19e008a4136b24a6344562401df..4810d7b459adaf5a672b5f4ed3bc2da3903a6519 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 403d3b430ffecd2961cce3ea105ced6e42f633fe..da1195071e0b874785e5b7397d9b9c3f25938b5c 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()); } }