Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider supporting Sort object in query method parameters #23

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading