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;
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 {
......
......@@ -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){
......
......@@ -6,7 +6,6 @@ import lombok.Data;
public class CommentDto {
private Long postId;
private String author;
private String text;
}
......@@ -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())
......
......@@ -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 {
......
......@@ -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;
......
......@@ -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() {
......
......@@ -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());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment