Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Aug 30, 2023
1 parent 61b1052 commit ada8473
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.demo.web;

import java.util.List;

public record PaginatedResult<T>(List<T> data, Long count) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public class PostController {
private final PostRepository posts;

@GetMapping(value = "", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<Page<PostSummary>> getAll(@RequestParam(defaultValue = "") String q,
public ResponseEntity<PaginatedResult<PostSummary>> getAll(@RequestParam(defaultValue = "") String q,
@RequestParam(defaultValue = "") String status,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
var postStatus = StringUtils.hasText(status) ? Status.valueOf(status) : null;
var data = this.posts.findAll(Specifications.findByKeyword(q, postStatus), PageRequest.of(page, size))
.map(p -> new PostSummary(p.getTitle(), p.getCreatedAt()));
return ok(data);
return ok(new PaginatedResult<>(data.getContent(), data.getTotalElements()));
}

@PostMapping(value = "", consumes = APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void tetGetAllPosts() throws Exception {
);

this.rest.perform(get("/posts").accept(MediaType.APPLICATION_JSON))
.andExpectAll(status().isOk(), jsonPath("$.totalElements", equalTo(2)));
.andExpectAll(status().isOk(), jsonPath("$.count", equalTo(2)));

verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class));
verifyNoMoreInteractions(this.posts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void getAllPostsWillBeOk() throws Exception {
.get()
.uri("/posts")
.exchange()
.expectStatus().isOk();
// .expectBody().jsonPath("$.totalElements").isEqualTo(2);
.expectStatus().isOk()
.expectBody().jsonPath("$.count").isEqualTo(2);

verify(this.posts, times(1)).findAll(isA(Specification.class), isA(Pageable.class));
verifyNoMoreInteractions(this.posts);
Expand Down

0 comments on commit ada8473

Please sign in to comment.