-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: comment api 구현 #31
Changes from 3 commits
404f003
b4af8ce
ab49013
e8f5652
ef4686e
4230d6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package core.kobaco.application.comment.controller; | ||
|
||
import core.kobaco.application.comment.service.CommentLikeDetailResponse; | ||
import core.kobaco.application.comment.service.CommentService; | ||
import core.kobaco.application.comment.service.dto.CommentDetail; | ||
import core.kobaco.global.ApiResponse; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/comments") | ||
@RequiredArgsConstructor | ||
public class CommentController { | ||
private final CommentService commentService; | ||
|
||
@PostMapping("/create") | ||
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO, | ||
@RequestParam Long userId) { | ||
CommentDetail createdComment = commentService.createComment(commentDTO, userId); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(createdComment); | ||
} | ||
|
||
@GetMapping("/all") | ||
public ResponseEntity<List<CommentDetail>> getAllComments() { | ||
List<CommentDetail> comments = commentService.getAllComments(); | ||
return ApiResponse.success(comments); | ||
} | ||
|
||
@PatchMapping("/{commentId}/like") | ||
public void likeComment(@PathVariable Long commentId) { | ||
commentService.likeComment(commentId); | ||
} | ||
|
||
@GetMapping("/{commentId}/like") | ||
public CommentLikeDetailResponse getCommentLikeCount(@PathVariable Long commentId) { | ||
return commentService.getCommentLikeCount(commentId); | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core.kobaco.application.comment.service; | ||
|
||
import javax.swing.text.StyledEditorKit; | ||
public record CommentLikeDetailResponse (Boolean isLike,Long likeCount){ | ||
public static CommentLikeDetailResponse of(Boolean isLike, Long likeCount){ | ||
return new CommentLikeDetailResponse(isLike, likeCount); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core.kobaco.application.comment.service; | ||
|
||
public interface CommentLikeManager { | ||
Long getLikeCount(Long commentId); | ||
Boolean isLike(Long commentId); | ||
void like(Long commentId, Long userId); | ||
|
||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package core.kobaco.application.comment.service; | ||
|
||
import core.kobaco.application.comment.service.dto.CommentDetail; | ||
import core.kobaco.domain.comment.*; | ||
|
||
import core.kobaco.domain.user.UserUtils; | ||
|
||
import core.kobaco.infra.jpa.user.UserEntity; | ||
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 UserUtils userUtils; | ||
private final CommentLikeManager commentLikeManager; | ||
|
||
@Transactional | ||
public CommentDetail createComment(CommentDetail commentDetail, Long userId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. userId 받는 부분 final Long userId = userUtils.getRequestUserId();로 수정해주라 |
||
if (userId == null) { | ||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다."); | ||
} | ||
|
||
Comment comment = new Comment( | ||
null, | ||
commentDetail.getContent(), | ||
UserEntity.from(userId) | ||
); | ||
|
||
Comment savedCommentEntity = commentRepository.save(comment); | ||
return new CommentDetail( | ||
savedCommentEntity.getCommentId(), | ||
savedCommentEntity.getContent(), | ||
savedCommentEntity.getCommenter().getId() | ||
); | ||
|
||
} | ||
public List<CommentDetail> getAllComments() { | ||
List<Comment> comments = commentRepository.findAll(); | ||
return comments.stream() | ||
.map(comment -> new CommentDetail(comment.getCommentId(), comment.getContent(), comment.getCommenter().getId())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Transactional | ||
public void likeComment(Long commentId) { | ||
final Long userId = userUtils.getRequestUserId(); | ||
commentLikeManager.like(commentId, userId); | ||
} | ||
public CommentLikeDetailResponse getCommentLikeCount(final Long commentId) { | ||
return CommentLikeDetailResponse.of( | ||
commentLikeManager.isLike(commentId), | ||
commentLikeManager.getLikeCount(commentId) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package core.kobaco.application.comment.service.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. setter는 제거해주라 |
||
public class CommentDTO { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. class 대신에 record를 찾아서 바꿔주라 |
||
private Long id; | ||
private String content; | ||
private String userEmail; | ||
private int likes; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package core.kobaco.application.comment.service.dto; | ||
|
||
import core.kobaco.domain.user.User; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class CommentDetail { | ||
private Long commentId; | ||
private String content; | ||
private Long userId; | ||
|
||
public CommentDetail(Long commentId, String content, Long userId) { | ||
this.commentId = commentId; | ||
this.content = content; | ||
this.userId = userId; | ||
} | ||
public static CommentDetail of(Long commentId, String content, Long userId){ | ||
return new CommentDetail(commentId, content, userId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package core.kobaco.domain.comment; | ||
import core.kobaco.domain.user.User; | ||
import core.kobaco.infra.jpa.user.UserEntity; | ||
import lombok.*; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class Comment { | ||
private Long commentId; | ||
private String content; | ||
private UserEntity commenter; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. User를 바로가지기보다는 Long userId를 가지는거 어때? |
||
|
||
public static Comment of (Long commentId, String content, UserEntity commenter) { | ||
return new Comment(commentId, content, commenter); | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.kobaco.domain.comment; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface CommentRepository { | ||
Optional<Comment> findById(Long commentId); | ||
Comment save(Comment comment); | ||
List<Comment> findAll(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package core.kobaco.domain.commentLike.adapter; | ||
|
||
import core.kobaco.application.comment.service.CommentLikeManager; | ||
import core.kobaco.domain.commentLike.service.CommentLike; | ||
import core.kobaco.domain.commentLike.service.CommentLikeRepository; | ||
import core.kobaco.domain.user.UserUtils; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommentLikeManagerAdapter implements CommentLikeManager { | ||
private final UserUtils userUtils; | ||
private final CommentLikeRepository commentLikeRepository; | ||
|
||
@Override | ||
public Long getLikeCount(Long commentId) { | ||
return commentLikeRepository.countByCommentId(commentId); | ||
} | ||
|
||
@Override | ||
public Boolean isLike(Long commentId) { | ||
return commentLikeRepository.isLike(commentId, userUtils.getRequestUserId()); | ||
} | ||
|
||
@Override | ||
public void like(Long commentId, Long userId) { | ||
commentLikeRepository.findByCommentIdAndUserId(commentId, userId) | ||
.ifPresentOrElse( | ||
commentLikeRepository::delete, | ||
() -> commentLikeRepository.save(CommentLike.of(null, commentId, userId)) | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package core.kobaco.domain.commentLike.service; | ||
|
||
import lombok.Getter; | ||
|
||
|
||
@Getter | ||
public class CommentLike { | ||
private Long likeId; | ||
private Long commentId; | ||
private Long userId; | ||
|
||
private CommentLike(Long likeId, Long commentId, Long userId) { | ||
this.likeId = likeId; | ||
this.commentId = commentId; | ||
this.userId = userId; | ||
} | ||
|
||
public static CommentLike of(Long likeId, Long commentId, Long userId) { | ||
return new CommentLike(likeId, commentId, userId); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core.kobaco.domain.commentLike.service; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CommentLikeRepository { | ||
Long countByCommentId(Long commentId); | ||
|
||
Boolean isLike(Long commentId, Long userId); | ||
|
||
CommentLike save(CommentLike commentLike); | ||
|
||
void delete(CommentLike commentLike); | ||
|
||
Optional<CommentLike> findByCommentIdAndUserId(Long commentId, Long userId); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package core.kobaco.infra.comment; | ||
|
||
import core.kobaco.infra.jpa.comment.entity.CommentEntity; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
|
||
|
||
@Repository | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JPA 인터페이스를 만들때는 @repository는 안넣어둬두 돼 |
||
public interface CommentJpaRepository extends JpaRepository<CommentEntity,Long> { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package core.kobaco.infra.jpa.comment.entity; | ||
|
||
import core.kobaco.infra.jpa.user.UserEntity; | ||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Table(name = "comments") | ||
@Getter | ||
@NoArgsConstructor | ||
public class CommentEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String content; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private UserEntity user; | ||
|
||
private CommentEntity(String content, UserEntity user) { | ||
this.content = content; | ||
this.user = user; | ||
} | ||
public static CommentEntity of(String content, UserEntity user){ | ||
return new CommentEntity(content, user); | ||
} | ||
public static CommentEntity from(Long id){ | ||
return new CommentEntity(null, null); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
비횔성화해주라