Skip to content

Commit

Permalink
Consider supporting Sort object in query method parameters
Browse files Browse the repository at this point in the history
Closes gh-22
  • Loading branch information
evgeniycheban committed Dec 29, 2024
1 parent db4fe82 commit 4f596bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import ru.rt.restream.reindexer.ReindexerIndex;
import ru.rt.restream.reindexer.ReindexerNamespace;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.repository.query.ParametersParameterAccessor;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.parser.Part;
Expand Down Expand Up @@ -108,6 +110,12 @@ private Query<?> createQuery(Object[] parameters) {
}
base = criteria.or();
}
if (this.queryMethod.getParameters().hasSortParameter()) {
Sort sort = (Sort) parameters[this.queryMethod.getParameters().getSortIndex()];
for (Order order : sort) {
base = base.sort(order.getProperty(), order.isDescending());
}
}
return base;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.reindexer.ReindexerTransactionManager;
import org.springframework.data.reindexer.core.mapping.Namespace;
import org.springframework.data.reindexer.core.mapping.Query;
Expand Down Expand Up @@ -661,6 +663,36 @@ public void countByValue() {
assertEquals(2, this.repository.countByValue("TestValue"));
}

@Test
public void findAllByIdInSortedByIdInAscOrder() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findAllByIdIn(expectedItems.stream()
.map(TestItem::getId)
.toList(), Sort.by(Direction.ASC, "id"));
assertEquals(expectedItems.size(), foundItems.size());
for (int i = 0; i < expectedItems.size(); i++) {
assertEquals(expectedItems.get(i), foundItems.get(i));
}
}

@Test
public void findAllByIdInSortedByIdInDescOrder() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findAllByIdIn(expectedItems.stream()
.map(TestItem::getId)
.toList(), Sort.by(Direction.DESC, "id"));
assertEquals(expectedItems.size(), foundItems.size());
for (int i = 0; i < expectedItems.size(); i++) {
assertEquals(expectedItems.get(i), foundItems.get(foundItems.size() - 1 - i));
}
}

@Test
public void findByEnumStringIn() {
List<TestItem> expectedItems = new ArrayList<>();
Expand Down Expand Up @@ -847,6 +879,8 @@ Optional<TestItem> findOneSqlByNameAndValueManyParams(String name1, String name2
boolean existsByName(String name);

int countByValue(String name);

List<TestItem> findAllByIdIn(List<Long> ids, Sort sort);
}

@Namespace(name = NAMESPACE_NAME)
Expand Down

0 comments on commit 4f596bb

Please sign in to comment.