Skip to content

Commit

Permalink
resolve(conflict): resolve conflict with main
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaud-thorel-of committed Feb 7, 2024
2 parents ff86e5d + 6d388ae commit a3aa53f
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 91 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Build

on:
push:
branches: [ main ]
branches: [ main, [0-9]+.[0-9]+.* ]
pull_request:
branches: [ main ]
branches: [ main, [0-9]+.[0-9]+.* ]

jobs:
build:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/renovate.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Renovate

on:
schedule:
- cron: '*/5 8-19 * * 1-5'
#on:
# schedule:
# - cron: '*/5 8-19 * * 1-5'

jobs:
renovate:
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>fr.ouestfrance.querydsl</groupId>
<artifactId>querydsl-postgrest-bom</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
<packaging>pom</packaging>
<name>querydsl-postgrest-bom</name>
<description>Implementation of unified queryDSL for postgRest API</description>
Expand Down
4 changes: 2 additions & 2 deletions querydsl-postgrest-resttemplate-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>fr.ouestfrance.querydsl</groupId>
<artifactId>querydsl-postgrest-bom</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
</parent>

<artifactId>querydsl-postgrest-resttemplate-adapter</artifactId>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>6.1.2</version>
<version>6.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.ouestfrance.querydsl.postgrest.model.Page;
import fr.ouestfrance.querydsl.postgrest.model.PageImpl;
import fr.ouestfrance.querydsl.postgrest.model.Range;
import fr.ouestfrance.querydsl.postgrest.model.RangeResponse;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.reflect.TypeUtils;
Expand All @@ -21,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
* Rest interface for querying postgrest
Expand All @@ -37,15 +39,15 @@ public class PostgrestRestTemplate implements PostgrestClient {
* Postgrest restTemplate adapter
*
* @param restTemplate restTemplate
* @return PostgrestResttemplate implementation
* @return PostgrestRestTemplate implementation
*/
public static PostgrestRestTemplate of(RestTemplate restTemplate) {
return new PostgrestRestTemplate(restTemplate);
}

@Override
public <T> Page<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
public <T> RangeResponse<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = restTemplate.exchange(restTemplate.getUriTemplateHandler()
.expand(UriComponentsBuilder.fromPath(resource)
.queryParams(toMultiMap(params)).build().toString(), new HashMap<>()), HttpMethod.GET,
Expand All @@ -54,14 +56,14 @@ public <T> Page<T> search(String resource, Map<String, List<String>> params,
return Optional.of(response)
.map(HttpEntity::getBody)
.map(x -> {
PageImpl<T> page = new PageImpl<>(x, null, x.size(), 1);
List<String> contentRangeHeaders = response.getHeaders().get("Content-Range");
if (contentRangeHeaders != null) {
Range range = Range.of(contentRangeHeaders.stream().findFirst().toString());
page.withRange(range);
}
return (Page<T>) page;
}).orElse(Page.empty());
Range range = Optional.ofNullable(response.getHeaders().get("Content-Range"))
.map(List::stream)
.map(Stream::findFirst)
.filter(Optional::isPresent)
.map(Optional::get)
.map(Range::of).orElse(null);
return new RangeResponse<>(x, range);
}).orElse(new RangeResponse<>(List.of(), null));
}

@Override
Expand All @@ -74,8 +76,8 @@ public <T> List<T> post(String resource, List<Object> value, Map<String, List<St
public <T> List<T> patch(String resource, Map<String, List<String>> params, Object value, Map<String, List<String>> headers, Class<T> clazz) {
MultiValueMap<String, String> queryParams = toMultiMap(params);
return restTemplate.exchange(restTemplate.getUriTemplateHandler()
.expand(UriComponentsBuilder.fromPath(resource).queryParams(queryParams).build().toString(), new HashMap<>()),
HttpMethod.PATCH, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz))
.expand(UriComponentsBuilder.fromPath(resource).queryParams(queryParams).build().toString(), new HashMap<>()),
HttpMethod.PATCH, new HttpEntity<>(value, toHeaders(headers)), listRef(clazz))
.getBody();
}

Expand Down
4 changes: 2 additions & 2 deletions querydsl-postgrest-webclient-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>fr.ouestfrance.querydsl</groupId>
<artifactId>querydsl-postgrest-bom</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
</parent>

<artifactId>querydsl-postgrest-webclient-adapter</artifactId>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>6.1.2</version>
<version>6.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import fr.ouestfrance.querydsl.postgrest.model.Page;
import fr.ouestfrance.querydsl.postgrest.model.PageImpl;
import fr.ouestfrance.querydsl.postgrest.model.Range;
import fr.ouestfrance.querydsl.postgrest.model.RangeResponse;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.reflect.TypeUtils;
Expand All @@ -19,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

