Skip to content
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 6 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kobaco/kobaco/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'

implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'

}

tasks.named('test') {
Expand Down
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);
}

}

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,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)
);
}
}
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setter는 제거해주라

public class CommentDTO {
Copy link
Member

Choose a reason for hiding this comment

The 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,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;
}

}

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();

}
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
Expand Up @@ -20,4 +20,8 @@ public static <T> ResponseEntity<T> fail(T body) {
public static <T> ResponseEntity<T> forbidden(T body) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(body);
}

public static <T> ResponseEntity<T> unauthorized(T body) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(body);
}
}
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> {

}
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);
}
}
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());
}
}
Loading
Loading