-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Kirill Mokevnin <[email protected]>
- Loading branch information
Showing
12 changed files
with
280 additions
and
11 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/io/hexlet/blog/controller/api/PostsCommentsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.hexlet.blog.controller.api; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import io.hexlet.blog.dto.PostCommentParamsDTO; | ||
import io.hexlet.blog.model.PostComment; | ||
import io.hexlet.blog.repository.PostCommentRepository; | ||
import io.hexlet.blog.specification.PostCommentSpecification; | ||
|
||
@RestController | ||
@RequestMapping("/api") | ||
public class PostsCommentsController { | ||
@Autowired | ||
private PostCommentRepository repository; | ||
|
||
@Autowired | ||
private PostCommentSpecification specBuilder; | ||
|
||
@GetMapping("/posts_comments") | ||
@ResponseStatus(HttpStatus.OK) | ||
Page<PostComment> index(PostCommentParamsDTO params, @RequestParam(defaultValue = "1") int page) { | ||
var spec = specBuilder.build(params); | ||
var comments = repository.findAll(spec, PageRequest.of(page - 1, 10)); | ||
|
||
return comments; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/io/hexlet/blog/dto/PostCommentParamsDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.hexlet.blog.dto; | ||
|
||
import java.util.Date; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
public class PostCommentParamsDTO { | ||
private String nameCont; | ||
private Long authorId; | ||
private Long postId; | ||
private Date createdAtGt; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/io/hexlet/blog/repository/PostCommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.hexlet.blog.repository; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import io.hexlet.blog.model.Post; | ||
import io.hexlet.blog.model.PostComment; | ||
|
||
@Repository | ||
public interface PostCommentRepository extends JpaRepository<PostComment, Long>, JpaSpecificationExecutor<PostComment> { | ||
// Page<Post> findAll(Specification<Post> spec, Pageable pageable); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.hexlet.blog.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import io.hexlet.blog.dto.PostCreateDTO; | ||
import io.hexlet.blog.dto.PostDTO; | ||
import io.hexlet.blog.dto.PostUpdateDTO; | ||
import io.hexlet.blog.exception.ResourceNotFoundException; | ||
import io.hexlet.blog.mapper.PostMapper; | ||
import io.hexlet.blog.repository.PostRepository; | ||
import io.hexlet.blog.util.UserUtils; | ||
|
||
@Service | ||
public class PostService { | ||
@Autowired | ||
private PostRepository repository; | ||
|
||
@Autowired | ||
private PostMapper postMapper; | ||
|
||
@Autowired | ||
private UserUtils userUtils; | ||
|
||
public List<PostDTO> getAll() { | ||
var posts = repository.findAll(); | ||
var result = posts.stream() | ||
.map(postMapper::map) | ||
.toList(); | ||
return result; | ||
} | ||
|
||
PostDTO create(PostCreateDTO postData) { | ||
var post = postMapper.map(postData); | ||
post.setAuthor(userUtils.getCurrentUser()); | ||
repository.save(post); | ||
var postDTO = postMapper.map(post); | ||
return postDTO; | ||
} | ||
|
||
PostDTO findById(Long id) { | ||
var post = repository.findById(id) | ||
.orElseThrow(() -> new ResourceNotFoundException("Not Found: " + id)); | ||
var postDTO = postMapper.map(post); | ||
return postDTO; | ||
} | ||
|
||
PostDTO update(PostUpdateDTO postData, Long id) { | ||
var post = repository.findById(id) | ||
.orElseThrow(() -> new ResourceNotFoundException("Not Found")); | ||
postMapper.update(postData, post); | ||
repository.save(post); | ||
var postDTO = postMapper.map(post); | ||
return postDTO; | ||
} | ||
|
||
void delete(Long id) { | ||
repository.deleteById(id); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/io/hexlet/blog/specification/PostCommentSpecification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.hexlet.blog.specification; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
import io.hexlet.blog.dto.PostCommentParamsDTO; | ||
import io.hexlet.blog.model.PostComment; | ||
|
||
/** | ||
* PostCommentSpecification | ||
*/ | ||
@Component | ||
public class PostCommentSpecification { | ||
public Specification<PostComment> build(PostCommentParamsDTO params) { | ||
Specification<PostComment> spec = Specification.where(null); | ||
return spec | ||
.and(withPostId(params.getPostId())) | ||
.and(withCreatedAtGt(params.getCreatedAtGt())); | ||
} | ||
|
||
private Specification<PostComment> withPostId(Long postId) { | ||
return (root, query, cb) -> postId == null ? cb.conjunction() : cb.equal(root.get("post").get("id"), postId); | ||
} | ||
|
||
private Specification<PostComment> withCreatedAtGt(Date date) { | ||
return (root, query, cb) -> date == null ? cb.conjunction() : cb.greaterThan(root.get("created_at"), date); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
src/test/java/io/hexlet/blog/controller/api/PostsCommentsControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.hexlet.blog.controller.api; | ||
|
||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | ||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.instancio.Instancio; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.JwtRequestPostProcessor; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import io.hexlet.blog.model.Post; | ||
import io.hexlet.blog.repository.PostCommentRepository; | ||
import io.hexlet.blog.repository.PostRepository; | ||
import io.hexlet.blog.util.ModelGenerator; | ||
import io.hexlet.blog.util.UserUtils; | ||
import jakarta.transaction.Transactional; | ||
|
||
@SpringBootTest | ||
@Transactional | ||
@AutoConfigureMockMvc | ||
public class PostsCommentsControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ModelGenerator modelGenerator; | ||
|
||
@Autowired | ||
private PostRepository postRepository; | ||
|
||
@Autowired | ||
private PostCommentRepository postCommentRepository; | ||
|
||
@Autowired | ||
private UserUtils userUtils; | ||
|
||
private JwtRequestPostProcessor token; | ||
|
||
private Post testPost; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
token = jwt().jwt(builder -> builder.subject("[email protected]")); | ||
testPost = Instancio.of(modelGenerator.getPostModel()) | ||
.create(); | ||
testPost.setAuthor(userUtils.getTestUser()); | ||
postRepository.save(testPost); | ||
|
||
var testPost2 = Instancio.of(modelGenerator.getPostModel()) | ||
.create(); | ||
testPost2.setAuthor(userUtils.getTestUser()); | ||
postRepository.save(testPost2); | ||
|
||
var testPostComment = Instancio.of(modelGenerator.getPostCommentModel()).create(); | ||
testPostComment.setPost(testPost); | ||
testPostComment.setAuthor(userUtils.getTestUser()); | ||
postCommentRepository.save(testPostComment); | ||
|
||
var testPostComment2 = Instancio.of(modelGenerator.getPostCommentModel()).create(); | ||
testPostComment2.setPost(testPost2); | ||
testPostComment2.setAuthor(userUtils.getTestUser()); | ||
postCommentRepository.save(testPostComment2); | ||
} | ||
|
||
@Test | ||
public void testIndex() throws Exception { | ||
var result = mockMvc.perform(get("/api/posts_comments").with(token)) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
var body = result.getResponse().getContentAsString(); | ||
assertThatJson(body) | ||
.node("content") | ||
.isArray() | ||
.hasSize(2); | ||
} | ||
|
||
@Test | ||
public void testFilteredIndex() throws Exception { | ||
var result = mockMvc.perform(get("/api/posts_comments?post_id=" + testPost.getId()).with(token)) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
var body = result.getResponse().getContentAsString(); | ||
assertThatJson(body) | ||
.node("content") | ||
.isArray() | ||
.hasSize(1); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters