-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from goormthon-Univ/feature/11
[Feat] 댓글 기능 구현
- Loading branch information
Showing
9 changed files
with
371 additions
and
11 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/com/beotkkot/qtudy/controller/comment/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.beotkkot.qtudy.controller.comment; | ||
|
||
import com.beotkkot.qtudy.dto.request.comments.CommentsRequestDto; | ||
import com.beotkkot.qtudy.dto.response.comments.CommentsResponseDto; | ||
import com.beotkkot.qtudy.dto.response.comments.GetCommentsAllResponseDto; | ||
import com.beotkkot.qtudy.dto.response.posts.PostsResponseDto; | ||
import com.beotkkot.qtudy.service.auth.AuthService; | ||
import com.beotkkot.qtudy.service.comments.CommentService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
private final AuthService authService; | ||
|
||
// 전체 댓글 조회 | ||
@GetMapping("/posts/comments/all") | ||
public ResponseEntity<? super GetCommentsAllResponseDto> getAllComment(@RequestParam("postId") Long postId, @RequestParam("page") int page) { | ||
ResponseEntity<? super GetCommentsAllResponseDto> response = commentService.getAllComment(postId, page); | ||
return response; | ||
} | ||
|
||
// 댓글 저장 | ||
@PostMapping("/posts/comments") | ||
public ResponseEntity<? super CommentsResponseDto> save(@RequestParam("postId") Long postId, @RequestHeader("Authorization") String token, @RequestBody CommentsRequestDto requestDto) { | ||
|
||
Long kakao_uid; | ||
try { | ||
kakao_uid = authService.getKakaoUserInfo(token).getId(); | ||
if (kakao_uid == null) { | ||
return PostsResponseDto.noAuthentication(); | ||
} | ||
} catch (Exception exception) { | ||
log.info(exception.getMessage()); | ||
return PostsResponseDto.databaseError(); | ||
} | ||
|
||
ResponseEntity<? super CommentsResponseDto> response = commentService.saveComment(postId, kakao_uid, requestDto); | ||
return response; | ||
} | ||
|
||
// 댓글 수정 | ||
@PatchMapping("/posts/comments") | ||
public ResponseEntity<? super CommentsResponseDto> patchComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, | ||
@RequestHeader("Authorization") String token, @RequestBody CommentsRequestDto requestDto) { | ||
Long kakao_uid; | ||
try { | ||
kakao_uid = authService.getKakaoUserInfo(token).getId(); | ||
if (kakao_uid == null) { | ||
return PostsResponseDto.noAuthentication(); | ||
} | ||
} catch (Exception exception) { | ||
log.info(exception.getMessage()); | ||
return PostsResponseDto.databaseError(); | ||
} | ||
|
||
ResponseEntity<? super CommentsResponseDto> response = commentService.patchComment(postId, commentId, kakao_uid, requestDto); | ||
return response; | ||
} | ||
|
||
// 댓글 삭제 | ||
@DeleteMapping("/posts/comments") | ||
public ResponseEntity<? super CommentsResponseDto> deleteComment(@RequestParam("postId") Long postId, @RequestParam("commentId") Long commentId, @RequestHeader("Authorization") String token) { | ||
Long kakao_uid; | ||
try { | ||
kakao_uid = authService.getKakaoUserInfo(token).getId(); | ||
if (kakao_uid == null) { | ||
return PostsResponseDto.noAuthentication(); | ||
} | ||
} catch (Exception exception) { | ||
log.info(exception.getMessage()); | ||
return PostsResponseDto.databaseError(); | ||
} | ||
|
||
ResponseEntity<? super CommentsResponseDto> response = commentService.deleteComment(postId, commentId, kakao_uid); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/beotkkot/qtudy/dto/object/CommentListItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.beotkkot.qtudy.dto.object; | ||
|
||
import com.beotkkot.qtudy.domain.comments.Comments; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CommentListItem { | ||
private Long commentId; | ||
private String content; | ||
private String createdAt; | ||
|
||
public static CommentListItem of(Comments comment) { | ||
|
||
return CommentListItem.builder() | ||
.commentId(comment.getCommentId()) | ||
.content(comment.getContent()) | ||
.createdAt(comment.getCreatedAt()) | ||
.build(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/beotkkot/qtudy/dto/request/comments/CommentsRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.beotkkot.qtudy.dto.request.comments; | ||
|
||
import com.beotkkot.qtudy.domain.comments.Comments; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.text.SimpleDateFormat; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
|
||
@NoArgsConstructor | ||
@Getter | ||
public class CommentsRequestDto { | ||
private String content; | ||
private Long userUid; | ||
|
||
public Comments toEntity(Long postId) { | ||
Date now = Date.from(Instant.now()); | ||
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
String writeDatetime = simpleDateFormat.format(now); | ||
|
||
return Comments.builder() | ||
.userUid(userUid) | ||
.postId(postId) | ||
.content(content) | ||
.createdAt(writeDatetime) | ||
.build(); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/beotkkot/qtudy/dto/response/comments/CommentsResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.beotkkot.qtudy.dto.response.comments; | ||
|
||
import com.beotkkot.qtudy.common.ResponseCode; | ||
import com.beotkkot.qtudy.common.ResponseMessage; | ||
import com.beotkkot.qtudy.dto.response.ResponseDto; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@Getter | ||
public class CommentsResponseDto extends ResponseDto{ | ||
public CommentsResponseDto() { | ||
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); | ||
} | ||
|
||
public static ResponseEntity<CommentsResponseDto> success() { | ||
CommentsResponseDto result = new CommentsResponseDto(); | ||
return ResponseEntity.status(HttpStatus.OK).body(result); | ||
} | ||
|
||
public static ResponseEntity<ResponseDto> notExistedPost(){ | ||
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); | ||
} | ||
|
||
public static ResponseEntity<ResponseDto> notExistedUser() { | ||
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_USER, ResponseMessage.NOT_EXISTED_USER); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); | ||
} | ||
|
||
public static ResponseEntity<? super CommentsResponseDto> notExistedComment() { | ||
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_COMMENT, ResponseMessage.NOT_EXISTED_COMMENT); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/beotkkot/qtudy/dto/response/comments/GetCommentsAllResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.beotkkot.qtudy.dto.response.comments; | ||
|
||
import com.beotkkot.qtudy.common.ResponseCode; | ||
import com.beotkkot.qtudy.common.ResponseMessage; | ||
import com.beotkkot.qtudy.dto.object.CommentListItem; | ||
import com.beotkkot.qtudy.dto.response.ResponseDto; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Getter | ||
public class GetCommentsAllResponseDto extends ResponseDto { | ||
private int page; | ||
private List<CommentListItem> commentList; | ||
|
||
public GetCommentsAllResponseDto(List<CommentListItem> commentListItem, int page) { | ||
super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); | ||
this.page = page; | ||
this.commentList = commentListItem; | ||
} | ||
|
||
public static ResponseEntity<GetCommentsAllResponseDto> success(List<CommentListItem> commentListItem, int page) { | ||
GetCommentsAllResponseDto result = new GetCommentsAllResponseDto(commentListItem, page); | ||
return ResponseEntity.status(HttpStatus.OK).body(result); | ||
} | ||
|
||
public static ResponseEntity<ResponseDto> notExistedPost(){ | ||
ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); | ||
} | ||
} | ||
|
||
|
||
|
13 changes: 10 additions & 3 deletions
13
src/main/java/com/beotkkot/qtudy/repository/comments/CommentsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
package com.beotkkot.qtudy.repository.comments; | ||
|
||
import com.beotkkot.qtudy.domain.comments.Comments; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
public interface CommentsRepository extends JpaRepository<Comments, Long> { | ||
// int countByPostId(Long postid); | ||
|
||
// @Transactional | ||
// void deleteByPostId(Long postId); | ||
|
||
Page<Comments> findAllByPostId(Long postId, Pageable pageable); | ||
|
||
int countByPostId(Long postid); | ||
|
||
@Transactional | ||
void deleteByPostId(Long postId); | ||
} |
Oops, something went wrong.