Skip to content

Commit

Permalink
feat: adds comments to post request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 10, 2023
1 parent 5abf4a8 commit 25b84e5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.example.graphql.querydsl.mapper;

import com.example.graphql.querydsl.entities.Post;
import com.example.graphql.querydsl.entities.PostComment;
import com.example.graphql.querydsl.entities.PostDetails;
import com.example.graphql.querydsl.model.request.CreatePostRequest;
import com.example.graphql.querydsl.model.request.PostRequest;
import com.example.graphql.querydsl.model.request.UpdatePostRequest;
import com.example.graphql.querydsl.model.response.PostResponse;
import java.time.LocalDateTime;
import java.util.List;
import org.mapstruct.*;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper
public interface PostMapper {

@Mapping(target = "details", ignore = true)
@Mapping(target = "comments", ignore = true)
@Mapping(target = "id", ignore = true)
Post toEntity(CreatePostRequest createPostRequest);

@Mapping(target = "details", ignore = true)
@Mapping(target = "comments", ignore = true)
@Mapping(target = "id", ignore = true)
// @Mapping(target = "createdOn", ignore = true)
void mapPostWithRequest(PostRequest postRequest, @MappingTarget Post post);
void mapPostWithRequest(UpdatePostRequest updatePostRequest, @MappingTarget Post post);

@Mapping(target = "createdOn", source = "details.createdOn")
PostResponse toResponse(Post post);
Expand All @@ -28,5 +35,9 @@ public interface PostMapper {
default void setAfterMappingToPost(CreatePostRequest createPostRequest, @MappingTarget Post post) {
post.addDetails(
new PostDetails().setCreatedBy(createPostRequest.createdBy()).setCreatedOn(LocalDateTime.now()));
createPostRequest
.postCommentRequests()
.forEach(postCommentRequest -> post.addComment(
new PostComment().setReview(postCommentRequest.review()).setCreatedOn(LocalDateTime.now())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

public record CreatePostRequest(
@NotEmpty(message = "Title cannot be empty") String title,
@NotBlank(message = "Content cannot be blank") String content,
@NotBlank(message = "CreatedBy cannot be blank") String createdBy) {}
@NotBlank(message = "CreatedBy cannot be blank") String createdBy,
List<PostCommentRequest> postCommentRequests) {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;

public record PostRequest(
public record UpdatePostRequest(
@NotEmpty(message = "Title cannot be empty") String title,
@NotBlank(message = "Content cannot be blank") String content) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.example.graphql.querydsl.model.response;

import java.time.LocalDateTime;
import java.util.List;

public record PostResponse(Long id, String title, String content, LocalDateTime createdOn) {}
public record PostResponse(
Long id,
String title,
String content,
LocalDateTime createdOn,
List<PostCommentResponse> postCommentResponses) {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.example.graphql.querydsl.mapper.PostMapper;
import com.example.graphql.querydsl.model.query.FindPostsQuery;
import com.example.graphql.querydsl.model.request.CreatePostRequest;
import com.example.graphql.querydsl.model.request.PostRequest;
import com.example.graphql.querydsl.model.request.UpdatePostRequest;
import com.example.graphql.querydsl.model.response.PagedResult;
import com.example.graphql.querydsl.model.response.PostResponse;
import com.example.graphql.querydsl.repositories.PostRepository;
Expand Down Expand Up @@ -60,11 +60,11 @@ public PostResponse savePost(CreatePostRequest createPostRequest) {
}

@Transactional
public PostResponse updatePost(Long id, PostRequest postRequest) {
public PostResponse updatePost(Long id, UpdatePostRequest updatePostRequest) {
Post post = postRepository.findById(id).orElseThrow(() -> new PostNotFoundException(id));

// Update the post object with data from postRequest
postMapper.mapPostWithRequest(postRequest, post);
postMapper.mapPostWithRequest(updatePostRequest, post);

// Save the updated post object
Post updatedPost = postRepository.save(post);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.example.graphql.querydsl.exception.PostNotFoundException;
import com.example.graphql.querydsl.model.query.FindPostsQuery;
import com.example.graphql.querydsl.model.request.CreatePostRequest;
import com.example.graphql.querydsl.model.request.PostRequest;
import com.example.graphql.querydsl.model.request.UpdatePostRequest;
import com.example.graphql.querydsl.model.response.PagedResult;
import com.example.graphql.querydsl.model.response.PostResponse;
import com.example.graphql.querydsl.services.PostService;
Expand Down Expand Up @@ -63,7 +63,8 @@ public ResponseEntity<PostResponse> createPost(@RequestBody @Validated CreatePos
}

@PutMapping("/{id}")
public ResponseEntity<PostResponse> updatePost(@PathVariable Long id, @RequestBody @Valid PostRequest postRequest) {
public ResponseEntity<PostResponse> updatePost(
@PathVariable Long id, @RequestBody @Valid UpdatePostRequest postRequest) {
return ResponseEntity.ok(postService.updatePost(id, postRequest));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.example.graphql.querydsl.model.response.PostResponse;
import com.example.graphql.querydsl.repositories.PostRepository;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -63,6 +64,6 @@ private Post getPost() {
}

private PostResponse getPostResponse() {
return new PostResponse(1L, "junitTest", "junitContent", LocalDateTime.now());
return new PostResponse(1L, "junitTest", "junitContent", LocalDateTime.now(), new ArrayList<>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import com.example.graphql.querydsl.entities.Post;
import com.example.graphql.querydsl.entities.PostDetails;
import com.example.graphql.querydsl.model.request.CreatePostRequest;
import com.example.graphql.querydsl.model.request.PostRequest;
import com.example.graphql.querydsl.model.request.PostCommentRequest;
import com.example.graphql.querydsl.model.request.UpdatePostRequest;
import com.example.graphql.querydsl.repositories.PostRepository;
import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -75,7 +76,11 @@ void shouldFindPostById() throws Exception {

@Test
void shouldCreateNewPost() throws Exception {
CreatePostRequest postRequest = new CreatePostRequest("New Post", "New Content", "Junit");
CreatePostRequest postRequest = new CreatePostRequest(
"New Post",
"New Content",
"Junit",
List.of(new PostCommentRequest("New Review"), new PostCommentRequest("Second Review")));
this.mockMvc
.perform(post("/api/posts")
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -89,7 +94,7 @@ void shouldCreateNewPost() throws Exception {

@Test
void shouldReturn400WhenCreateNewPostWithoutTitleAndContent() throws Exception {
CreatePostRequest createPostRequest = new CreatePostRequest(null, null, null);
CreatePostRequest createPostRequest = new CreatePostRequest(null, null, null, new ArrayList<>());

this.mockMvc
.perform(post("/api/posts")
Expand All @@ -115,16 +120,16 @@ void shouldReturn400WhenCreateNewPostWithoutTitleAndContent() throws Exception {
@Test
void shouldUpdatePost() throws Exception {
Long postId = postList.get(0).getId();
PostRequest postRequest = new PostRequest("Updated Post", "New Content");
UpdatePostRequest updatePostRequest = new UpdatePostRequest("Updated Post", "New Content");

this.mockMvc
.perform(put("/api/posts/{id}", postId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(postRequest)))
.content(objectMapper.writeValueAsString(updatePostRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(postId), Long.class))
.andExpect(jsonPath("$.title", is(postRequest.title())))
.andExpect(jsonPath("$.content", is(postRequest.content())));
.andExpect(jsonPath("$.title", is(updatePostRequest.title())))
.andExpect(jsonPath("$.content", is(updatePostRequest.content())));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.example.graphql.querydsl.exception.PostNotFoundException;
import com.example.graphql.querydsl.model.query.FindPostsQuery;
import com.example.graphql.querydsl.model.request.CreatePostRequest;
import com.example.graphql.querydsl.model.request.PostRequest;
import com.example.graphql.querydsl.model.request.UpdatePostRequest;
import com.example.graphql.querydsl.model.response.PagedResult;
import com.example.graphql.querydsl.model.response.PostResponse;
import com.example.graphql.querydsl.services.PostService;
Expand Down Expand Up @@ -104,7 +104,7 @@ void shouldFetchAllPosts() throws Exception {
@Test
void shouldFindPostById() throws Exception {
Long postId = 1L;
PostResponse post = new PostResponse(postId, "text 1", "content 1", getCreatedOn());
PostResponse post = new PostResponse(postId, "text 1", "content 1", getCreatedOn(), new ArrayList<>());
given(postService.findPostById(postId)).willReturn(Optional.of(post));

this.mockMvc
Expand Down Expand Up @@ -134,8 +134,9 @@ void shouldReturn404WhenFetchingNonExistingPost() throws Exception {
@Test
void shouldCreateNewPost() throws Exception {

PostResponse post = new PostResponse(1L, "some text", "some content", getCreatedOn());
CreatePostRequest postRequest = new CreatePostRequest("some title", "some content", "appUser");
PostResponse post = new PostResponse(1L, "some text", "some content", getCreatedOn(), new ArrayList<>());
CreatePostRequest postRequest =
new CreatePostRequest("some title", "some content", "appUser", new ArrayList<>());
given(postService.savePost(any(CreatePostRequest.class))).willReturn(post);

this.mockMvc
Expand All @@ -153,7 +154,7 @@ void shouldCreateNewPost() throws Exception {

@Test
void shouldReturn400WhenCreateNewPostWithoutTitleAndContent() throws Exception {
CreatePostRequest createPostRequest = new CreatePostRequest(null, null, null);
CreatePostRequest createPostRequest = new CreatePostRequest(null, null, null, null);

this.mockMvc
.perform(post("/api/posts")
Expand Down Expand Up @@ -182,13 +183,15 @@ class Update {
@Test
void shouldUpdatePost() throws Exception {
Long postId = 1L;
PostResponse post = new PostResponse(postId, "Updated text", "some content", getCreatedOn());
PostRequest postRequest = new PostRequest("Updated text", "some content");
given(postService.updatePost(eq(postId), any(PostRequest.class))).willReturn(post);
PostResponse post =
new PostResponse(postId, "Updated text", "some content", getCreatedOn(), new ArrayList<>());
UpdatePostRequest updatePostRequest = new UpdatePostRequest("Updated text", "some content");
given(postService.updatePost(eq(postId), any(UpdatePostRequest.class)))
.willReturn(post);

mockMvc.perform(put("/api/posts/{id}", postId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(postRequest)))
.content(objectMapper.writeValueAsString(updatePostRequest)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(postId), Long.class))
.andExpect(jsonPath("$.title", is(post.title())))
Expand All @@ -200,13 +203,13 @@ void shouldUpdatePost() throws Exception {
@Test
void shouldReturn404WhenUpdatingNonExistingPost() throws Exception {
Long postId = 1L;
PostRequest postRequest = new PostRequest("Updated text", "some content");
given(postService.updatePost(eq(postId), any(PostRequest.class)))
UpdatePostRequest updatePostRequest = new UpdatePostRequest("Updated text", "some content");
given(postService.updatePost(eq(postId), any(UpdatePostRequest.class)))
.willThrow(new PostNotFoundException(postId));

mockMvc.perform(put("/api/posts/{id}", postId)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(postRequest)))
.content(objectMapper.writeValueAsString(updatePostRequest)))
.andExpect(status().isNotFound())
.andExpect(header().string("Content-Type", is(MediaType.APPLICATION_PROBLEM_JSON_VALUE)))
.andExpect(jsonPath("$.type", is("http://api.boot-graphql-querydsl.com/errors/not-found")))
Expand All @@ -219,7 +222,7 @@ void shouldReturn404WhenUpdatingNonExistingPost() throws Exception {
@Test
void shouldDeletePost() throws Exception {
Long postId = 1L;
PostResponse post = new PostResponse(postId, "Some text", "some content", getCreatedOn());
PostResponse post = new PostResponse(postId, "Some text", "some content", getCreatedOn(), new ArrayList<>());
given(postService.findPostById(postId)).willReturn(Optional.of(post));
doNothing().when(postService).deletePostById(postId);

Expand Down Expand Up @@ -252,7 +255,8 @@ List<PostResponse> getPostResponseList() {
post.getId(),
post.getTitle(),
post.getContent(),
post.getDetails().getCreatedOn()))
post.getDetails().getCreatedOn(),
new ArrayList<>()))
.toList();
}

Expand Down

0 comments on commit 25b84e5

Please sign in to comment.