Skip to content

Commit

Permalink
Merge pull request #52 from Kusitms-29th-Kobaco-A/chore/Comment
Browse files Browse the repository at this point in the history
fix: 댓글 생성, 조회 수정
  • Loading branch information
jihyo-j authored Mar 5, 2024
2 parents ce0c0cc + f1ed982 commit dd6a3ea
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import core.kobaco.application.comment.service.dto.CommentDetail;
import core.kobaco.global.ApiResponse;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,23 +19,28 @@
public class CommentController {
private final CommentService commentService;

@PostMapping("/create")
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO, @RequestParam Long advertiseId) {

@Operation(summary = "댓글 생성")
@PostMapping("/{advertiseId}")
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO, @PathVariable Long advertiseId) {
CommentDetail createdComment = commentService.createComment(commentDTO, advertiseId);
return ResponseEntity.status(HttpStatus.CREATED).body(createdComment);
}

@GetMapping("/all")
public ResponseEntity<List<CommentDetail>> getAllComments() {
List<CommentDetail> comments = commentService.getAllComments();
@Operation(summary = "댓글 조회")
@GetMapping("/{advertiseId}")
public ResponseEntity<List<CommentDetail>> getAllComments(@PathVariable Long advertiseId) {
List<CommentDetail> comments = commentService.getAllComments(advertiseId);
return ApiResponse.success(comments);
}

@Operation(summary = "댓글 좋아요")
@PatchMapping("/{commentId}/like")
public void likeComment(@PathVariable Long commentId) {
commentService.likeComment(commentId);
}

@Operation(summary = "댓글 좋아요 수")
@GetMapping("/{commentId}/like")
public CommentLikeDetailResponse getCommentLikeCount(@PathVariable Long commentId) {
return commentService.getCommentLikeCount(commentId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,34 @@

import core.kobaco.domain.user.UserUtils;

import core.kobaco.domain.user.service.UserReader;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;


import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class CommentService {
private final CommentRepository commentRepository;
private final UserReader userReader;
private final UserUtils userUtils;
private final CommentLikeManager commentLikeManager;

public CommentService(CommentRepository commentRepository, UserReader userReader, UserUtils userUtils, CommentLikeManager commentLikeManager) {
this.commentRepository = commentRepository;
this.userReader = userReader;
this.userUtils = userUtils;
this.commentLikeManager = commentLikeManager;
}

@Transactional
public CommentDetail createComment(CommentDetail commentDetail, Long advertiseId) {
final Long userId = userUtils.getRequestUserId();

if (userId == null) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다.");
}
Expand All @@ -38,20 +45,28 @@ public CommentDetail createComment(CommentDetail commentDetail, Long advertiseId
);

Comment savedCommentEntity = commentRepository.save(comment, advertiseId);

String userEmail = getUserEmail(userId);

return new CommentDetail(
savedCommentEntity.getCommentId(),
savedCommentEntity.getContent(),
savedCommentEntity.getCommenterId()
userEmail
);

}
public List<CommentDetail> getAllComments() {
List<Comment> comments = commentRepository.findAll();

public List<CommentDetail> getAllComments(Long advertiseId) {
List<Comment> comments = commentRepository.findAllByAdvertiseId(advertiseId);
return comments.stream()
.map(comment -> new CommentDetail(comment.getCommentId(), comment.getContent(), comment.getCommenterId()))
.map(comment -> new CommentDetail(comment.getCommentId(), comment.getContent(), getUserEmail(comment.getCommenterId())))
.collect(Collectors.toList());
}

private String getUserEmail(Long userId) {
return userReader.read(userId).getEmail();

}

@Transactional
public void likeComment(Long commentId) {
final Long userId = userUtils.getRequestUserId();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package core.kobaco.application.comment.service.dto;

import core.kobaco.domain.user.User;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -10,14 +8,15 @@
public class CommentDetail {
private Long commentId;
private String content;
private Long userId;
private String commenterEmail;

public CommentDetail(Long commentId, String content, Long userId) {
public CommentDetail(Long commentId, String content, String commenterEmail) {
this.commentId = commentId;
this.content = content;
this.userId = userId;
this.commenterEmail = commenterEmail;
}
public static CommentDetail of(Long commentId, String content, Long userId){
return new CommentDetail(commentId, content, userId);

public static CommentDetail of(Long commentId, String content, String commenterEmail) {
return new CommentDetail(commentId, content, commenterEmail);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
public interface CommentRepository {
Optional<Comment> findById(Long commentId);
Comment save(Comment comment, Long advertiseId);
List<Comment> findAll();
List<Comment> findAllByAdvertiseId(Long advertiseId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import core.kobaco.infra.jpa.comment.entity.CommentEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface CommentJpaRepository extends JpaRepository<CommentEntity,Long> {
List<CommentEntity> findAllByAdvertiseId(Long advertiseId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public Comment save(Comment comment, Long advertiseId) {
}

@Override
public List<Comment> findAll() {
List<CommentEntity> commentEntities = commentJpaRepository.findAll();
public List<Comment> findAllByAdvertiseId(Long advertiseId) {
List<CommentEntity> commentEntities = commentJpaRepository.findAllByAdvertiseId(advertiseId);
return commentMapper.toDomainList(commentEntities);
}

}

0 comments on commit dd6a3ea

Please sign in to comment.