Skip to content

Commit

Permalink
Merge pull request #137 from Me1tingPot/feature/#132
Browse files Browse the repository at this point in the history
Feature/#132
  • Loading branch information
moonyaeyoon authored Jul 26, 2024
2 parents cd6d8b3 + 352bfc8 commit 02b10a7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,22 @@ public ResponseEntity<ResponseData<CommentsListResponse>> getCommentsList (@Curr
@RequestParam(required = false) Long cursor,
@RequestParam(defaultValue = "10") int pageSize){
try{
return ResponseData.toResponseEntity(ResponseCode.READ_COMMENTS_LIST_SUCCESS, commentService.getCommentsList(account,postId,cursor,pageSize));
return ResponseData.toResponseEntity(ResponseCode.READ_COMMENTS_LIST_SUCCESS, commentService.getCommentsList(account, postId, cursor,pageSize));
}catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.READ_COMMENT_FAIL, null);
}
}

@Operation (summary = "๋Œ“๊ธ€ ์‚ญ์ œํ•˜๊ธฐ")
@DeleteMapping("{commentId}")
public ResponseEntity<ResponseData> createComment( @CurrentUser Account account, @PathVariable Long commentId) {
try {
return ResponseData.toResponseEntity(commentService.deleteComment(commentId,account));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.COMMENT_DELETE_FAIL);
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public static CommentDetail from(Comment comment) {
return CommentDetail.builder()
.commentId(comment.getId())
.parentId(comment.getParent() != null ? comment.getParent().getId() : null)
.userId(comment.getAccount().getId())
.userId(comment.getAccount()!= null ? comment.getAccount().getId() : null)
.content(comment.getContent())
.name(comment.getAccount().getName())
.name(comment.getAccount() != null? comment.getAccount().getName() : null)
.isAnonymous(comment.getIsAnonymous())
.imageUrl(comment.getCommentImage() != null ? comment.getCommentImage().getImageUrl() : null)
.updatedAt(comment.getUpdatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.web.server.ResponseStatusException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -131,8 +131,10 @@ public ResponseCode updateComment(CommentCreateRequest updateCommentDTO, Account
//
// return CommentsListResponse.from(commentDetailDTOs,nextCursor,isLast);
// }

/* ๋Œ“๊ธ€ ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ */
@Transactional(readOnly = true)
public CommentsListResponse getCommentsList(Account account, Long postId, Long cursor, int pageSize) {
Post post = findPostById(postId);
List<CommentsListResponse.CommentDetail> commentDetailDTOs = new ArrayList<>();
int count = 0;
Long parentCursor = null;
Expand Down Expand Up @@ -192,7 +194,40 @@ public CommentsListResponse getCommentsList(Account account, Long postId, Long c
}


/* ๋Œ“๊ธ€ ์‚ญ์ œํ•˜๊ธฐ */
@Transactional
public ResponseCode deleteComment(Long commentId, Account account) {
Comment comment = findCommentById(commentId);

if (!comment.getAccount().getId().equals(account.getId())) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "์‚ญ์ œ ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.");
}
if (comment.getParent() == null) {
comment.setContent("์‚ญ์ œ๋œ ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค.");
comment.setAccount(null);
comment.setIsAnonymous(true);

if (comment.getCommentImage() != null) {
CommentImage commentImage = comment.getCommentImage();
comment.setCommentImage(null);
commentImageRepository.delete(commentImage);
}
// ์ž์‹ ๋Œ“๊ธ€์ด ๋‚จ์•„์žˆ์ง€ ์•Š์œผ๋ฉด ๋ถ€๋ชจ ๋Œ“๊ธ€๋„ ์‚ญ์ œ
if (comment.getChildren().isEmpty()) {
commentRepository.delete(comment);
}


} else {
Comment parentComment = comment.getParent();
commentRepository.delete(comment);
parentComment.getChildren().remove(comment);
if (parentComment.getChildren().isEmpty()) {
commentRepository.delete(parentComment);
}
}
return ResponseCode.COMMENT_DELETE_SUCCESS;
}

private Comment findCommentById(Long commentId) {
return commentRepository.findById(commentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public void setContent(String content) {
this.content = content;
}

public void setAccount(Account account) {
this.account = account;
}

public void setIsAnonymous (Boolean isAnonymous) {
this.isAnonymous = isAnonymous;
}

public Comment getParent() { return parent;}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import meltingpot.server.domain.entity.comment.Comment;
import meltingpot.server.domain.entity.post.Post;
import meltingpot.server.domain.repository.CommentRepository;
import meltingpot.server.post.dto.PostDetailResponse;
import meltingpot.server.post.dto.PostsListResponse;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -51,7 +54,7 @@ public ResponseEntity<ResponseData> updatePost(@CurrentUser Account account,@Pat
@ApiResponse(responseCode = "OK", description = "์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ ์„ฑ๊ณต"),
@ApiResponse(responseCode = "NOT_FOUND", description = "์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค")
})
public ResponseEntity<ResponseData<PostsListResponse>> getPostList(@CurrentUser Account account, @PathVariable PostType postType , @RequestParam(name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) {
public ResponseEntity<ResponseData<PostsListResponse>> getPostList(@CurrentUser Account account, @PathVariable PostType postType , @RequestParam(required = false, name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) {
try {
return ResponseData.toResponseEntity(ResponseCode.POST_LIST_FETCH_SUCCESS, postService.getPostsList(account,postType, cursor, pageSize));
} catch (NoSuchElementException e) {
Expand All @@ -61,11 +64,23 @@ public ResponseEntity<ResponseData<PostsListResponse>> getPostList(@CurrentUser

@GetMapping("/{postId}")
@Operation(summary = "์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ๋‚ด์šฉ ์กฐํšŒ", description = "postId๋กœ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ๋‚ด์šฉ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
public ResponseEntity<ResponseData<PostDetailResponse>> getPostDetail(@CurrentUser Account account, @PathVariable Long postId, @RequestParam(name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) {
public ResponseEntity<ResponseData<PostDetailResponse>> getPostDetail(@CurrentUser Account account, @PathVariable Long postId, @RequestParam(required = false, name = "cursor") Long cursor, @RequestParam(name = "pageSize") Integer pageSize) {
try{
return ResponseData.toResponseEntity(ResponseCode.POST_DETAIL_FETCH_SUCCEESS,postService.getPostDetail(postId,cursor, pageSize));
}catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.POST_NOT_FOUND, null);
}
}


@DeleteMapping("/{postId}")
@Operation(summary = "์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ์‚ญ์ œ", description = "postId๋กœ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ธ€ ์‚ญ์ œ")
public ResponseEntity<ResponseData> deletePost(@CurrentUser Account account,@PathVariable Long postId) {
try {
return ResponseData.toResponseEntity(postService.deletePost(postId, account));
} catch (NoSuchElementException e) {
return ResponseData.toResponseEntity(ResponseCode.POST_DELETE_FAIL);
}
}

}
21 changes: 21 additions & 0 deletions src/main/java/meltingpot/server/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ public PostsListResponse getPostsList(Account account, PostType postType, Long
return PostsListResponse.from(posts, nextCursor, isLast);
}

/*post ์‚ญ์ œํ•˜๊ธฐ*/
public ResponseCode deletePost(Long postId, Account account){
Post post = findPostById(postId);
Account postAccount = findAccountById(post.getAccount().getId());

// ๊ฒŒ์‹œ๋ฌผ์— ์—ฐ๊ด€๋œ ์ด๋ฏธ์ง€ ์‚ญ์ œ
if (!post.getPostImages().isEmpty()) {
postImageRepository.deleteAll(post.getPostImages());
}

// ๊ฒŒ์‹œ๋ฌผ์— ์—ฐ๊ด€๋œ ๋Œ“๊ธ€ ์‚ญ์ œ
if (!post.getComments().isEmpty()) {
commentRepository.deleteAll(post.getComments());
}

// ๊ฒŒ์‹œ๋ฌผ ์‚ญ์ œ
postRepository.delete(post);

return ResponseCode.POST_DELETE_SUCCESS;

}


private Account findAccountById(Long accountId) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/meltingpot/server/util/ResponseCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public enum ResponseCode {
SOCKET_TOKEN_GET_SUCCESS(OK, "์†Œ์ผ“ ํ† ํฐ ์กฐํšŒ ์„ฑ๊ณต"),
POST_LIST_FETCH_SUCCESS(OK, "๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์„ฑ๊ณต"),
POST_DETAIL_FETCH_SUCCEESS(OK, "๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์„ฑ๊ณต"),
POST_DELETE_SUCCESS(OK, "๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œํ•˜๊ธฐ ์„ฑ๊ณต"),
CHAT_ROOM_USER_DELETE_SUCCESS(OK, "์ฑ„ํŒ…๋ฐฉ ๋‚˜๊ฐ€๊ธฐ ์„ฑ๊ณต"),

CHAT_ALARM_UPDATE_SUCCESS(OK, "์ฑ„ํŒ… ์•Œ๋ฆผ ์„ค์ • ์ˆ˜์ • ์„ฑ๊ณต"),
Expand All @@ -51,6 +52,7 @@ public enum ResponseCode {
UPDATE_POST_SUCCESS(OK,"๊ฒŒ์‹œ๋ฌผ ์ˆ˜์ • ์„ฑ๊ณต"),
UPDATE_COMMENT_SUCCESS(OK, "๋Œ“๊ธ€ ์ˆ˜์ • ์„ฑ๊ณต"),
READ_COMMENTS_LIST_SUCCESS(OK,"๋Œ“๊ธ€ ๋ชฉ๋ก ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์„ฑ๊ณต"),
COMMENT_DELETE_SUCCESS(OK, "๋Œ“๊ธ€ ์‚ญ์ œํ•˜๊ธฐ ์„ฑ๊ณต"),



Expand Down Expand Up @@ -90,9 +92,11 @@ public enum ResponseCode {
DESTINATION_NOT_VALID(BAD_REQUEST, "์ž˜๋ชป๋œ ๋ชฉ์ ์ง€๋กœ ์ ‘๊ทผํ•˜์˜€์Šต๋‹ˆ๋‹ค"),
COMMENT_CREATE_FAIL(BAD_REQUEST, "๋Œ“๊ธ€ ์ž‘์„ฑ ์‹คํŒจ "),
COMMENT_UPDATE_FAIL(BAD_REQUEST, "๋Œ“๊ธ€ ์ˆ˜์ • ์‹คํŒจ "),
COMMENT_DELETE_FAIL(BAD_REQUEST,"๋Œ“๊ธ€ ์‚ญ์ œ ์‹คํŒจ"),
READ_COMMENT_FAIL(BAD_REQUEST, "๋Œ“๊ธ€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์‹คํŒจ "),
POST_CREATE_FAIL(BAD_REQUEST, "๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑ ์‹คํŒจ"),
POST_UPDATE_FAIL(BAD_REQUEST,"๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์‹คํŒจ"),
POST_DELETE_FAIL(BAD_REQUEST,"๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์‹คํŒจ"),
REPORT_CREATE_FAIL(BAD_REQUEST, "์‹ ๊ณ  ์ž‘์„ฑ ์‹คํŒจ"),
AREA_FETCH_FAILED(BAD_REQUEST, "์ง€์—ญ ์กฐํšŒ ์‹คํŒจ"),
AREA_FETCH_FAILED_NOT_SERVICE_AREA(BAD_REQUEST, "ํ˜„์žฌ ์ขŒํ‘œ ์กฐํšŒ๋Š” ๊ตญ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค"),
Expand Down

0 comments on commit 02b10a7

Please sign in to comment.