Skip to content

Commit cfb9414

Browse files
authored
Fix: Wrong X-Pagination-Item-Total header value (#94)
1 parent 82283f3 commit cfb9414

30 files changed

Lines changed: 309 additions & 145 deletions

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/ActorController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ public ActorController(ActorServiceImpl service, ResponseUtils responseUtils) {
6868
*/
6969
@Operation(summary = "Get all actors")
7070
public ResponseEntity<List<ActorDTO>> findAll(Map<String, String> filters, Pageable pageable) {
71-
Page<UUID> uuids = service.findAllUUIDs(pageable);
71+
long count = service.getCount();
72+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
7273
HttpHeaders headers = responseUtils.getHeaders(
7374
uuids,
7475
pageable,
75-
null
76+
null,
77+
count
7678
);
7779

7880
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/CharacterController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ public CharacterController(CharacterServiceImpl service, ResponseUtils responseU
6666
*/
6767
@Operation(summary = "Get all characters")
6868
public ResponseEntity<List<CharacterDTO>> findAll(Map<String, String> filters, Pageable pageable) {
69-
Page<UUID> uuids = service.findAllUUIDs(pageable);
69+
long count = service.getCount();
70+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
7071
HttpHeaders headers = responseUtils.getHeaders(
7172
uuids,
7273
pageable,
73-
null
74+
null,
75+
count
7476
);
7577

7678
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/EpisodeController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public EpisodeController(EpisodeServiceImpl service, ResponseUtils responseUtils
7373
public ResponseEntity<List<EpisodeDTO>> findAll(Map<String, String> filters, Pageable pageable) {
7474
filters.putIfAbsent("language", responseUtils.getDefaultLanguage());
7575

76-
Page<UUID> uuids = service.findAllUUIDs(pageable);
76+
long count = service.getCount();
77+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
7778
HttpHeaders headers = responseUtils.getHeaders(
7879
uuids,
7980
pageable,
80-
filters.get("language")
81+
filters.get("language"),
82+
count
8183
);
8284

8385
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/GameController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public GameController(GameServiceImpl GameService, ResponseUtils responseUtils)
7373
public ResponseEntity<List<GameDTO>> findAll(Map<String, String> filters, Pageable pageable) {
7474
filters.putIfAbsent("language", responseUtils.getDefaultLanguage());
7575

76-
Page<UUID> uuids = service.findAllUUIDs(pageable);
76+
long count = service.getCount();
77+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
7778
HttpHeaders headers = responseUtils.getHeaders(
7879
uuids,
7980
pageable,
80-
filters.get("language")
81+
filters.get("language"),
82+
count
8183
);
8284

8385
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/LocationController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public LocationController(LocationServiceImpl LocationService, ResponseUtils res
7373
public ResponseEntity<List<LocationDTO>> findAll(Map<String, String> filters, Pageable pageable) {
7474
filters.putIfAbsent("language", responseUtils.getDefaultLanguage());
7575

76-
Page<UUID> uuids = service.findAllUUIDs(pageable);
76+
long count = service.getCount();
77+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
7778
HttpHeaders headers = responseUtils.getHeaders(
7879
uuids,
7980
pageable,
80-
filters.get("language")
81+
filters.get("language"),
82+
count
8183
);
8284

8385
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/SeasonController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ public SeasonController(SeasonServiceImpl SeasonService, ResponseUtils responseU
7373
public ResponseEntity<List<SeasonDTO>> findAll(Map<String, String> filters, Pageable pageable) {
7474
filters.putIfAbsent("language", responseUtils.getDefaultLanguage());
7575

76-
Page<UUID> uuids = service.findAllUUIDs(pageable);
76+
long count = service.getCount();
77+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
7778
HttpHeaders headers = responseUtils.getHeaders(
7879
uuids,
7980
pageable,
80-
filters.get("language")
81+
filters.get("language"),
82+
count
8183
);
8284

8385
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/api/v1/SoundtrackController.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ public SoundtrackController(SoundtrackServiceImpl SoundtrackService, ResponseUti
6363
*/
6464
@GetMapping
6565
public ResponseEntity<List<SoundtrackDTO>> findAll(Map<String, String> filters, Pageable pageable) {
66-
Page<UUID> uuids = service.findAllUUIDs(pageable);
66+
long count = service.getCount();
67+
Page<UUID> uuids = service.findAllUUIDs(pageable, count);
6768
HttpHeaders headers = responseUtils.getHeaders(
6869
uuids,
6970
pageable,
70-
null
71+
null,
72+
count
7173
);
7274

7375
if (uuids.isEmpty()) ResponseEntity.ok().headers(headers).body(Collections.emptyList());

‎src/main/java/com/lucasjosino/hawapi/controllers/utils/ResponseUtils.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ public ResponseUtils(LanguageUtils languageUtils) {
2323
this.languageUtils = languageUtils;
2424
}
2525

26-
public <T extends BaseModel> HttpHeaders getHeaders(Page<UUID> modelPage, Pageable pageable, String language) {
26+
public <T extends BaseModel> HttpHeaders getHeaders(
27+
Page<UUID> modelPage,
28+
Pageable pageable,
29+
String language,
30+
long totalCount
31+
) {
2732
HttpHeaders headers = new HttpHeaders() {{
2833
add("X-Pagination-Page-Index", String.valueOf(pageable.getPageNumber() + 1));
2934
add("X-Pagination-Page-Size", String.valueOf(pageable.getPageSize()));
3035
add("X-Pagination-Page-Total", String.valueOf(modelPage.getTotalPages()));
31-
add("X-Pagination-Item-Total", String.valueOf(modelPage.getContent().size()));
36+
add("X-Pagination-Item-Total", String.valueOf(totalCount));
3237
}};
3338

3439
if (!isNullOrEmpty(language)) headers.add("Content-Language", language);

‎src/main/java/com/lucasjosino/hawapi/services/base/BaseService.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
*/
2727
public interface BaseService<D extends BaseDTO> {
2828

29-
Page<UUID> findAllUUIDs(Pageable pageable);
29+
long getCount();
30+
31+
Page<UUID> findAllUUIDs(Pageable pageable, long count);
3032

3133
@Cacheable(value = "findAll", keyGenerator = "findAllKeyGenerator")
3234
List<D> findAll(Map<String, String> filters, List<UUID> uuids);

‎src/main/java/com/lucasjosino/hawapi/services/impl/ActorServiceImpl.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,25 @@ public ActorServiceImpl(
6464
this.socialRepository = socialRepository;
6565
}
6666

67+
/**
68+
* Method that the count of all actor
69+
*
70+
* @return The count of all actors
71+
* @since 1.0.0
72+
*/
73+
public long getCount() {
74+
return repository.count();
75+
}
76+
6777
/**
6878
* Method that get all actor uuids filtering with {@link Pageable}
6979
*
7080
* @param pageable An {@link Page} with pageable params. Can be null
7181
* @return A {@link Page} of {@link UUID} or empty
7282
* @since 1.0.0
7383
*/
74-
public Page<UUID> findAllUUIDs(Pageable pageable) {
84+
public Page<UUID> findAllUUIDs(Pageable pageable, long count) {
7585
List<UUID> res = repository.findAllUUIDs(pageable);
76-
long count = repository.count();
7786
return PageableExecutionUtils.getPage(res, pageable, () -> count);
7887
}
7988

0 commit comments

Comments
 (0)