Skip to content

Commit

Permalink
Merge pull request #109 from team9502/feature/comment
Browse files Browse the repository at this point in the history
fix: ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋Œ“๊ธ€ ์ „์ฒด ์กฐํšŒ api ์ƒ์„ฑ, ์ „์ฒด ์กฐํšŒ ์‘๋‹ต ์ˆ˜์ •
  • Loading branch information
EUNCHAEv1006 authored Jun 19, 2024
2 parents 0745dd4 + 82d3feb commit a1089f0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package team9502.sinchulgwinong.domain.comment.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
Expand All @@ -26,8 +27,8 @@ public class CommentController {

@PostMapping("/boards/{boardId}")
public ResponseEntity<GlobalApiResponse<CommentResponseDTO>> commentCreate(
@PathVariable Long boardId,
@RequestBody CommentRequestDTO commentRequestDTO,
@PathVariable("boardId") Long boardId,
@RequestBody @Valid CommentRequestDTO commentRequestDTO,
@AuthenticationPrincipal UserDetailsImpl userDetails) {

User user = (User) userDetails.getUser();
Expand All @@ -45,7 +46,7 @@ public ResponseEntity<GlobalApiResponse<CommentResponseDTO>> commentCreate(

@GetMapping("/boards/{boardId}")
public ResponseEntity<GlobalApiResponse<List<CommentResponseDTO>>> getAllComment(
@PathVariable Long boardId) {
@PathVariable("boardId") Long boardId) {

List<CommentResponseDTO> commentResponseDTOS = commentService.getAllComment(boardId);

Expand All @@ -60,10 +61,10 @@ public ResponseEntity<GlobalApiResponse<List<CommentResponseDTO>>> getAllComment

@PatchMapping("{commentId}/boards/{boardId}")
public ResponseEntity<GlobalApiResponse<CommentResponseDTO>> commentUpdate(
@PathVariable Long boardId,
@PathVariable Long commentId,
@PathVariable("boardId") Long boardId,
@PathVariable("commentId") Long commentId,
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody CommentRequestDTO commentRequestDTO) {
@RequestBody @Valid CommentRequestDTO commentRequestDTO) {

User user = (User) userDetails.getUser();

Expand All @@ -80,8 +81,8 @@ public ResponseEntity<GlobalApiResponse<CommentResponseDTO>> commentUpdate(

@DeleteMapping("/{commentId}/boards/{boardId}")
public ResponseEntity<GlobalApiResponse<Object>> deleteComment(
@PathVariable Long boardId,
@PathVariable Long commentId,
@PathVariable("boardId") Long boardId,
@PathVariable("commentId") Long commentId,
@AuthenticationPrincipal UserDetailsImpl userDetails) {

User user = (User) userDetails.getUser();
Expand All @@ -97,5 +98,22 @@ public ResponseEntity<GlobalApiResponse<Object>> deleteComment(
);
}

@GetMapping("/my-comments")
public ResponseEntity<GlobalApiResponse<List<CommentResponseDTO>>> getAllMyComment(
@AuthenticationPrincipal UserDetailsImpl userDetails) {

User user = (User) userDetails.getUser();

List<CommentResponseDTO> commentResponseDTOS = commentService.getAllMyComment(user);

return ResponseEntity.status(SUCCESS_READ_ALL_MY_COMMENT.getHttpStatus())
.body(
GlobalApiResponse.of(
SUCCESS_READ_ALL_MY_COMMENT.getMessage(),
commentResponseDTOS
)
);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class CommentResponseDTO {

private String commentContent;

private Long totalComments;

private LocalDateTime createdAt;

private LocalDateTime modifiedAt;
Expand All @@ -28,4 +30,14 @@ public CommentResponseDTO(Comment comment) {
this.createdAt = comment.getCreatedAt();
this.modifiedAt = comment.getModifiedAt();
}

public CommentResponseDTO(Comment comment, Long totalComments) {
this.commentId = comment.getCommentId();
this.userId = comment.getUser().getUserId();
this.boardId = comment.getBoard().getBoardId();
this.commentContent = comment.getCommentContent();
this.createdAt = comment.getCreatedAt();
this.modifiedAt = comment.getModifiedAt();
this.totalComments = totalComments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import team9502.sinchulgwinong.domain.user.entity.User;
import team9502.sinchulgwinong.global.entity.BaseTimeEntity;

import java.time.LocalDateTime;

@Entity
@Getter
public class Comment extends BaseTimeEntity {
Expand All @@ -28,5 +26,6 @@ public class Comment extends BaseTimeEntity {
private Board board;

@Setter
@Column(length = 300)
private String commentContent;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package team9502.sinchulgwinong.domain.comment.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Query;
import team9502.sinchulgwinong.domain.comment.entity.Comment;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findByBoard_BoardId(Long boardId);

List<Comment> findByUser_UserId(Long userId);

@Query("SELECT COUNT(c) FROM Comment c WHERE c.board.boardId = :boardId")
Long countCommentsByBoardId(Long boardId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
import team9502.sinchulgwinong.domain.comment.dto.response.CommentResponseDTO;
import team9502.sinchulgwinong.domain.comment.entity.Comment;
import team9502.sinchulgwinong.domain.comment.repository.CommentRepository;
import team9502.sinchulgwinong.domain.point.enums.SpType;
import team9502.sinchulgwinong.domain.point.service.PointService;
import team9502.sinchulgwinong.domain.user.entity.User;
import team9502.sinchulgwinong.global.exception.ApiException;
import team9502.sinchulgwinong.global.exception.ErrorCode;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -23,10 +24,13 @@ public class CommentService {

private final CommentRepository commentRepository;
private final BoardRepository boardRepository;
private final PointService pointService;

@Transactional
public CommentResponseDTO createComment(Long boardId, User user, CommentRequestDTO commentRequestDTO) {

validation(commentRequestDTO);

Board board = boardRepository.findById(boardId)
.orElseThrow(() -> new ApiException(ErrorCode.BOARD_NOT_FOUND));

Expand All @@ -38,23 +42,27 @@ public CommentResponseDTO createComment(Long boardId, User user, CommentRequestD

commentRepository.save(comment);

pointService.earnPoints(user, SpType.COMMENT);

return new CommentResponseDTO(comment);
}

@Transactional(readOnly = true)
public List<CommentResponseDTO> getAllComment(Long boardId) {

commentRepository.findByBoard_BoardId(boardId);
Long totalComments = commentRepository.countCommentsByBoardId(boardId);

return commentRepository.findByBoard_BoardId(boardId).stream()
.map(CommentResponseDTO::new)
.map(comment -> new CommentResponseDTO(comment, totalComments))
.collect(Collectors.toList());

}

@Transactional
public CommentResponseDTO updateComment(Long boardId, Long commentId, User user, CommentRequestDTO commentRequestDTO) {

validation(commentRequestDTO);

Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new ApiException(ErrorCode.COMMENT_NOT_FOUND));

Expand Down Expand Up @@ -90,4 +98,22 @@ public void deleteComment(Long boardId, Long commentId, User user) {
commentRepository.delete(comment);
}

@Transactional(readOnly = true)
public List<CommentResponseDTO> getAllMyComment(User user) {

return commentRepository.findByUser_UserId(user.getUserId()).stream()
.map(CommentResponseDTO::new)
.collect(Collectors.toList());
}

private void validation(CommentRequestDTO commentRequestDTO) {

if (commentRequestDTO.getCommentContent().isEmpty()) {
throw new ApiException(ErrorCode.CONTENT_REQUIRED);
}
if (commentRequestDTO.getCommentContent().length() > 300) {
throw new ApiException(ErrorCode.CONTENT_TOO_LONG);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public enum SuccessCode {
SUCCESS_READ_ALL_COMMENT(HttpStatus.OK, "๋Œ“๊ธ€ ์ „์ฒด ์กฐํšŒ ์„ฑ๊ณต"),
SUCCESS_UPDATE_COMMENT(HttpStatus.OK, "๋Œ“๊ธ€ ์—…๋ฐ์ดํŠธ ์„ฑ๊ณต"),
SUCCESS_DELETE_COMMENT(HttpStatus.OK, "๋Œ“๊ธ€ ์‚ญ์ œ ์„ฑ๊ณต"),
SUCCESS_READ_ALL_MY_COMMENT(HttpStatus.OK, "๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋Œ“๊ธ€ ์ „์ฒด ์กฐํšŒ ์„ฑ๊ณต"),

//Scrap
SUCCESS_CREATE_SCRAP(HttpStatus.CREATED, "์Šคํฌ๋žฉ ์ƒ์„ฑ ์„ฑ๊ณต"),
Expand Down

0 comments on commit a1089f0

Please sign in to comment.