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 3 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
3 changes: 2 additions & 1 deletion kobaco/kobaco/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-security'
Copy link
Member

Choose a reason for hiding this comment

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

비횔성화해주라

testImplementation 'org.springframework.boot:spring-boot-starter-test'
// testImplementation 'org.springframework.security:spring-security-test'
runtimeOnly 'com.mysql:mysql-connector-j'
Expand All @@ -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,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) {
Copy link
Member

Choose a reason for hiding this comment

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

Choose a reason for hiding this comment

The 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
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,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
Copy link
Member

Choose a reason for hiding this comment

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