Skip to content

Commit

Permalink
댓글 권한 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
sleeg00 committed Mar 19, 2024
1 parent a44dae5 commit c753139
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 64 deletions.
30 changes: 20 additions & 10 deletions src/main/java/com/example/just/Controller/CommentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public ResponseEntity<ResponseCommentDto> createComment(@PathVariable Long post_
member_id = Long.valueOf(jwtProvider.getIdFromToken(token)); //토큰
comment = commentService.createComment(post_id, member_id, comment_dto);

} catch (NullPointerException e){
} catch (NullPointerException e) {
return ResponseEntity.status(404).body(new ResponseCommentDto("해당 부모 댓글 없음"));
} catch (RuntimeException e){
} catch (RuntimeException e) {
return ResponseEntity.status(404).body(new ResponseCommentDto("대댓글에는 대댓글 작성 불가"));
}
return ResponseEntity.ok(new ResponseCommentDto(comment,member_id,"입력 완료"));
return ResponseEntity.ok(new ResponseCommentDto(comment, member_id, "입력 완료"));
}

@ApiOperation(value = "댓글 조회 API")
Expand All @@ -102,20 +102,24 @@ public ResponseEntity<ResponsePostCommentDtoBefore> getCommentListBefore(@PathVa

@Operation(summary = "댓글 삭제 api", description = "대댓글까지 다 삭제되니 유의해야 함")
@DeleteMapping("/delete/comment/{post_id}/{comment_id}")
public ResponseEntity<String> deleteComment(@PathVariable Long post_id, @PathVariable Long comment_id) {
return commentService.deleteComment(post_id, comment_id);
public ResponseEntity<String> deleteComment(@PathVariable Long post_id, @PathVariable Long comment_id,
HttpServletRequest request) {
Long member_id = getAccessTokenOfMemberId(request);
return commentService.deleteComment(post_id, comment_id, member_id);
}


@ApiOperation(value = "댓글 수정")
@ApiResponses(value = {
@ApiResponse(responseCode = "200",description = "댓글 내용"),
@ApiResponse(responseCode = "400",description = "댓글이 존재하지 않습니다.\n게시물이 존재하지 않습니다.")
@ApiResponse(responseCode = "200", description = "댓글 내용"),
@ApiResponse(responseCode = "400", description = "댓글이 존재하지 않습니다.\n게시물이 존재하지 않습니다.")
})
@PutMapping("/put/comment/{post_id}/{comment_id}")
public ResponseEntity<String> putComment(@PathVariable Long post_id, @PathVariable Long comment_id,
@RequestBody PutCommentDto commentDto,
HttpServletRequest req) {
return commentService.putComment(post_id, comment_id, commentDto);
HttpServletRequest request) {
Long member_id = getAccessTokenOfMemberId(request);
return commentService.putComment(post_id, comment_id, commentDto, member_id);
}

@ApiOperation(value = "댓글 신고")
Expand Down Expand Up @@ -153,7 +157,7 @@ public ResponseEntity getMyComment(HttpServletRequest request) {
return commentService.getMyComment(member_id);
}

private String errorMassage(String error){
private String errorMassage(String error) {
return "{\n"
+ " \"comment_id\": \"\",\n"
+ " \"comment_create_time\": 0\n"
Expand All @@ -165,4 +169,10 @@ private String errorMassage(String error){
+ " \"message\": \"성공\"\n"
+ "}";
}

private Long getAccessTokenOfMemberId(HttpServletRequest request) {
String token = jwtProvider.getAccessToken(request);
Long member_id = Long.valueOf(jwtProvider.getIdFromToken(token)); //토큰
return member_id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.example.just.Dao.Member;
import com.example.just.Dao.Post;
import com.example.just.Dao.PostLike;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostLikeRepository extends JpaRepository<PostLike, Long> {
PostLike findByPostAndMember(Post post, Member member);

List<PostLike> findByMember(Member member);
}
45 changes: 28 additions & 17 deletions src/main/java/com/example/just/Service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Comment createComment(Long postId, Long member_id, CommentDto commentDto)
if (commentDto.getParent_comment_id() != null && commentDto.getParent_comment_id() != 0) {
parentComment = commentRepository.findById(commentDto.getParent_comment_id())
.orElseThrow(() -> new NullPointerException("부모 댓글이 존재하지 않습니다."));
if(parentComment.getParent() != null){
if (parentComment.getParent() != null) {
throw new RuntimeException("해당 댓글에는 대댓글을 작성할 수 없습니다.");
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public ResponsePostCommentDto getCommentList(Long postId, HttpServletRequest req
if (post != null && post.getComments() != null) {
comments = post.getComments().stream()
.filter(comment -> comment.getParent() == null)
.map(comment -> new ResponseCommentDto(comment, member_id,""))
.map(comment -> new ResponseCommentDto(comment, member_id, ""))
.collect(Collectors.toList());
}

Expand All @@ -137,32 +137,43 @@ public ResponsePostCommentDtoBefore getCommentListBefore(Long postId, HttpServle
return new ResponsePostCommentDtoBefore(post.getPostContent(), comments);
}

public ResponseEntity<String> deleteComment(Long postId, Long commentId) {
public ResponseEntity<String> deleteComment(Long postId, Long commentId, Long member_id) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new RuntimeException("게시물이 존재하지 않습니다."));
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new RuntimeException("부모 댓글이 존재하지 않습니다."));
comment.setChildren(null);
PostDocument postDocument = postContentESRespository.findById(postId).get();
postDocument.setCommentSize(postDocument.getCommentSize() - 1);
postContentESRespository.save(postDocument);
commentRepository.deleteById(commentId);
return ResponseEntity.ok("ok");
if (comment.getMember().getId() == member_id) {
comment.setChildren(null);
PostDocument postDocument = postContentESRespository.findById(postId).get();
postDocument.setCommentSize(postDocument.getCommentSize() - 1);
postContentESRespository.save(postDocument);
commentRepository.deleteById(commentId);
return ResponseEntity.ok("ok");
} else {
return ResponseEntity.status(403).body("권한이 없습니다.");
}
}

public ResponseEntity<String> putComment(Long postId, Long commentId,
PutCommentDto commentDto) {
PutCommentDto commentDto, Long member_id) {
Comment comment = commentRepository.findById(commentId).get();
if(comment == null) ResponseEntity.status(404).body("댓글이 존재하지 않습니다.");
if (comment == null) {
ResponseEntity.status(404).body("댓글이 존재하지 않습니다.");
}

Post post = postRepository.findById(postId).get();
if(post == null) ResponseEntity.status(404).body("게시물이 존재하지 않습니다.");

comment.setComment_content(commentDto.getComment_content());
// 업데이트된 댓글을 저장합니다.
commentRepository.save(comment);
if (post == null) {
ResponseEntity.status(404).body("게시물이 존재하지 않습니다.");
}
if (comment.getMember().getId() == member_id) {
comment.setComment_content(commentDto.getComment_content());
// 업데이트된 댓글을 저장합니다.
commentRepository.save(comment);

return ResponseEntity.ok(comment.getComment_content());
return ResponseEntity.ok(comment.getComment_content());
} else {
return ResponseEntity.status(403).body("권한이 없습니다.");
}
}

public ResponseEntity<String> blameComment(Long postId, Long commentId) {
Expand Down
89 changes: 52 additions & 37 deletions src/main/java/com/example/just/Service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,25 +180,29 @@ public ResponsePutPostDto putPost(Long member_id, PutPostDto postDto) throws Not
Long post_id = postDto.getPost_id();
Member member = checkMember(member_id);
Post checkPost = checkPost(post_id);
List<HashTagMap> hashTagMaps = checkPost.getHashTagMaps();
if (checkPost.getMember().getId().equals(member_id)) {
List<HashTagMap> hashTagMaps = checkPost.getHashTagMaps();

deleteHashTag(checkPost);
deleteHashTag(checkPost);

List<String> content = new ArrayList<>();
for (int i = 0; i < postDto.getPost_content().size(); i++) {
content.add(getConvertString(postDto.getPost_content().get(i)));
}
postDto.setPost_content(content);
List<String> content = new ArrayList<>();
for (int i = 0; i < postDto.getPost_content().size(); i++) {
content.add(getConvertString(postDto.getPost_content().get(i)));
}
postDto.setPost_content(content);

checkPost.changePost(postDto, member, checkPost);
checkPost.changePost(postDto, member, checkPost);

Post p = postRepository.save(checkPost);
saveHashTag(postDto.getHash_tage(), p);
Post p = postRepository.save(checkPost);
saveHashTag(postDto.getHash_tage(), p);

postContentESRespository.save(new PostDocument(checkPost));
postContentESRespository.save(new PostDocument(checkPost));

ResponsePutPostDto responsePutPostDto = new ResponsePutPostDto(p);
return responsePutPostDto;
ResponsePutPostDto responsePutPostDto = new ResponsePutPostDto(p);
return responsePutPostDto;
} else {
throw new NotFoundException();
}
}

private void deleteHashTag(Post post) {
Expand Down Expand Up @@ -361,27 +365,38 @@ public ResponseGetPost searchByCursorMember(String cursor, Long limit, Long memb
// 응답 코드 확인
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Response Code: " + statusCode);

// 응답 데이터 읽기
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println("Response: " + responseBody);//Response: [[3], [1], [1], [1]]
// 여기서 Python Server의 추천 시스템으로 Post_id들을 가져온다.
List<Long> postIds = new ArrayList<>();
for (int i = 2; i < responseBody.length(); i += 5) {
postIds.add(Long.parseLong(responseBody.substring(i, i + 1)));
if (statusCode == 200) {

// 응답 데이터 읽기
String responseBody = EntityUtils.toString(response.getEntity());

// 여기서 Python Server의 추천 시스템으로 Post_id들을 가져온다.
List<Long> postIds = new ArrayList<>();
for (int i = 2; i < responseBody.length(); i += 5) {
postIds.add(Long.parseLong(responseBody.substring(i, i + 1)));
}

results = query.select(post)
.from(post)
.where(post.post_id.notIn(viewedPostIds),
post.post_create_time.isNotNull(),
post.post_id.notIn(blames),
post.member.id.notIn(targetMembers),
post.post_id.in(postIds))
.orderBy(Expressions.numberTemplate(Double.class, "function('rand')").asc())
.limit(limit)
.fetch();
} else {
results = query.select(post)
.from(post)
.where(post.post_id.notIn(viewedPostIds),
post.post_create_time.isNotNull(),
post.post_id.notIn(blames),
post.member.id.notIn(targetMembers))
.orderBy(Expressions.numberTemplate(Double.class, "function('rand')").asc())
.limit(limit)
.fetch();
}
System.out.println(postIds);

results = query.select(post)
.from(post)
.where(post.post_id.notIn(viewedPostIds),
post.post_create_time.isNotNull(),
post.post_id.notIn(blames),
post.member.id.notIn(targetMembers),
post.post_id.in(postIds))
.orderBy(Expressions.numberTemplate(Double.class, "function('rand')").asc())
.limit(limit)
.fetch();
} else {

results = query.select(post)
Expand Down Expand Up @@ -468,10 +483,10 @@ public List<String> getLikeHashTag(Long member_id) {
QHashTagMap hashTagMap = QHashTagMap.hashTagMap;
QHashTag hashTag = QHashTag.hashTag;
Member member = checkMember(member_id);
//회웡니 쓴글 다 가져오기
// List<PostLike> postLikes = postLikeRepository.findByMemberId(member_id);
//회웡니 쓴글 다 가져오기

//회웡니 쓴 글 다 가져오기
List<Post> posts = member.getPosts();

//회원이 좋아요 한글의 해시태그 ID 가져오기
List<Long> hashTagMapsOfLike = query.select(hashTagMap.id)
.from(hashTagMap)
Expand All @@ -492,7 +507,7 @@ public List<String> getLikeHashTag(Long member_id) {
hashTagMaps.add(hashTagMapsOfLike.get(i));
}

//해시태그맵의 ID랑 겹치는거 뽑아오기
//좋아요 한 글 쓴 글의 해시태그맵의 ID랑 겹치는거 뽑아오기
List<String> hashTags = query.select(hashTag.name)
.from(hashTag)
.where(hashTag.id.in(hashTagMaps))
Expand Down

0 comments on commit c753139

Please sign in to comment.