/**
* Rest interface for querying postgrest
Expand Down Expand Up @@ -48,8 +50,8 @@ public static PostgrestWebClient of(WebClient webClient) {
}

@Override
public <T> Page<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
public <T> RangeResponse<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz) {
ResponseEntity<List<T>> response = webClient.get().uri(uriBuilder -> {
uriBuilder.path(resource);
uriBuilder.queryParams(toMultiMap(params));
Expand All @@ -64,14 +66,14 @@ public <T> Page<T> search(String resource, Map<String, List<String>> params,
return Optional.ofNullable(response)
.map(HttpEntity::getBody)
.map(x -> {
PageImpl<T> page = new PageImpl<>(x, null, x.size(), 1);
List<String> contentRangeHeaders = response.getHeaders().get("Content-Range");
if (contentRangeHeaders != null && !contentRangeHeaders.isEmpty()) {
Range range = Range.of(contentRangeHeaders.stream().findFirst().toString());
page.withRange(range);
}
return (Page<T>) page;
}).orElse(Page.empty());
Range range = Optional.ofNullable(response.getHeaders().get("Content-Range"))
.map(List::stream)
.map(Stream::findFirst)
.filter(Optional::isPresent)
.map(Optional::get)
.map(Range::of).orElse(null);
return new RangeResponse<>(x, range);
}).orElse(new RangeResponse<>(List.of(), null));
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion querydsl-postgrest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>fr.ouestfrance.querydsl</groupId>
<artifactId>querydsl-postgrest-bom</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.4-SNAPSHOT</version>
</parent>

<artifactId>querydsl-postgrest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.ouestfrance.querydsl.postgrest;

import fr.ouestfrance.querydsl.postgrest.model.CountItem;
import fr.ouestfrance.querydsl.postgrest.model.Page;
import fr.ouestfrance.querydsl.postgrest.model.RangeResponse;

import java.util.List;
import java.util.Map;
Expand All @@ -21,8 +21,8 @@ public interface PostgrestClient {
* @return ResponseEntity containing the results
*/

<T> Page<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz);
<T> RangeResponse<T> search(String resource, Map<String, List<String>> params,
Map<String, List<String>> headers, Class<T> clazz);

/**
* Save body
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,14 @@ public Page<T> search(Object criteria, Pageable pageable) {
}
// Add select criteria
getSelects(criteria).ifPresent(queryParams::add);
Page<T> response = client.search(annotation.resource(), toMap(queryParams), headers, clazz);
if (response instanceof PageImpl<T> page) {
page.setPageable(pageable);
}
// Retrieve result headers
return response;
RangeResponse<T> response = client.search(annotation.resource(), toMap(queryParams), headers, clazz);

int pageSize = Optional.of(pageable)
.filter(Pageable::hasSize)
.map(Pageable::getPageSize)
.orElse(response.getPageSize());
// Compute PageResponse
return new PageImpl<>(response.data(), pageable, response.getTotalElements(), (int) Math.ceil((double) response.getTotalElements() / pageSize));
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package fr.ouestfrance.querydsl.postgrest.model;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;

Expand Down Expand Up @@ -101,11 +100,10 @@ default <U> Page<U> map(Function<T, U> converter) {

/**
* Check that page has next page
*
* @return true if totalPages is greater than pageNumber
*/
default boolean hasNext() {
return Optional.ofNullable(getPageable())
.map(pageable -> pageable.getPageNumber() +1 < getTotalPages())
.orElse(false);
return getPageable().getPageNumber() + 1 < getTotalPages();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class PageImpl<T> implements Page<T> {
/**
* Request information
*/
@Setter
private Pageable pageable;
/**
* Total elements
Expand All @@ -31,15 +30,4 @@ public class PageImpl<T> implements Page<T> {
* Total pages
*/
private int totalPages;

/**
* Apply range to a specific Page
* @param range range to apply
*/
public void withRange(Range range) {
totalElements = range.getTotalElements();
if (totalElements > 0) {
totalPages = (int) (totalElements / (range.getLimit() - range.getOffset()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ default int pageLimit() {

/**
* Return the next page
*
* @return next page
*/
default Pageable next() {
Expand All @@ -107,9 +108,19 @@ default Pageable next() {

/**
* Return the previous page or the first one
*
* @return previous page
*/
default Pageable previous() {
return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
}

/**
* Indicates that the pageable object is unPaged
*
* @return true if unPaged
*/
default boolean hasSize() {
return getPageSize() > 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fr.ouestfrance.querydsl.postgrest.model;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -9,6 +11,8 @@
* Range object
*/
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Range {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.ouestfrance.querydsl.postgrest.model;

import java.util.List;

/**
* Postgrest range response
*
* @param <T> type of the page
* @param data Elements returned
* @param range Range returned (first returned, last returned, total elements)
*/
public record RangeResponse<T>(List<T> data, Range range) {

/**
* Create a range response from a list of elements
*
* @param items elements
* @param <T> type of the elements
* @return range response
*/
@SafeVarargs
public static <T> RangeResponse<T> of(T... items) {
return new RangeResponse<>(List.of(items), null);
}

/**
* Retrieve total elements from range response
*
* @return total elements
*/
public long getTotalElements() {
return (range != null) ? range.getTotalElements() : data.size();
}

/**
* Retrieve page size from range response
*
* @return page size
*/
public int getPageSize() {
if (range != null) {
return range.getLimit() - range.getOffset() + 1;
}
return data.isEmpty() ? 1 : data.size();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import fr.ouestfrance.querydsl.postgrest.builders.QueryFilterVisitor;
import fr.ouestfrance.querydsl.postgrest.model.Filter;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand Down
Loading

0 comments on commit a3aa53f

Please sign in to comment.