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

[feature] 댓글 생성 #13

Merged
merged 5 commits into from
Dec 19, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package org.programmers.signalbuddy.domain.comment.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.programmers.signalbuddy.domain.comment.dto.CommentRequest;
import org.programmers.signalbuddy.domain.comment.service.CommentService;
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Comment API")
@RestController
@RequestMapping("/api/comments")
@RequiredArgsConstructor
public class CommentController {

private final CommentService commentService;

@Operation(summary = "댓글 작성")
@PostMapping("/write")
public ResponseEntity<Void> writeComment(@RequestBody @Valid CommentRequest request,
User user) { // TODO: 인자값에 User 객체는 나중에 변경해야 함!
commentService.writeComment(request, user);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.programmers.signalbuddy.domain.comment.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CommentRequest {

@NotNull(message = "피드백 ID 값은 필수입니다.")
private Long feedbackId;

@NotBlank(message = "댓글 내용은 비어있을 수 없습니다.")
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.util.Objects;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.programmers.signalbuddy.domain.basetime.BaseTimeEntity;
import org.programmers.signalbuddy.domain.comment.dto.CommentRequest;
import org.programmers.signalbuddy.domain.feedback.entity.Feedback;
import org.programmers.signalbuddy.domain.member.entity.Member;

@Entity(name = "comments")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@ToString
public class Comment extends BaseTimeEntity {

@Id
Expand All @@ -39,4 +36,11 @@ public class Comment extends BaseTimeEntity {
@ManyToOne
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@Builder(builderMethodName = "creator")
private Comment(CommentRequest request, Feedback feedback, Member member) {
this.content = Objects.requireNonNull(request.getContent());
this.feedback = Objects.requireNonNull(feedback);
this.member = Objects.requireNonNull(member);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
package org.programmers.signalbuddy.domain.comment.service;

import lombok.RequiredArgsConstructor;
import org.programmers.signalbuddy.domain.comment.dto.CommentRequest;
import org.programmers.signalbuddy.domain.comment.entity.Comment;
import org.programmers.signalbuddy.domain.comment.repository.CommentRepository;
import org.programmers.signalbuddy.domain.feedback.entity.Feedback;
import org.programmers.signalbuddy.domain.feedback.exception.FeedbackErrorCode;
import org.programmers.signalbuddy.domain.feedback.repository.FeedbackRepository;
import org.programmers.signalbuddy.domain.member.entity.Member;
import org.programmers.signalbuddy.domain.member.exception.MemberErrorCode;
import org.programmers.signalbuddy.domain.member.repository.MemberRepository;
import org.programmers.signalbuddy.global.exception.BusinessException;
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CommentService {

private final CommentRepository commentRepository;
private final MemberRepository memberRepository;
private final FeedbackRepository feedbackRepository;

// TODO: 인자값에 User 객체는 나중에 변경해야 함!
@Transactional
public void writeComment(CommentRequest request, User user) {
Member memeber = memberRepository.findById(Long.parseLong(user.getName()))
.orElseThrow(() -> new BusinessException(MemberErrorCode.NOT_FOUND_MEMBER));
Feedback feedback = feedbackRepository.findById(request.getFeedbackId())
.orElseThrow(() -> new BusinessException(FeedbackErrorCode.NOT_FOUND_FEEDBACK));

Comment comment = Comment.creator()
.request(request)
.feedback(feedback)
.member(memeber)
.build();

commentRepository.save(comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FeedbackController {
@PostMapping("/write")
public ResponseEntity<FeedbackResponse> writeFeedback(
@RequestBody @Valid FeedbackWriteRequest feedbackWriteRequest,
User user) { // 인자값에 User 객체는 나중에 변경해야 함!
User user) { // TODO: 인자값에 User 객체는 나중에 변경해야 함!
return ResponseEntity.status(HttpStatus.CREATED)
.body(feedbackService.writeFeedback(feedbackWriteRequest, user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class FeedbackService {
private final FeedbackRepository feedbackRepository;
private final MemberRepository memberRepository;

// 인자값에 User 객체는 나중에 변경해야 함!
// TODO: 인자값에 User 객체는 나중에 변경해야 함!
@Transactional
public FeedbackResponse writeFeedback(FeedbackWriteRequest request, User user) {
Member member = memberRepository.findById(Long.parseLong(user.getName()))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.programmers.signalbuddy.domain.comment.service;

import java.util.Optional;
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.programmers.signalbuddy.domain.comment.dto.CommentRequest;
import org.programmers.signalbuddy.domain.comment.entity.Comment;
import org.programmers.signalbuddy.domain.comment.repository.CommentRepository;
import org.programmers.signalbuddy.domain.feedback.dto.FeedbackWriteRequest;
import org.programmers.signalbuddy.domain.feedback.entity.Feedback;
import org.programmers.signalbuddy.domain.feedback.repository.FeedbackRepository;
import org.programmers.signalbuddy.domain.member.MemberStatus;
import org.programmers.signalbuddy.domain.member.entity.Member;
import org.programmers.signalbuddy.domain.member.repository.MemberRepository;
import org.programmers.signalbuddy.global.support.ServiceTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.transaction.annotation.Transactional;

@Transactional
class CommentServiceTest extends ServiceTest {

@Autowired
private CommentService commentService;

@Autowired
private CommentRepository commentRepository;

@Autowired
private MemberRepository memberRepository;

@Autowired
private FeedbackRepository feedbackRepository;

private Member member;
private Feedback feedback;

@BeforeEach
void setup() {
member = Member.builder()
.email("[email protected]")
.password("123456")
.role("USER")
.nickname("tester")
.memberStatus(MemberStatus.ACTIVITY)
.profileImageUrl("https://test-image.com/test-123131")
.build();
member = memberRepository.save(member);

String subject = "test subject";
String content = "test content";
FeedbackWriteRequest request = new FeedbackWriteRequest(subject, content);
feedback = feedbackRepository.save(Feedback.create(request, member));
}

@DisplayName("댓글 작성 성공")
@Test
void writeComment() {
// given
Long feedbackId = feedback.getFeedbackId();
String content = "test comment content";
CommentRequest request = new CommentRequest(feedbackId, content);
// TODO: User 객체는 나중에 변경해야 함!
User user = new User();
user.setName(member.getMemberId().toString());

// when
commentService.writeComment(request, user);

// then
Optional<Comment> actual = commentRepository.findById(1L);
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual).get().isNotNull();
softAssertions.assertThat(actual.get().getCommentId()).isNotNull();
softAssertions.assertThat(actual.get().getContent()).isEqualTo(content);
softAssertions.assertThat(actual.get().getMember().getMemberId())
.isEqualTo(Long.parseLong(user.getName()));
softAssertions.assertThat(actual.get().getFeedback().getFeedbackId())
.isEqualTo(feedbackId);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.boot.autoconfigure.security.SecurityProperties.User;
import org.springframework.transaction.annotation.Transactional;

@Transactional
class FeedbackServiceTest extends ServiceTest {

@Autowired
Expand All @@ -22,9 +23,11 @@ class FeedbackServiceTest extends ServiceTest {
@Autowired
private MemberRepository memberRepository;

private Member member;

@BeforeEach
void setup() {
Member member = Member.builder()
member = Member.builder()
.email("[email protected]")
.password("123456")
.role("USER")
Expand All @@ -33,27 +36,26 @@ void setup() {
.profileImageUrl("https://test-image.com/test-123131")
.build();

memberRepository.save(member);
member = memberRepository.save(member);
}

@DisplayName("피드백 작성 성공")
@Test
@Transactional
void writeFeedback() {
// given
String subject = "test subject";
String content = "test content";
FeedbackWriteRequest request = new FeedbackWriteRequest(subject, content);
// User 객체는 나중에 변경해야 함!
// TODO: User 객체는 나중에 변경해야 함!
User user = new User();
user.setName("1");
user.setName(member.getMemberId().toString());

// when
FeedbackResponse actual = feedbackService.writeFeedback(request, user);

// then
SoftAssertions.assertSoftly(softAssertions -> {
softAssertions.assertThat(actual.getFeedbackId()).isEqualTo(1L);
softAssertions.assertThat(actual.getFeedbackId()).isNotNull();
softAssertions.assertThat(actual.getSubject()).isEqualTo(subject);
softAssertions.assertThat(actual.getContent()).isEqualTo(content);
softAssertions.assertThat(actual.getLikeCount()).isZero();
Expand Down
Loading