Skip to content
Merged
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "달력을 불러왔습니다."),
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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<Comment, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ public boolean isEnabled() {
public Long getId() {
return member.getId();
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
}//댓글 삭제
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ public void DeletePost(long id) {
Post post = postRepository.findById(id).orElseThrow(() ->new PostHandler(ErrorStatus.POST_NOT_FOUND));
postRepository.delete(post);
} // 게시글 삭제

}
}
Original file line number Diff line number Diff line change
@@ -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<CommentResponseDTO. CommentPostResponseDTO> 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<Void> deleteComment(@PathVariable("commentId") long commentId) {
commentService.deleteComment(commentId);
return ApiResponse.of(SuccessStatus.COMMENT_DELETE_OK, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostResponseDTO.addPostResponseDTO> AddPost(@Parameter(name = "user", hidden = true) @AuthUser Member member,
Expand All @@ -45,10 +45,10 @@ ApiResponse<PostResponseDTO.addPostResponseDTO> AddPost(@Parameter(name = "user"
return ApiResponse.onSuccess(PostConverter.addPostToResultDTO(post));
}

@Operation(summary = "게시글 삭제 API", description = "게시글을 삭제합니다")
@DeleteMapping("/api/post/{postId}")
public ApiResponse<Void> DeletePost(@PathVariable(name = "postId") long postId){
postService.DeletePost(postId);
return ApiResponse.of(SuccessStatus.POST_DELETE_OK, null);
}

}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading