Skip to content

Commit

Permalink
✨ create comment on recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxrxn committed Aug 4, 2024
1 parent ee634f5 commit 5925ec5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.authentication.resolver.LoginUser;
import net.pengcook.comment.dto.CommentOfRecipeResponse;
import net.pengcook.comment.dto.CreateCommentRequest;
import net.pengcook.comment.service.CommentService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
Expand All @@ -22,4 +27,10 @@ public class CommentController {
public List<CommentOfRecipeResponse> readComments(@PathVariable long recipeId, @LoginUser UserInfo userInfo) {
return commentService.readComments(recipeId, userInfo);
}

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public void createComment(@RequestBody CreateCommentRequest request, @LoginUser UserInfo userInfo) {
commentService.createComment(request, userInfo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.pengcook.recipe.domain.Recipe;
Expand All @@ -16,6 +17,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class Comment {

@Id
Expand All @@ -33,4 +35,8 @@ public class Comment {
private String message;

private LocalDateTime createdAt;

public Comment(User user, Recipe recipe, String message, LocalDateTime createdAt) {
this(0L, user, recipe, message, createdAt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.pengcook.comment.dto;

public record CreateCommentRequest(long recipeId, String message) {
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package net.pengcook.comment.service;

import java.time.LocalDateTime;
import java.util.List;
import lombok.RequiredArgsConstructor;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.comment.domain.Comment;
import net.pengcook.comment.dto.CommentOfRecipeResponse;
import net.pengcook.comment.dto.CreateCommentRequest;
import net.pengcook.comment.repository.CommentRepository;
import net.pengcook.recipe.domain.Recipe;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.user.domain.User;
import net.pengcook.user.repository.UserRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;
private final RecipeRepository recipeRepository;
private final UserRepository userRepository;

public List<CommentOfRecipeResponse> readComments(Long recipeId, UserInfo userInfo) {
List<Comment> comments = commentRepository.findByRecipeId(recipeId);
Expand All @@ -21,4 +29,12 @@ public List<CommentOfRecipeResponse> readComments(Long recipeId, UserInfo userIn
.map(comment -> new CommentOfRecipeResponse(comment, userInfo))
.toList();
}

public void createComment(CreateCommentRequest request, UserInfo userInfo) {
User user = userRepository.findByEmail(userInfo.getEmail()).orElseThrow();
Recipe recipe = recipeRepository.findById(request.recipeId()).orElseThrow();
Comment comment = new Comment(user, recipe, request.message(), LocalDateTime.now());

commentRepository.save(comment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import static com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper.document;
import static org.hamcrest.Matchers.is;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.requestFields;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName;
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import net.pengcook.RestDocsSetting;
import net.pengcook.authentication.annotation.WithLoginUser;
import net.pengcook.authentication.annotation.WithLoginUserTest;
import net.pengcook.comment.dto.CreateCommentRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.jdbc.Sql;
Expand Down Expand Up @@ -44,4 +47,26 @@ void readComments() {
.then().log().all()
.body("size()", is(2));
}

@Test
@WithLoginUser(email = "[email protected]")
@DisplayName("λŒ“κΈ€μ„ λ“±λ‘ν•œλ‹€.")
void createComment() {
CreateCommentRequest request = new CreateCommentRequest(1L, "thank you!");

RestAssured.given(spec).log().all()
.filter(document(DEFAULT_RESTDOCS_PATH,
"λ ˆμ‹œν”Όμ— λŒ“κΈ€μ„ λ“±λ‘ν•©λ‹ˆλ‹€.",
"λŒ“κΈ€ 등둝 API",
requestFields(
fieldWithPath("recipeId").description("λ ˆμ‹œν”Ό 아이디"),
fieldWithPath("message").description("λŒ“κΈ€ λ‚΄μš©")
)
))
.contentType(ContentType.JSON)
.body(request)
.when().post("api/comments")
.then().log().all()
.statusCode(201);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import net.pengcook.authentication.domain.UserInfo;
import net.pengcook.comment.dto.CommentOfRecipeResponse;
import net.pengcook.comment.dto.CreateCommentRequest;
import net.pengcook.comment.repository.CommentRepository;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,8 +20,12 @@
@Sql(scripts = "/data/comment.sql")
class CommentServiceTest {

private static final int INITIAL_COMMENT_COUNT = 3;

@Autowired
private CommentService commentService;
@Autowired
private CommentRepository commentRepository;

@Test
@DisplayName("λ ˆμ‹œν”Όμ˜ λŒ“κΈ€μ„ μ‘°νšŒν•œλ‹€.")
Expand All @@ -36,4 +42,15 @@ void readComments() {

assertThat(actual).containsExactlyInAnyOrderElementsOf(expect);
}

@Test
@DisplayName("λŒ“κΈ€μ„ λ“±λ‘ν•œλ‹€.")
void createComment() {
CreateCommentRequest request = new CreateCommentRequest(2L, "thank you!");
UserInfo userInfo = new UserInfo(2L, "[email protected]");

commentService.createComment(request, userInfo);

assertThat(commentRepository.count()).isEqualTo(INITIAL_COMMENT_COUNT + 1);
}
}

0 comments on commit 5925ec5

Please sign in to comment.