Skip to content

Commit

Permalink
[VAS-682] total page
Browse files Browse the repository at this point in the history
  • Loading branch information
jacopocarlini committed Feb 13, 2024
1 parent eec681f commit 3df8ba3
Show file tree
Hide file tree
Showing 2 changed files with 685 additions and 692 deletions.
Original file line number Diff line number Diff line change
@@ -1,58 +1,77 @@
package it.pagopa.afm.marketplacebe.repository;

import java.util.List;
import java.util.StringJoiner;

import com.azure.cosmos.models.SqlQuerySpec;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
import it.pagopa.afm.marketplacebe.entity.Bundle;
import it.pagopa.afm.marketplacebe.entity.BundleType;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.azure.cosmos.models.SqlQuerySpec;
import com.azure.spring.data.cosmos.core.CosmosTemplate;

import it.pagopa.afm.marketplacebe.entity.Bundle;
import it.pagopa.afm.marketplacebe.entity.BundleType;
import java.util.List;
import java.util.StringJoiner;

@Repository
public class CosmosRepository {

@Autowired
CosmosTemplate cosmosTemplate;

public List<Bundle> getBundlesByNameAndType(String idPsp, String name,
List<BundleType> types, int offset, int limit) {

StringBuilder builder = new StringBuilder();
builder.append("SELECT * FROM bundles b WHERE 1=1 ");

// adds the idPsp clause if present
if (StringUtils.isNotEmpty(idPsp)) {
builder.append("AND b.idPsp = '" + idPsp + "' ");
} else {
// NOT idPsp search --> adds check on validityDateTo to return only valid bundles
builder.append("AND (IS_NULL(b.validityDateTo) "
+ "OR SUBSTRING(DateTimeFromParts(b.validityDateTo[0], b.validityDateTo[1], b.validityDateTo[2], 0, 0, 0, 0), 0, 10) > SUBSTRING(GetCurrentDateTime(), 0, 10)) ");
}

// adds the name clause
if (StringUtils.isNotEmpty(name)) {
builder.append("AND b.name like '%" + name + "%' ");
}

// adds the bundle types clause
if (CollectionUtils.isNotEmpty(types)) {
builder.append("AND b.type IN ( ");
StringJoiner joiner = new StringJoiner(",");
types.forEach(item -> joiner.add("'" + item.toString() + "'"));
builder.append(joiner.toString() + " ) ");
}

builder.append("ORDER BY b.id OFFSET " + offset + " LIMIT " + limit);

return IterableUtils
.toList(cosmosTemplate.runQuery(new SqlQuerySpec(builder.toString()), Bundle.class, Bundle.class));
}
@Autowired
CosmosTemplate cosmosTemplate;

public List<Bundle> getBundlesByNameAndType(String idPsp, String name,
List<BundleType> types, int pageNumber, int pageSize) {

StringBuilder builder = new StringBuilder();
builder.append("SELECT * FROM bundles b WHERE 1=1 ");

buildWhereConditions(idPsp, name, types, builder);

builder.append("ORDER BY b.id OFFSET " + pageNumber * pageSize + " LIMIT " + pageSize);

return IterableUtils
.toList(cosmosTemplate.runQuery(new SqlQuerySpec(builder.toString()), Bundle.class, Bundle.class));
}

public Integer getTotalPages(String idPsp, String name, List<BundleType> types, int pageSize) {

StringBuilder builder = new StringBuilder();
builder.append("SELECT VALUE COUNT(b.id) FROM bundles b WHERE 1=1 ");

buildWhereConditions(idPsp, name, types, builder);

List<Integer> result = IterableUtils
.toList(cosmosTemplate.runQuery(new SqlQuerySpec(builder.toString()), Bundle.class, Integer.class));

if(!result.isEmpty()) {
return (int) Math.ceil((double) result.get(0) / pageSize);
} else {
return 0;
}
}

private static void buildWhereConditions(String idPsp, String name, List<BundleType> types, StringBuilder builder) {
// adds the idPsp clause if present
if(StringUtils.isNotEmpty(idPsp)) {
builder.append("AND b.idPsp = '" + idPsp + "' ");
} else {
// NOT idPsp search --> adds check on validityDateTo to return only valid bundles
builder.append("AND (IS_NULL(b.validityDateTo) "
+ "OR SUBSTRING(DateTimeFromParts(b.validityDateTo[0], b.validityDateTo[1], b.validityDateTo[2], 0, 0, 0, 0), 0, 10) > SUBSTRING(GetCurrentDateTime(), 0, 10)) ");
}

// adds the name clause
if(StringUtils.isNotEmpty(name)) {
builder.append("AND b.name like '%" + name + "%' ");
}

// adds the bundle types clause
if(CollectionUtils.isNotEmpty(types)) {
builder.append("AND b.type IN ( ");
StringJoiner joiner = new StringJoiner(",");
types.forEach(item -> joiner.add("'" + item.toString() + "'"));
builder.append(joiner + " ) ");
}
}

}
Loading

0 comments on commit 3df8ba3

Please sign in to comment.