Skip to content

Commit

Permalink
[PPANTT-54] feat: introducing total counter to pageInfo on apis
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini committed Jul 23, 2024
1 parent d1c955e commit ccd002f
Show file tree
Hide file tree
Showing 12 changed files with 1,679 additions and 1,793 deletions.
1,476 changes: 697 additions & 779 deletions openapi/openapi.json

Large diffs are not rendered by default.

999 changes: 477 additions & 522 deletions openapi/openapi_backoffice_external_ec.json

Large diffs are not rendered by default.

468 changes: 224 additions & 244 deletions openapi/openapi_backoffice_external_psp.json

Large diffs are not rendered by default.

407 changes: 189 additions & 218 deletions openapi/openapi_backoffice_helpdesk.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package it.pagopa.selfcare.pagopa.entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class BrokerInstitutionAggregate {

private List<BrokerInstitutionEntity> institutionEntities;
private Long total;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.pagopa.selfcare.pagopa.entities;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class IbanAggregate {

private List<IbanEntity> ibansSlice;
private Long total;

}
8 changes: 8 additions & 0 deletions src/main/java/it/pagopa/selfcare/pagopa/model/PageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,12 @@ public class PageInfo implements Serializable {
@Schema(description = "number of elements per page", requiredMode = Schema.RequiredMode.REQUIRED)
Integer limit;

@JsonProperty("totalElements")
@Schema(description = "total elements of elements", requiredMode = Schema.RequiredMode.REQUIRED)
Long totalElements;

@JsonProperty("totalPages")
@Schema(description = "total number of pages", requiredMode = Schema.RequiredMode.REQUIRED)
Long totalPages;

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package it.pagopa.selfcare.pagopa.repository;

import it.pagopa.selfcare.pagopa.entities.BrokerIbansEntity;
import it.pagopa.selfcare.pagopa.entities.IbanAggregate;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;


@Repository
public interface BrokerIbansRepository extends MongoRepository<BrokerIbansEntity, String> {

@Query(value = "{ brokerCode : ?0 }", fields = "{ ibans: { $slice: [?1, ?2]}}")
Optional<BrokerIbansEntity> getBrokerIbans(String brokerCode, Integer skip, Integer limit);
@Aggregation(pipeline = {
"{ $match : { brokerCode : ?0 } }",
"{ $project: { ibansSlice : { $slice: ['$ibans', ?1, ?2] }, total: { $size: '$ibans' }, } }"
})
Optional<IbanAggregate> getBrokerIbans(String brokerCode, Integer sliceStart, Integer size);

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package it.pagopa.selfcare.pagopa.repository;

import it.pagopa.selfcare.pagopa.entities.BrokerInstitutionAggregate;
import it.pagopa.selfcare.pagopa.entities.BrokerInstitutionsEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.Aggregation;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface BrokerInstitutionsRepository extends MongoRepository<BrokerInstitutionsEntity, String> {

@Query(value = "{ brokerCode : ?0 }", fields = "{ institutions: { $slice: [?1, ?2]}}")
Optional<BrokerInstitutionsEntity> findPagedInstitutionsByBrokerCode(String brokerCode, int skip, Integer limit);
@Aggregation(pipeline = {
"{ $match : { brokerCode : ?0 } }",
"{ $project: { institutionsSlice : { $slice: ['$institutions', ?1, ?2] }, total: { $size: '$institutions' }, } }"
})
Optional<BrokerInstitutionAggregate> findPagedInstitutionsByBrokerCode(String brokerCode, Integer sliceStart, Integer size);

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package it.pagopa.selfcare.pagopa.service.impl;

import it.pagopa.selfcare.pagopa.entities.BrokerIbansEntity;
import it.pagopa.selfcare.pagopa.entities.BrokerInstitutionsEntity;
import it.pagopa.selfcare.pagopa.entities.CreditorInstitutionIbansEntity;
import it.pagopa.selfcare.pagopa.entities.*;
import it.pagopa.selfcare.pagopa.exception.AppError;
import it.pagopa.selfcare.pagopa.exception.AppException;
import it.pagopa.selfcare.pagopa.model.BrokerInstitutionResource;
Expand All @@ -21,6 +19,7 @@
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
Expand All @@ -41,22 +40,24 @@ public ExternalServiceImpl(BrokerInstitutionsRepository brokerInstitutionsReposi

@Override
public BrokerInstitutionsResponse getBrokerInstitutions(String brokerCode, Integer limit, Integer page) {
Optional<BrokerInstitutionsEntity> brokerInstitutionsEntity = brokerInstitutionsRepository
Optional<BrokerInstitutionAggregate> brokerInstitutionsEntity = brokerInstitutionsRepository
.findPagedInstitutionsByBrokerCode(brokerCode, page == 0 ? 0 : ((page * limit) - 1), limit);
if(brokerInstitutionsEntity.isEmpty() ||
brokerInstitutionsEntity.get().getInstitutions() == null) {
brokerInstitutionsEntity.get().getInstitutionEntities() == null) {
throw new AppException(AppError.BROKER_INSTITUTIONS_NOT_FOUND, brokerCode);
}
return BrokerInstitutionsResponse
.builder()
.creditorInstitutions(brokerInstitutionsEntity.get().getInstitutions().stream().map(
.creditorInstitutions(brokerInstitutionsEntity.get().getInstitutionEntities().stream().map(
brokerInstutitionEntity -> {
BrokerInstitutionResource response = new BrokerInstitutionResource();
BeanUtils.copyProperties(brokerInstutitionEntity, response);
return response;
}
).toList())
.pageInfo(PageInfoMapper.toPageInfo(page, limit))
.pageInfo(PageInfoMapper.toPageInfo(page, limit,
brokerInstitutionsEntity.get().getTotal() / limit,
brokerInstitutionsEntity.get().getTotal()))
.build();
}

Expand All @@ -74,28 +75,33 @@ public CIIbansResponse getCIsIbans(Integer limit, Integer page) {
return response;
})
.toList())
.pageInfo(PageInfoMapper.toPageInfo(page, limit))
.pageInfo(PageInfoMapper.toPageInfo(
page, limit,
creditorInstitutionIbansEntities.getTotalPages(),
creditorInstitutionIbansEntities.getTotalElements()))
.build();
}

@Override
public CIIbansResponse getBrokerIbans(String brokerCode, Integer limit, Integer page) {
Optional<BrokerIbansEntity> brokerIbanEntities = brokerIbansRepository.getBrokerIbans(
Pageable pageable = PageRequest.of(page, limit);
Optional<IbanAggregate> brokerIbanAggregate = brokerIbansRepository.getBrokerIbans(
brokerCode, page == 0 ? 0 : ((page * limit) - 1), limit);
if(brokerIbanEntities.isEmpty() ||
brokerIbanEntities.get().getIbans() == null) {
if(brokerIbanAggregate.isEmpty() ||
brokerIbanAggregate.get().getIbansSlice() == null) {
throw new AppException(AppError.BROKER_IBANS_NOT_FOUND, brokerCode);
}
return CIIbansResponse
.builder()
.ibans(brokerIbanEntities.get().getIbans() != null ?
brokerIbanEntities.get().getIbans().stream().map(brokerIbanEntity -> {
.ibans(brokerIbanAggregate.get().getIbansSlice().stream().map(brokerIbanEntity -> {
CIIbansResource response = new CIIbansResource();
BeanUtils.copyProperties(brokerIbanEntity, response);
return response;
}).toList()
: Collections.emptyList())
.pageInfo(PageInfoMapper.toPageInfo(page, limit))
}).toList())
.pageInfo(PageInfoMapper.toPageInfo(
page, limit,
brokerIbanAggregate.get().getTotal() / limit,
brokerIbanAggregate.get().getTotal()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Contains utility methods related to the creation of a PageInfo instance
*/
public class PageInfoMapper {
public static PageInfo toPageInfo(int page, int limit) {
return PageInfo.builder().limit(limit).page(page).build();
public static PageInfo toPageInfo(int page, int limit, long totalPages, long totalElements) {
return PageInfo.builder().limit(limit).page(page).totalElements(totalElements).totalPages(totalPages).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ void setup() {
void requestWithValidBrokerCodeShouldReturnValidResponse() {
when(brokerInstitutionsRepository.findPagedInstitutionsByBrokerCode(
"VALID_BROKER_CODE", 0, 10)).thenReturn(
Optional.of(BrokerInstitutionsEntity.builder()
.brokerCode("VALID_BROKER_CODE")
.institutions(Collections.singletonList(BrokerInstitutionEntity
Optional.of(BrokerInstitutionAggregate.builder()
.total(10L)
.institutionEntities(Collections.singletonList(BrokerInstitutionEntity
.builder().brokerTaxCode("brokerTaxCode").build())).build())
);
BrokerInstitutionsResponse brokerInstitutionsResponse = Assertions.assertDoesNotThrow(
Expand Down Expand Up @@ -105,9 +105,9 @@ void requestWithMissingIbansShouldReturnEmptyList() {
void requestOnIbansWithValidBrokerCodeShouldReturnValidResponse() {
when(brokerIbansRepository.getBrokerIbans(
"VALID_BROKER_CODE", 0, 10)).thenReturn(
Optional.of(BrokerIbansEntity.builder()
.brokerCode("VALID_BROKER_CODE")
.ibans(Collections.singletonList(IbanEntity
Optional.of(IbanAggregate.builder()
.total(10L)
.ibansSlice(Collections.singletonList(IbanEntity
.builder().iban("IBAN").build())).build())
);
CIIbansResponse brokerInstitutionsResponse = Assertions.assertDoesNotThrow(
Expand Down

0 comments on commit ccd002f

Please sign in to comment.