-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
404f003
chore: comment api 구현
jihyo-j b4af8ce
Merge branch 'main' into chore/Comment
jihyo-j ab49013
chore: comment api 구현
jihyo-j e8f5652
fix: 전반적인 클래스 수정
jihyo-j ef4686e
fix: 댓글 생성할때 advertiseId로 연결 수정
jihyo-j 4230d6c
fix: advertise 외래키 연결
jihyo-j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
...co/kobaco/src/main/java/core/kobaco/application/comment/controller/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,44 @@ | ||
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 | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/comments") | ||
public class CommentController { | ||
private final CommentService commentService; | ||
|
||
@PostMapping("/create") | ||
public ResponseEntity<CommentDetail> createComment(@RequestBody CommentDetail commentDTO, @RequestParam 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(); | ||
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); | ||
} | ||
|
||
} | ||
|
9 changes: 9 additions & 0 deletions
9
...baco/src/main/java/core/kobaco/application/comment/service/CommentLikeDetailResponse.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,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); | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
kobaco/kobaco/src/main/java/core/kobaco/application/comment/service/CommentLikeManager.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,10 @@ | ||
package core.kobaco.application.comment.service; | ||
|
||
public interface CommentLikeManager { | ||
Long getLikeCount(Long commentId); | ||
Boolean isLike(Long commentId); | ||
void like(Long commentId, Long userId); | ||
|
||
} | ||
|
||
|
66 changes: 66 additions & 0 deletions
66
kobaco/kobaco/src/main/java/core/kobaco/application/comment/service/CommentService.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,66 @@ | ||
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 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 advertiseId) { | ||
final Long userId = userUtils.getRequestUserId(); | ||
if (userId == null) { | ||
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "사용자가 인증되지 않았습니다."); | ||
} | ||
|
||
Comment comment = new Comment( | ||
null, | ||
commentDetail.getContent(), | ||
userId, | ||
advertiseId | ||
); | ||
|
||
Comment savedCommentEntity = commentRepository.save(comment, advertiseId); | ||
return new CommentDetail( | ||
savedCommentEntity.getCommentId(), | ||
savedCommentEntity.getContent(), | ||
savedCommentEntity.getCommenterId() | ||
); | ||
|
||
} | ||
public List<CommentDetail> getAllComments() { | ||
List<Comment> comments = commentRepository.findAll(); | ||
return comments.stream() | ||
.map(comment -> new CommentDetail(comment.getCommentId(), comment.getContent(), comment.getCommenterId())) | ||
.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) | ||
); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
kobaco/kobaco/src/main/java/core/kobaco/application/comment/service/dto/CommentDTO.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,13 @@ | ||
package core.kobaco.application.comment.service.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@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; | ||
} |
23 changes: 23 additions & 0 deletions
23
kobaco/kobaco/src/main/java/core/kobaco/application/comment/service/dto/CommentDetail.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,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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
kobaco/kobaco/src/main/java/core/kobaco/domain/comment/Comment.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,21 @@ | ||
package core.kobaco.domain.comment; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class Comment { | ||
private Long commentId; | ||
private String content; | ||
private Long commenterId; | ||
private Long advertiseId; | ||
|
||
public Comment(Long commentId, String content, Long commenterId) { | ||
this.commentId = commentId; | ||
this.content = content; | ||
this.commenterId = commenterId; | ||
this.advertiseId = null; | ||
} | ||
|
||
} | ||
|
11 changes: 11 additions & 0 deletions
11
kobaco/kobaco/src/main/java/core/kobaco/domain/comment/CommentRepository.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,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, Long advertiseId); | ||
List<Comment> findAll(); | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...obaco/src/main/java/core/kobaco/domain/commentLike/adapter/CommentLikeManagerAdapter.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,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)) | ||
); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
kobaco/kobaco/src/main/java/core/kobaco/domain/commentLike/service/CommentLike.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,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); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...co/kobaco/src/main/java/core/kobaco/domain/commentLike/service/CommentLikeRepository.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,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); | ||
} | ||
|
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
9 changes: 9 additions & 0 deletions
9
kobaco/kobaco/src/main/java/core/kobaco/infra/comment/CommentJpaRepository.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,9 @@ | ||
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; | ||
|
||
public interface CommentJpaRepository extends JpaRepository<CommentEntity,Long> { | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
kobaco/kobaco/src/main/java/core/kobaco/infra/jpa/comment/entity/CommentEntity.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,42 @@ | ||
package core.kobaco.infra.jpa.comment.entity; | ||
|
||
import core.kobaco.domain.advertise.Advertisement; | ||
import core.kobaco.infra.jpa.advertisement.entity.AdvertisementEntity; | ||
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; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "advertise_id", nullable = false) | ||
private AdvertisementEntity advertise; | ||
|
||
private CommentEntity(String content, UserEntity user, AdvertisementEntity advertise) { | ||
this.content = content; | ||
this.user = user; | ||
this.advertise = advertise; | ||
} | ||
|
||
public static CommentEntity of(String content, UserEntity user, AdvertisementEntity advertise) { | ||
return new CommentEntity(content, user, advertise); | ||
} | ||
|
||
public static CommentEntity from(Long id) { | ||
return new CommentEntity(null, null, null); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
kobaco/kobaco/src/main/java/core/kobaco/infra/jpa/comment/repository/CommentMapper.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 core.kobaco.infra.jpa.comment.repository; | ||
|
||
import core.kobaco.domain.comment.Comment; | ||
|
||
import core.kobaco.infra.jpa.advertisement.entity.AdvertisementEntity; | ||
import core.kobaco.infra.jpa.comment.entity.CommentEntity; | ||
import core.kobaco.infra.jpa.user.UserEntity; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class CommentMapper { | ||
public CommentEntity toEntity(Comment comment, Long userId, Long advertiseId) { | ||
UserEntity userEntity = UserEntity.from(userId); | ||
AdvertisementEntity advertisementEntity = AdvertisementEntity.from(advertiseId); | ||
return CommentEntity.of(comment.getContent(), userEntity, advertisementEntity); | ||
} | ||
public Comment toDomain(CommentEntity commentEntity) { | ||
return new Comment( | ||
commentEntity.getId(), | ||
commentEntity.getContent(), | ||
commentEntity.getUser().getId() | ||
); | ||
} | ||
|
||
public List<Comment> toDomainList(List<CommentEntity> commentEntities) { | ||
return commentEntities.stream() | ||
.map(this::toDomain) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
setter는 제거해주라