Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor #153] 자유게시판, 성지순례 인증글 response DTO, status 수정 #154

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package com.favoriteplace.app.controller;

import com.favoriteplace.app.domain.Member;
import com.favoriteplace.app.dto.community.*;
import com.favoriteplace.app.repository.MemberRepository;
import com.favoriteplace.app.dto.community.CommentRequestDto;
import com.favoriteplace.app.dto.community.CommentResponseDto;
import com.favoriteplace.app.dto.community.GuestBookResponseDto;
import com.favoriteplace.app.dto.community.PostResponseDto;
import com.favoriteplace.app.service.community.CommentCommandService;
import com.favoriteplace.app.service.community.CommentQueryService;
import com.favoriteplace.global.util.SecurityUtil;
import com.google.api.Http;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/posts/guestbooks")
@RequiredArgsConstructor
public class GuestBookCommentController {
private final SecurityUtil securityUtil;
private final CommentQueryService commentQueryService;
private final CommentCommandService commentCommandService;
private final MemberRepository memberRepository;

@GetMapping("/my-comments")
public ResponseEntity<GuestBookResponseDto.MyGuestBookCommentDto> getMyComments(
Expand All @@ -46,52 +45,53 @@ public ResponseEntity<CommentResponseDto.CommentDto> getGuestBookComments(
}

@PostMapping("/{guestbook_id}/comments")
public ResponseEntity<PostResponseDto.SuccessResponseDto> createGuestBookComment(
public ResponseEntity<PostResponseDto.CommentSuccessResponseDto> createGuestBookComment(
@PathVariable("guestbook_id") Long guestbookId,
@RequestBody CommentRequestDto.CreateComment guestBookCommentDto
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
Long commentId = commentCommandService.createGuestBookComment(member, guestbookId, guestBookCommentDto);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().commentId(commentId).message("댓글이 성공적으로 등록했습니다.").build(),
PostResponseDto.CommentSuccessResponseDto.builder().commentId(commentId).build(),
HttpStatus.OK
);
}

@PostMapping("/{guestbook_id}/comments/{comment_id}/notification")
public ResponseEntity<?> sendGuestBookNotification(
public ResponseEntity<Void> sendGuestBookNotification(
@PathVariable("guestbook_id") Long guestbookId,
@PathVariable("comment_id") Long commentId
){
commentCommandService.sendGuestBookNotification(guestbookId, commentId);
return ResponseEntity.ok().build();
}

@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
Comment on lines +69 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 스웨거 문서화를 위해 추가해주신건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네! 해당 코드를 추가하지 않으면 코드에는 204로 설정해도, 스웨거에서는 200으로 뜨더라구요.

@PutMapping("/comments/{comment_id}")
public ResponseEntity<PostResponseDto.SuccessResponseDto> modifyGuestBookComment(
public ResponseEntity<Void> modifyGuestBookComment(
@PathVariable("comment_id") Long commentId,
@RequestBody CommentRequestDto.ModifyComment dto
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.modifyComment(member, commentId, dto.getContent());
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글 성공적으로 수정했습니다.").build(),
HttpStatus.OK
HttpStatus.NO_CONTENT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http status에 대한 이해 없이 개발을 하다보니 놓친 부분이었는데, 유리님이 코드 수정해주셔서 저도 다른 api를 웹 규약에 맞게 수정해야겠다는 생각이 들었네요. 감사합니다!

);
}

@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@DeleteMapping("/comments/{comment_id}")
public ResponseEntity<PostResponseDto.SuccessResponseDto> deleteGuestBookComment(
public ResponseEntity<Void> deleteGuestBookComment(
@PathVariable("comment_id") Long commentId
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.deleteComment(member, commentId);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("댓글 성공적으로 삭제했습니다.").build(),
HttpStatus.OK
HttpStatus.NO_CONTENT
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.favoriteplace.app.service.community.GuestBookQueryService;
import com.favoriteplace.app.service.community.LikedPostService;
import com.favoriteplace.global.util.SecurityUtil;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -80,47 +82,51 @@ public GuestBookResponseDto.DetailGuestBookDto getDetailGuestBook(
return guestBookQueryService.getDetailGuestBookInfo(guestBookId, member);
}

@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@PatchMapping("/{guestbook_id}")
public ResponseEntity<PostResponseDto.SuccessResponseDto> modifyGuestBook(
public ResponseEntity<Void> modifyGuestBook(
@PathVariable("guestbook_id") Long guestbookId,
@RequestPart GuestBookRequestDto.ModifyGuestBookDto data,
@RequestPart(required = false) List<MultipartFile> images
) throws IOException {
Member member = securityUtil.getUser();
guestBookCommandService.modifyGuestBook(member, guestbookId, data, images);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("성지순례 인증글을 성공적으로 수정했습니다.").build(),
HttpStatus.OK
HttpStatus.NO_CONTENT
);
}
Comment on lines 96 to 99
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spring은 ResponseEntity를 더 쉽게 생성하기 위한 빌더 메서드를 제공하고 있는데요!

취향 차이일 수도 있지만 해당 코드를 return ResponseEntity.noContent().build()로 작성할 수도 있을 것 같아요~!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오! 빌더 좋은 것 같아요!


@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@DeleteMapping("/{guestbook_id}")
public ResponseEntity<PostResponseDto.SuccessResponseDto> deleteGuestBook(
public ResponseEntity<Void> deleteGuestBook(
@PathVariable("guestbook_id") Long guestbookId
){
Member member = securityUtil.getUser();
guestBookCommandService.deleteGuestBook(member, guestbookId);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message("성지순례 인증글을 성공적으로 삭제했습니다.").build(),
HttpStatus.OK
HttpStatus.NO_CONTENT
);
}

@PostMapping("/{guestbook_id}/like")
public ResponseEntity<PostResponseDto.SuccessResponseDto> modifyGuestBookLike(
public ResponseEntity<PostResponseDto.LikeSuccessResponseDto> modifyGuestBookLike(
@PathVariable("guestbook_id") Long guestbookId
){
Member member = securityUtil.getUser();
String message = likedPostService.modifyGuestBookLike(member, guestbookId);
Long likedId = likedPostService.modifyGuestBookLike(member, guestbookId);
return new ResponseEntity<>(
PostResponseDto.SuccessResponseDto.builder().message(message).build(),
PostResponseDto.LikeSuccessResponseDto.builder().likedId(likedId).build(),
HttpStatus.OK
);
}
Comment on lines 115 to 125
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

message 대신, 생성된 likeId만 담아서 보내는 방향으로 리팩하신 이유가 궁금해요! ㅎㅎ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 API는 특정 게시글에 좋아요, 좋아요 취소를 제공하는 기능입니다! 기존의 방식에는 "좋아요를 눌렀습니다", "좋아요를 취소했습니다."를 message에 전달했는데, 해당 값은 프론트에서 유의미 하게 쓰이는 값이 아니라서 message는 제거를 했습니다. LikedId는 Post 행위인 만큼 생성된 자원의 id를 반환하고자 추가를 했는데, 사실 likedId를 사용하는 API가 없네요..허헣ㅎㅎ


// 성지순례 장소 방문 인증글 작성하기
@PostMapping("/{pilgrimage_id}")
public PostResponseDto.SuccessResponseDto postToPilgrimage(
public PostResponseDto.GuestBookIdResponseDto postToPilgrimage(
@PathVariable("pilgrimage_id") Long pilgrimageId,
@RequestPart GuestBookRequestDto.ModifyGuestBookDto data,
@RequestPart(required = false) List<MultipartFile> images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.favoriteplace.app.service.MyPageCommandService;
import com.favoriteplace.app.service.MyPageQueryService;
import com.favoriteplace.global.util.SecurityUtil;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -89,6 +91,9 @@ public MyPageDto.MyModifyBlockDto modifyMemberBlock(
}

//FCM token 등록 & 변경
@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@PatchMapping("/fcmToken")
public ResponseEntity<?> modifyFcmToken(
@Valid @RequestBody MyFcmTokenDto request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.favoriteplace.app.dto.NotificationResponseDto;
import com.favoriteplace.app.service.NotificationService;
import com.favoriteplace.global.util.SecurityUtil;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -30,16 +32,22 @@ public ResponseEntity<NotificationResponseDto> getAllNotification(
}

// 알림 한번에 다 읽음 처리
@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@PatchMapping()
public ResponseEntity<?> readAllNotification(){
public ResponseEntity<Void> readAllNotification(){
Member member = securityUtil.getUser();
notificationService.readAllNotification(member);
return ResponseEntity.noContent().build();
}

// 특정 알림 읽음 처리
@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@PatchMapping("/{notificationId}")
public ResponseEntity<?> readNotification(
public ResponseEntity<Void> readNotification(
@PathVariable Long notificationId
){
Member member = securityUtil.getUser();
Expand All @@ -48,8 +56,11 @@ public ResponseEntity<?> readNotification(
}

// 특정 알림 삭제
@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@DeleteMapping("/{notificationId}")
public ResponseEntity<?> deleteNotification(
public ResponseEntity<Void> deleteNotification(
@PathVariable Long notificationId
){
Member member = securityUtil.getUser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ public CommonResponseDto.PostResponseDto likeToRally(@PathVariable("rally_id")Lo

// 랠리 FCM 구독
@PostMapping("/{rally_id}/subscribe")
public ResponseEntity<?> subscribeRally(@PathVariable("rally_id") Long rallyId){
public ResponseEntity<Void> subscribeRally(@PathVariable("rally_id") Long rallyId){
Member member = securityUtil.getUser();
pilgrimageCommandService.subscribeRally(rallyId, member);
return ResponseEntity.ok().build();
}

// 랠리 FCM 구독 취소
@DeleteMapping("/{rally_id}/unsubscribe")
public ResponseEntity<?> unsubscribeRally(@PathVariable("rally_id") Long rallyId){
public ResponseEntity<Void> unsubscribeRally(@PathVariable("rally_id") Long rallyId){
Member member = securityUtil.getUser();
pilgrimageCommandService.unsubscribeRally(rallyId, member);
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
import com.favoriteplace.app.domain.Member;
import com.favoriteplace.app.dto.community.CommentRequestDto;
import com.favoriteplace.app.dto.community.CommentResponseDto;
import com.favoriteplace.app.dto.community.PostResponseDto;
import com.favoriteplace.app.repository.MemberRepository;
import com.favoriteplace.app.service.community.CommentCommandService;
import com.favoriteplace.app.service.community.CommentQueryService;
import com.favoriteplace.global.exception.ErrorCode;
import com.favoriteplace.global.exception.RestApiException;
import com.favoriteplace.global.util.SecurityUtil;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.favoriteplace.app.dto.community.PostResponseDto.*;
import static com.favoriteplace.app.dto.community.PostResponseDto.MyCommentDto;
import static com.favoriteplace.app.dto.community.PostResponseDto.CommentSuccessResponseDto;

@RestController
@RequestMapping("/posts/free")
Expand All @@ -28,15 +25,13 @@ public class PostCommentController {
private final SecurityUtil securityUtil;
private final CommentQueryService commentQueryService;
private final CommentCommandService commentCommandService;
private final MemberRepository memberRepository;

@GetMapping("/my-comments")
public ResponseEntity<MyCommentDto> getMyComments(
@RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "10") int size
){
//Member member = securityUtil.getUser();
Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
Member member = securityUtil.getUser();
return ResponseEntity.ok(commentQueryService.getMyPostComments(member, page, size));
}

Expand All @@ -52,52 +47,53 @@ public ResponseEntity<CommentResponseDto.CommentDto> getPostComments(
}

@PostMapping("/{post_id}/comments")
public ResponseEntity<SuccessResponseDto> createPostComment(
public ResponseEntity<CommentSuccessResponseDto> createPostComment(
@PathVariable("post_id") Long postId,
@RequestBody CommentRequestDto.CreateComment dto
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
Long commentId = commentCommandService.createPostComment(member, postId, dto);
return new ResponseEntity<>(
SuccessResponseDto.builder().commentId(commentId).message("댓글을 성공적으로 등록했습니다.").build(),
CommentSuccessResponseDto.builder().commentId(commentId).build(),
HttpStatus.OK
);
}

@PostMapping("/{post_id}/comments/{comment_id}/notification")
public ResponseEntity<?> sendPostNotification(
public ResponseEntity<Void> sendPostNotification(
@PathVariable("post_id") long postId,
@PathVariable("comment_id") long commentId
){
commentCommandService.sendPostNotification(postId, commentId);
return ResponseEntity.ok().build();
}

@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@PutMapping("/comments/{comment_id}")
public ResponseEntity<SuccessResponseDto> modifyPostComment(
public ResponseEntity<Void> modifyPostComment(
@PathVariable("comment_id") long commentId,
@RequestBody CommentRequestDto.ModifyComment dto
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.modifyComment(member, commentId, dto.getContent());
return new ResponseEntity<>(
SuccessResponseDto.builder().message("댓글을 성공적으로 수정했습니다.").build(),
HttpStatus.OK
HttpStatus.NO_CONTENT
);
}

@ApiResponses(value = {
@ApiResponse(responseCode = "204")
})
@DeleteMapping("/comments/{comment_id}")
public ResponseEntity<SuccessResponseDto> deletePostComment(
public ResponseEntity<CommentSuccessResponseDto> deletePostComment(
@PathVariable("comment_id") long commentId
){
Member member = securityUtil.getUser();
// Member member = memberRepository.findById(1L).orElseThrow(() -> new RestApiException(ErrorCode.USER_NOT_FOUND));
commentCommandService.deleteComment(member, commentId);
return new ResponseEntity<>(
SuccessResponseDto.builder().message("댓글을 성공적으로 삭제했습니다.").build(),
HttpStatus.OK
HttpStatus.NO_CONTENT
);
}

Expand Down
Loading
Loading