From 7b25d16181396bb912645dd7dc121154eba84ac3 Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 02:38:18 +0900 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=C2=A0Feat:=20ErrorStatus,=20Succe?= =?UTF-8?q?ssStatus=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Midnight/Snacker/apiPayload/code/status/ErrorStatus.java | 3 +++ .../Snacker/apiPayload/code/status/SuccessStatus.java | 5 ++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java index 06cc5bd..7ba71f7 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java @@ -39,6 +39,9 @@ public enum ErrorStatus implements BaseErrorCode { // Post 관련 POST_NOT_FOUND(HttpStatus.NOT_FOUND, "POST_001", "존재하지 않는 게시글입니다"), + + // Comment 관련 + COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_001", "댓글 달기가 실패하였습니다.") ; private final HttpStatus httpStatus; diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java index 22bc428..8c526ab 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java @@ -18,7 +18,10 @@ public enum SuccessStatus implements BaseCode { MYPAGE_OK(HttpStatus.OK, "AUTH2004", "마이페이지 조회가 완료되었습니다."), //Post - POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다.") + POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다."), + + //Comment + COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다.") ; private final HttpStatus httpStatus; From 5cad4d5aefe6b648204f6afbef363ef076e9dc28 Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 02:57:43 +0900 Subject: [PATCH 2/8] Temporarily commit changes before merging --- .../Snacker/repository/CommentRepository.java | 9 ++++++ .../CommentService/CommentService.java | 9 ++++++ .../CommentService/CommentServiceImpl.java | 28 +++++++++++++++++++ .../web/controller/CommentController.java | 24 ++++++++++++++++ .../web/dto/CommentDTO/CommentRequestDTO.java | 17 +++++++++++ .../dto/CommentDTO/CommentResponseDTO.java | 17 +++++++++++ 6 files changed, 104 insertions(+) create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java new file mode 100644 index 0000000..5dfff95 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java @@ -0,0 +1,9 @@ +package com.example.Midnight.Snacker.repository; + +import com.example.Midnight.Snacker.domain.Comment; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.CrudRepository; + +public interface CommentRepository extends JpaRepository { + +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java new file mode 100644 index 0000000..b648dde --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java @@ -0,0 +1,9 @@ +package com.example.Midnight.Snacker.service.CommentService; + +import com.example.Midnight.Snacker.domain.Comment; + +public interface CommentService { + + Comment addComment(long postId); + Comment deleteComment(long commentId); +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java new file mode 100644 index 0000000..843b6c2 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java @@ -0,0 +1,28 @@ +package com.example.Midnight.Snacker.service.CommentService; + +import com.example.Midnight.Snacker.domain.Comment; +import com.example.Midnight.Snacker.domain.Post; +import com.example.Midnight.Snacker.repository.CommentRepository; +import com.example.Midnight.Snacker.repository.PostRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class CommentServiceImpl implements CommentService { + + private final PostRepository postRepository; + private final CommentRepository commentRepository; + + @Override + public Comment addComment(long postId) { + Post post = postRepository.findById(postId).get(); + Comment comment; + return null; + } //댓글 달기 + + @Override + public Comment deleteComment(long commentId) { + return null; + }//댓글 삭제 +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java new file mode 100644 index 0000000..a4b1069 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java @@ -0,0 +1,24 @@ +package com.example.Midnight.Snacker.web.controller; + +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class CommentController { + + @PostMapping("/api/post/{postId}/comment") + public String postComment(@PathVariable("postId") Integer postId) { + + return postComment(postId); + } + + @DeleteMapping("/api/post/{commentId}") + public String deleteComment(@PathVariable("commentId") Integer commentId) { + return deleteComment(commentId); + } + +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java new file mode 100644 index 0000000..9dcf963 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java @@ -0,0 +1,17 @@ +package com.example.Midnight.Snacker.web.dto.CommentDTO; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +public class CommentRequestDTO { + @Getter + @Setter + @NoArgsConstructor + @AllArgsConstructor + public static class addCommentRequestDTO { + long commentId; + } + +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java new file mode 100644 index 0000000..9cca0d7 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentResponseDTO.java @@ -0,0 +1,17 @@ +package com.example.Midnight.Snacker.web.dto.CommentDTO; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +public class CommentResponseDTO { + + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class CommentPostResponseDTO{ + long commentId; + } +} From 8ded189a1caaab50791f42f764f4659fa4560438 Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 03:01:19 +0900 Subject: [PATCH 3/8] Temporarily commit changes before merging --- .../Snacker/apiPayload/code/status/SuccessStatus.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java index 8c526ab..45b6a92 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java @@ -20,6 +20,14 @@ public enum SuccessStatus implements BaseCode { //Post POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다."), + + + + + + + + //Comment COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다.") ; From 372ce34c350db2787e373382992edc3daafeebca Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 03:02:49 +0900 Subject: [PATCH 4/8] Temporarily commit changes before merging --- .../Midnight/Snacker/apiPayload/code/status/SuccessStatus.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java index 45b6a92..0ef8ca8 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java @@ -20,7 +20,7 @@ public enum SuccessStatus implements BaseCode { //Post POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다."), - + From ed6e1aaf323eb2742478d49fc1fbd2abf41dc036 Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 03:04:36 +0900 Subject: [PATCH 5/8] Temporarily commit changes before merging --- .../apiPayload/code/status/SuccessStatus.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java index 0ef8ca8..ee89980 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java @@ -20,16 +20,14 @@ public enum SuccessStatus implements BaseCode { //Post POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다."), - - - - - - - - //Comment - COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다.") + COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다."), + + //캘린더 + ADD_CALENDAR_OK(HttpStatus.OK, "COMMON200", "달력에 기록이 되었습니다."), + INQUERY_MONTH_CALENDAR_OK(HttpStatus.OK, "COMMON200", "달력을 불러왔습니다."), + INQUERY_DATE_CALENDAR_OK(HttpStatus.OK,"COMMON200", "기록을 불러왔습니다."), + DELETE_RECORD_OK(HttpStatus.OK, "COMMON200", "기록이 성공적으로 삭제되었습니다."), ; private final HttpStatus httpStatus; From 2fb5b0e4cd10429bbed42d8009776d5b39a1a458 Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 03:46:00 +0900 Subject: [PATCH 6/8] =?UTF-8?q?=E2=9C=A8=C2=A0Feat:=20=EB=8C=93=EA=B8=80?= =?UTF-8?q?=20=EC=9E=91=EC=84=B1=20api=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Snacker/converter/CommentConverter.java | 16 +++++++++ .../security/principal/PrincipalDetails.java | 3 -- .../CommentService/CommentService.java | 4 +-- .../CommentService/CommentServiceImpl.java | 16 ++++++--- .../web/controller/CommentController.java | 33 +++++++++++++------ .../web/dto/CommentDTO/CommentRequestDTO.java | 4 ++- 6 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/converter/CommentConverter.java diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/converter/CommentConverter.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/converter/CommentConverter.java new file mode 100644 index 0000000..07a7c07 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/converter/CommentConverter.java @@ -0,0 +1,16 @@ +package com.example.Midnight.Snacker.converter; + +import com.example.Midnight.Snacker.domain.Comment; +import com.example.Midnight.Snacker.domain.Post; +import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentResponseDTO; +import com.example.Midnight.Snacker.web.dto.PostDTO.PostResponseDTO; + +public class CommentConverter { + + public static CommentResponseDTO.CommentPostResponseDTO addCommentToResultDTO(Comment comment){ + return CommentResponseDTO.CommentPostResponseDTO.builder() + .commentId(comment.getId()) + .build(); + } + +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java index eba8950..98e3d53 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java @@ -59,7 +59,4 @@ public boolean isEnabled() { return true; } - public Long getId() { - return member.getId(); - } } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java index b648dde..7a1a31c 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java @@ -1,9 +1,9 @@ package com.example.Midnight.Snacker.service.CommentService; import com.example.Midnight.Snacker.domain.Comment; +import com.example.Midnight.Snacker.domain.Member; public interface CommentService { - - Comment addComment(long postId); + Comment addComment(Member member,long postId, String content); Comment deleteComment(long commentId); } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java index 843b6c2..4485df4 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java @@ -1,6 +1,9 @@ package com.example.Midnight.Snacker.service.CommentService; +import com.example.Midnight.Snacker.apiPayload.code.status.ErrorStatus; +import com.example.Midnight.Snacker.apiPayload.exception.handler.PostHandler; import com.example.Midnight.Snacker.domain.Comment; +import com.example.Midnight.Snacker.domain.Member; import com.example.Midnight.Snacker.domain.Post; import com.example.Midnight.Snacker.repository.CommentRepository; import com.example.Midnight.Snacker.repository.PostRepository; @@ -15,10 +18,15 @@ public class CommentServiceImpl implements CommentService { private final CommentRepository commentRepository; @Override - public Comment addComment(long postId) { - Post post = postRepository.findById(postId).get(); - Comment comment; - return null; + public Comment addComment(Member member, long postId, String content) { + Post post = postRepository.findById(postId).orElseThrow(() ->new PostHandler(ErrorStatus.POST_NOT_FOUND)); + Comment newComment = Comment.builder() + .content(content) + .post(post) + .member(member) + .build(); + commentRepository.save(newComment); + return newComment; } //댓글 달기 @Override diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java index a4b1069..3ae9b80 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java @@ -1,24 +1,37 @@ package com.example.Midnight.Snacker.web.controller; +import com.example.Midnight.Snacker.apiPayload.ApiResponse; +import com.example.Midnight.Snacker.converter.CommentConverter; +import com.example.Midnight.Snacker.converter.PostConverter; +import com.example.Midnight.Snacker.domain.Comment; +import com.example.Midnight.Snacker.domain.Member; +import com.example.Midnight.Snacker.security.handler.annotation.AuthUser; +import com.example.Midnight.Snacker.service.CommentService.CommentService; +import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentRequestDTO; +import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentResponseDTO; +import com.example.Midnight.Snacker.web.dto.PostDTO.PostResponseDTO; +import io.swagger.v3.oas.annotations.Parameter; import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequiredArgsConstructor public class CommentController { - @PostMapping("/api/post/{postId}/comment") - public String postComment(@PathVariable("postId") Integer postId) { + private final CommentService commentService; - return postComment(postId); - } + @PostMapping("/api/post/{postId}/comment") + public ApiResponse postComment(@Parameter(name = "user", hidden = true) @AuthUser Member member, + @PathVariable("postId") Integer postId, + @RequestBody CommentRequestDTO.addCommentRequestDTO request) { + String content = request.getContent(); + Comment newcomment = commentService.addComment(member,postId,content); + return ApiResponse.onSuccess(CommentConverter.addCommentToResultDTO(newcomment)); + } // - @DeleteMapping("/api/post/{commentId}") + /*@DeleteMapping("/api/post/{commentId}") public String deleteComment(@PathVariable("commentId") Integer commentId) { return deleteComment(commentId); - } + }*/ } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java index 9dcf963..2c80f8f 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java @@ -5,13 +5,15 @@ import lombok.NoArgsConstructor; import lombok.Setter; +import java.time.LocalDateTime; + public class CommentRequestDTO { @Getter @Setter @NoArgsConstructor @AllArgsConstructor public static class addCommentRequestDTO { - long commentId; + String content; } } From 72e75e811a23fa4c34ba2573778333ae9567d9e2 Mon Sep 17 00:00:00 2001 From: Sojeong0430 Date: Sun, 12 Jan 2025 04:36:18 +0900 Subject: [PATCH 7/8] =?UTF-8?q?=E2=9C=A8=C2=A0Feat:=20=EB=8C=93=EA=B8=80,?= =?UTF-8?q?=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/misc.xml | 2 +- .../apiPayload/code/status/ErrorStatus.java | 8 ++++---- .../apiPayload/code/status/SuccessStatus.java | 1 + .../exception/handler/CommentHandler.java | 10 ++++++++++ .../Snacker/repository/CommentRepository.java | 1 - .../CommentService/CommentService.java | 2 +- .../CommentService/CommentServiceImpl.java | 8 +++++--- .../service/PostService/PostServiceImpl.java | 3 +-- .../web/controller/CommentController.java | 20 +++++++++++-------- .../web/controller/PostController.java | 6 +++--- 10 files changed, 38 insertions(+), 23 deletions(-) create mode 100644 Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/exception/handler/CommentHandler.java diff --git a/.idea/misc.xml b/.idea/misc.xml index e84ca07..fbe46a4 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java index 05b637d..3adefae 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/ErrorStatus.java @@ -39,12 +39,12 @@ public enum ErrorStatus implements BaseErrorCode { USER_EXISTS(HttpStatus.BAD_REQUEST, "USER_002", "이미 존재하는 아이디입니다."), USER_DELETE_FAILED(HttpStatus.NOT_FOUND, "USER_003", "회원 탈퇴에 실패했습니다."), - - // Post 관련 - POST_NOT_FOUND(HttpStatus.NOT_FOUND, "POST_001", "존재하지 않는 게시글입니다"), + // Post + POST_NOT_FOUND(HttpStatus.NOT_FOUND, "POST_001", "관련게시글을 찾을 수 없습니다."), // Comment 관련 - COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_001", "댓글 달기가 실패하였습니다.") + COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_001", "댓글 달기가 실패하였습니다."), + COMMENT_DELETE_FAILD(HttpStatus.NOT_FOUND, "COMMENT_002", "댓글 삭제를 실패하였습니다.") ; private final HttpStatus httpStatus; diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java index ee89980..4faf9ad 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/code/status/SuccessStatus.java @@ -22,6 +22,7 @@ public enum SuccessStatus implements BaseCode { //Comment COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다."), + COMMENT_DELETE_OK(HttpStatus.OK, "COMMENT2002", "댓글이 성공적으로 삭제되었습니다."), //캘린더 ADD_CALENDAR_OK(HttpStatus.OK, "COMMON200", "달력에 기록이 되었습니다."), diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/exception/handler/CommentHandler.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/exception/handler/CommentHandler.java new file mode 100644 index 0000000..07fb243 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/apiPayload/exception/handler/CommentHandler.java @@ -0,0 +1,10 @@ +package com.example.Midnight.Snacker.apiPayload.exception.handler; + +import com.example.Midnight.Snacker.apiPayload.code.BaseErrorCode; +import com.example.Midnight.Snacker.apiPayload.exception.GeneralException; + +public class CommentHandler extends GeneralException { + public CommentHandler(BaseErrorCode errorCode) { + super(errorCode); + } +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java index 5dfff95..520c021 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java @@ -2,7 +2,6 @@ import com.example.Midnight.Snacker.domain.Comment; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.repository.CrudRepository; public interface CommentRepository extends JpaRepository { diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java index 7a1a31c..cf9522f 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java @@ -5,5 +5,5 @@ public interface CommentService { Comment addComment(Member member,long postId, String content); - Comment deleteComment(long commentId); + void deleteComment(long commentId); } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java index 4485df4..df8ad45 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java @@ -1,6 +1,7 @@ package com.example.Midnight.Snacker.service.CommentService; import com.example.Midnight.Snacker.apiPayload.code.status.ErrorStatus; +import com.example.Midnight.Snacker.apiPayload.exception.handler.CommentHandler; import com.example.Midnight.Snacker.apiPayload.exception.handler.PostHandler; import com.example.Midnight.Snacker.domain.Comment; import com.example.Midnight.Snacker.domain.Member; @@ -30,7 +31,8 @@ public Comment addComment(Member member, long postId, String content) { } //댓글 달기 @Override - public Comment deleteComment(long commentId) { - return null; + public void deleteComment(long id) { + Comment comment = commentRepository.findById(id).orElseThrow(() ->new CommentHandler(ErrorStatus.COMMENT_NOT_FOUND)); + commentRepository.delete(comment); }//댓글 삭제 -} +} \ No newline at end of file diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java index fd5b0bf..c00c342 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/PostService/PostServiceImpl.java @@ -33,5 +33,4 @@ public void DeletePost(long id) { Post post = postRepository.findById(id).orElseThrow(() ->new PostHandler(ErrorStatus.POST_NOT_FOUND)); postRepository.delete(post); } // 게시글 삭제 - -} +} \ No newline at end of file diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java index 3ae9b80..344ac09 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java @@ -1,6 +1,7 @@ package com.example.Midnight.Snacker.web.controller; import com.example.Midnight.Snacker.apiPayload.ApiResponse; +import com.example.Midnight.Snacker.apiPayload.code.status.SuccessStatus; import com.example.Midnight.Snacker.converter.CommentConverter; import com.example.Midnight.Snacker.converter.PostConverter; import com.example.Midnight.Snacker.domain.Comment; @@ -10,6 +11,7 @@ import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentRequestDTO; import com.example.Midnight.Snacker.web.dto.CommentDTO.CommentResponseDTO; import com.example.Midnight.Snacker.web.dto.PostDTO.PostResponseDTO; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -20,18 +22,20 @@ public class CommentController { private final CommentService commentService; + @Operation(summary = "댓글 등록 API", description = "댓글을 등록합니다") @PostMapping("/api/post/{postId}/comment") public ApiResponse postComment(@Parameter(name = "user", hidden = true) @AuthUser Member member, @PathVariable("postId") Integer postId, @RequestBody CommentRequestDTO.addCommentRequestDTO request) { String content = request.getContent(); Comment newcomment = commentService.addComment(member,postId,content); - return ApiResponse.onSuccess(CommentConverter.addCommentToResultDTO(newcomment)); - } // + return ApiResponse.of(SuccessStatus.COMMENT_POST_OK, CommentConverter.addCommentToResultDTO(newcomment)); + } - /*@DeleteMapping("/api/post/{commentId}") - public String deleteComment(@PathVariable("commentId") Integer commentId) { - return deleteComment(commentId); - }*/ - -} + @Operation(summary = "댓글 삭제 API", description = "댓글을 삭제합니다") + @DeleteMapping("/api/post/{commentId}") + public ApiResponse deleteComment(@PathVariable("commentId") long commentId) { + commentService.deleteComment(commentId); + return ApiResponse.of(SuccessStatus.COMMENT_DELETE_OK, null); + } +} \ No newline at end of file diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java index adfcd39..375e999 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/PostController.java @@ -30,7 +30,7 @@ public class PostController { private final S3ImageService s3ImageService; @ResponseStatus(code = HttpStatus.CREATED) - @Operation(summary = "게시글 생성", description = "게시글 생성 API입니다") + @Operation(summary = "게시글 생성 API", description = "게시글을 생성합니다") @ApiResponses({@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON201", description="등록성공")}) @PostMapping(value = "/api/post/", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) ApiResponse AddPost(@Parameter(name = "user", hidden = true) @AuthUser Member member, @@ -45,10 +45,10 @@ ApiResponse AddPost(@Parameter(name = "user" return ApiResponse.onSuccess(PostConverter.addPostToResultDTO(post)); } + @Operation(summary = "게시글 삭제 API", description = "게시글을 삭제합니다") @DeleteMapping("/api/post/{postId}") public ApiResponse DeletePost(@PathVariable(name = "postId") long postId){ postService.DeletePost(postId); return ApiResponse.of(SuccessStatus.POST_DELETE_OK, null); } - -} +} \ No newline at end of file From 5a055d6718e79d2955accf1b7599adc386f4a6d8 Mon Sep 17 00:00:00 2001 From: Ssamssamukja <109636635+Ssamssamukja@users.noreply.github.com> Date: Sun, 12 Jan 2025 06:36:27 +0900 Subject: [PATCH 8/8] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor:=20uri?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Snacker/security/principal/PrincipalDetails.java | 4 ++++ .../Snacker/service/CommentService/CommentService.java | 4 ++-- .../Snacker/service/CommentService/CommentServiceImpl.java | 4 +++- .../Midnight/Snacker/web/controller/CommentController.java | 6 +++--- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java index f3de5d5..22fde59 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/security/principal/PrincipalDetails.java @@ -55,4 +55,8 @@ public boolean isEnabled() { return true; } + public Long getId() { + return member.getId(); + } + } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java index cf9522f..8fb293e 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentService.java @@ -4,6 +4,6 @@ import com.example.Midnight.Snacker.domain.Member; public interface CommentService { - Comment addComment(Member member,long postId, String content); - void deleteComment(long commentId); + Comment addComment(Member member, long postId, String content); + void deleteComment(long id); } diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java index df8ad45..f98b043 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java @@ -10,6 +10,7 @@ import com.example.Midnight.Snacker.repository.PostRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -31,8 +32,9 @@ public Comment addComment(Member member, long postId, String content) { } //댓글 달기 @Override + @Transactional public void deleteComment(long id) { Comment comment = commentRepository.findById(id).orElseThrow(() ->new CommentHandler(ErrorStatus.COMMENT_NOT_FOUND)); commentRepository.delete(comment); }//댓글 삭제 -} \ No newline at end of file +} diff --git a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java index 344ac09..4e724db 100644 --- a/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java @@ -33,9 +33,9 @@ public ApiResponse postComment(@Para } @Operation(summary = "댓글 삭제 API", description = "댓글을 삭제합니다") - @DeleteMapping("/api/post/{commentId}") - public ApiResponse deleteComment(@PathVariable("commentId") long commentId) { + @DeleteMapping("/api/post/comment/{commentId}") + public ApiResponse deleteComment(@PathVariable("commentId") long commentId) { commentService.deleteComment(commentId); return ApiResponse.of(SuccessStatus.COMMENT_DELETE_OK, null); } -} \ No newline at end of file +}