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 5c226c9..6d8fefa 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,13 +39,18 @@ 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", "관련게시글을 찾을 수 없습니다."), + + + // Comment 관련 + COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMENT_001", "댓글 달기가 실패하였습니다."), + COMMENT_DELETE_FAILD(HttpStatus.NOT_FOUND, "COMMENT_002", "댓글 삭제를 실패하였습니다."), + - // Post 관련 - POST_NOT_FOUND(HttpStatus.NOT_FOUND, "POST_001", "존재하지 않는 게시글입니다"), //calendar - CALENDAR_NOT_FOUND(HttpStatus.NOT_FOUND,"CALENDAR_404", " 해당 기록을 찾으 수 없습니다.") - ; + CALENDAR_NOT_FOUND(HttpStatus.NOT_FOUND,"CALENDAR_404", " 해당 기록을 찾으 수 없습니다."); private final HttpStatus httpStatus; private final String code; 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 9747627..73fc54e 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,10 @@ public enum SuccessStatus implements BaseCode { //Post POST_DELETE_OK(HttpStatus.OK, "POST2001", "게시글이 성공적으로 삭제되었습니다."), + //Comment + COMMENT_POST_OK(HttpStatus.OK, "COMMENT2001", "댓글이 성공적으로 달렸습니다."), + COMMENT_DELETE_OK(HttpStatus.OK, "COMMENT2002", "댓글이 성공적으로 삭제되었습니다."), + //캘린더 ADD_CALENDAR_OK(HttpStatus.OK, "COMMON200", "달력에 기록이 되었습니다."), INQUERY_MONTH_CALENDAR_OK(HttpStatus.OK, "COMMON200", "달력을 불러왔습니다."), @@ -28,11 +32,12 @@ public enum SuccessStatus implements BaseCode { INQUERY_DATE_CALENDAR_OK(HttpStatus.OK,"COMMON200", "기록을 불러왔습니다."), DELETE_RECORD_OK(HttpStatus.OK, "COMMON200", "기록이 성공적으로 삭제되었습니다."), + //추천 RECOMMEND_OK(HttpStatus.OK, "COMMON200", "추천이 완료되었습니다."), - ; + ; private final HttpStatus httpStatus; private final String code; 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/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/repository/CommentRepository.java b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java new file mode 100644 index 0000000..520c021 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/repository/CommentRepository.java @@ -0,0 +1,8 @@ +package com.example.Midnight.Snacker.repository; + +import com.example.Midnight.Snacker.domain.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface CommentRepository extends JpaRepository { + +} 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 a27c2ab..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 @@ -58,4 +58,5 @@ public boolean isEnabled() { 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 new file mode 100644 index 0000000..8fb293e --- /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; +import com.example.Midnight.Snacker.domain.Member; + +public interface CommentService { + 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 new file mode 100644 index 0000000..f98b043 --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/service/CommentService/CommentServiceImpl.java @@ -0,0 +1,40 @@ +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; +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; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class CommentServiceImpl implements CommentService { + + private final PostRepository postRepository; + private final CommentRepository commentRepository; + + @Override + 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 + @Transactional + public void deleteComment(long id) { + Comment comment = commentRepository.findById(id).orElseThrow(() ->new CommentHandler(ErrorStatus.COMMENT_NOT_FOUND)); + commentRepository.delete(comment); + }//댓글 삭제 +} 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 new file mode 100644 index 0000000..4e724db --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/controller/CommentController.java @@ -0,0 +1,41 @@ +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; +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.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +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.of(SuccessStatus.COMMENT_POST_OK, CommentConverter.addCommentToResultDTO(newcomment)); + } + + @Operation(summary = "댓글 삭제 API", description = "댓글을 삭제합니다") + @DeleteMapping("/api/post/comment/{commentId}") + public ApiResponse deleteComment(@PathVariable("commentId") long commentId) { + commentService.deleteComment(commentId); + return ApiResponse.of(SuccessStatus.COMMENT_DELETE_OK, null); + } +} 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 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..2c80f8f --- /dev/null +++ b/Midnight-Snacker/src/main/java/com/example/Midnight/Snacker/web/dto/CommentDTO/CommentRequestDTO.java @@ -0,0 +1,19 @@ +package com.example.Midnight.Snacker.web.dto.CommentDTO; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalDateTime; + +public class CommentRequestDTO { + @Getter + @Setter + @NoArgsConstructor + @AllArgsConstructor + public static class addCommentRequestDTO { + String content; + } + +} 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; + } +}