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

Optional sort in REST Data Panache #17274

Merged
merged 2 commits into from
Jul 7, 2021
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 @@ -17,6 +17,15 @@ public interface DataAccessImplementor {
*/
ResultHandle findById(BytecodeCreator creator, ResultHandle id);

/**
* Find all entities.
*
* @param creator Bytecode creator that should be used for implementation.
* @param page Page instance that should be used in a query. Might be null if pagination is disabled.
* @return Entity list
*/
ResultHandle findAll(BytecodeCreator creator, ResultHandle page);

/**
* Find all entities.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
id);
}

/**
* Implements <code>Entity.findAll().page(page).list()</code>
*/
@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeStaticMethod(ofMethod(entityClassName, "findAll", PanacheQuery.class));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query,
page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

/**
* Implements <code>Entity.findAll(sort).page(page).list()</code>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
getRepositoryInstance(creator), id);
}

/**
* Implements <code>repository.findAll().page(page).list()</code>
*/
@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeInterfaceMethod(
ofMethod(PanacheRepositoryBase.class, "findAll", PanacheQuery.class), getRepositoryInstance(creator));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query, page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

/**
* Implements <code>repository.findAll(sort).page(page).list()</code>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.hibernate.orm.rest.data.panache.deployment;

import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -8,6 +10,7 @@
import org.jboss.jandex.FieldInfo;
import org.jboss.logging.Logger;

import io.quarkus.gizmo.BranchResult;
import io.quarkus.gizmo.BytecodeCreator;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
Expand Down Expand Up @@ -65,7 +68,13 @@ private void implementList(ClassCreator classCreator, DataAccessImplementor data
MethodCreator methodCreator = classCreator.getMethodCreator("list", List.class, Page.class, Sort.class);
ResultHandle page = methodCreator.getMethodParam(0);
ResultHandle sort = methodCreator.getMethodParam(1);
methodCreator.returnValue(dataAccessImplementor.findAll(methodCreator, page, sort));
ResultHandle columns = methodCreator.invokeVirtualMethod(ofMethod(Sort.class, "getColumns", List.class), sort);
ResultHandle isEmptySort = methodCreator.invokeInterfaceMethod(ofMethod(List.class, "isEmpty", boolean.class), columns);

BranchResult isEmptySortBranch = methodCreator.ifTrue(isEmptySort);
isEmptySortBranch.trueBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.trueBranch(), page));
isEmptySortBranch.falseBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.falseBranch(), page, sort));

methodCreator.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ public interface DataAccessImplementor {
*/
ResultHandle findById(BytecodeCreator creator, ResultHandle id);

/**
* Find all entities.
*
* @param creator Bytecode creator that should be used for implementation.
* @param page Page instance that should be used in a query. Might be null if pagination is disabled.
* @return Entity list
*/
ResultHandle findAll(BytecodeCreator creator, ResultHandle page);

/**
* Find all entities.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
ofMethod(entityClassName, "findById", PanacheMongoEntityBase.class, Object.class), id);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeStaticMethod(ofMethod(entityClassName, "findAll", PanacheQuery.class));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query,
page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page, ResultHandle sort) {
ResultHandle query = creator.invokeStaticMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public ResultHandle findById(BytecodeCreator creator, ResultHandle id) {
getRepositoryInstance(creator), id);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page) {
ResultHandle query = creator.invokeInterfaceMethod(
ofMethod(PanacheMongoRepositoryBase.class, "findAll", PanacheQuery.class),
getRepositoryInstance(creator));
creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "page", PanacheQuery.class, Page.class), query,
page);
return creator.invokeInterfaceMethod(ofMethod(PanacheQuery.class, "list", List.class), query);
}

@Override
public ResultHandle findAll(BytecodeCreator creator, ResultHandle page, ResultHandle sort) {
ResultHandle query = creator.invokeInterfaceMethod(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package io.quarkus.mongodb.rest.data.panache.deployment;

import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import org.jboss.jandex.FieldInfo;
import org.jboss.logging.Logger;

import io.quarkus.gizmo.BranchResult;
import io.quarkus.gizmo.BytecodeCreator;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.ClassOutput;
Expand Down Expand Up @@ -60,7 +63,13 @@ private void implementList(ClassCreator classCreator, DataAccessImplementor data
MethodCreator methodCreator = classCreator.getMethodCreator("list", List.class, Page.class, Sort.class);
ResultHandle page = methodCreator.getMethodParam(0);
ResultHandle sort = methodCreator.getMethodParam(1);
methodCreator.returnValue(dataAccessImplementor.findAll(methodCreator, page, sort));
ResultHandle columns = methodCreator.invokeVirtualMethod(ofMethod(Sort.class, "getColumns", List.class), sort);
ResultHandle isEmptySort = methodCreator.invokeInterfaceMethod(ofMethod(List.class, "isEmpty", boolean.class), columns);

BranchResult isEmptySortBranch = methodCreator.ifTrue(isEmptySort);
isEmptySortBranch.trueBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.trueBranch(), page));
isEmptySortBranch.falseBranch().returnValue(dataAccessImplementor.findAll(isEmptySortBranch.falseBranch(), page, sort));

methodCreator.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ insert into author(id, name, dob) values (nextval('hibernate_sequence'), 'Fyodor

insert into book(id, title, author_id) values (nextval('hibernate_sequence'), 'Crime and Punishment', 1);
insert into book(id, title, author_id) values (nextval('hibernate_sequence'), 'Idiot', 1);
insert into book(id, title, author_id) values (nextval('hibernate_sequence'), 'Demons', 1);
insert into book(id, title, author_id) values (nextval('hibernate_sequence'), 'The adolescent', 1);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class HibernateOrmRestDataPanacheTest {

private static final String IDIOT_TITLE = "Idiot";

private static final int DEMONS_ID = 4;

private static final String DEMONS_TITLE = "Demons";

private static final int THE_ADOLESCENT_ID = 5;

private static final String THE_ADOLESCENT_TITLE = "The adolescent";

@Test
void shouldGetAuthor() {
given().accept("application/json")
Expand Down Expand Up @@ -94,31 +102,81 @@ void shouldListBooks() {
given().accept("application/json")
.when().get("/books")
.then().statusCode(200)
.and().body("id", contains(CRIME_AND_PUNISHMENT_ID, IDIOT_ID))
.and().body("title", contains(CRIME_AND_PUNISHMENT_TITLE, IDIOT_TITLE))
.and().body("author.id", contains(DOSTOEVSKY_ID, DOSTOEVSKY_ID))
.and().body("author.name", contains(DOSTOEVSKY_NAME, DOSTOEVSKY_NAME))
.and().body("author.dob", contains(DOSTOEVSKY_DOB, DOSTOEVSKY_DOB));
.and().body("id", contains(CRIME_AND_PUNISHMENT_ID, IDIOT_ID, DEMONS_ID, THE_ADOLESCENT_ID))
.and().body("title", contains(CRIME_AND_PUNISHMENT_TITLE, IDIOT_TITLE, DEMONS_TITLE, THE_ADOLESCENT_TITLE))
.and().body("author.id", contains(DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID))
.and().body("author.name", contains(DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME))
.and().body("author.dob", contains(DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB));
given().accept("application/json")
.when().get("/books?sort=title")
.then().statusCode(200)
.and().body("id", contains(CRIME_AND_PUNISHMENT_ID, DEMONS_ID, IDIOT_ID, THE_ADOLESCENT_ID))
.and().body("title", contains(CRIME_AND_PUNISHMENT_TITLE, DEMONS_TITLE, IDIOT_TITLE, THE_ADOLESCENT_TITLE))
.and().body("author.id", contains(DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID))
.and().body("author.name", contains(DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME))
.and().body("author.dob", contains(DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB));
}

@Test
void shouldListBooksHal() {
given().accept("application/hal+json")
.when().get("/books")
.then().statusCode(200)
.and().body("_embedded.books.id", contains(CRIME_AND_PUNISHMENT_ID, IDIOT_ID))
.and().body("_embedded.books.title", contains(CRIME_AND_PUNISHMENT_TITLE, IDIOT_TITLE))
.and().body("_embedded.books.author.id", contains(DOSTOEVSKY_ID, DOSTOEVSKY_ID))
.and().body("_embedded.books.author.name", contains(DOSTOEVSKY_NAME, DOSTOEVSKY_NAME))
.and().body("_embedded.books.author.dob", contains(DOSTOEVSKY_DOB, DOSTOEVSKY_DOB))
.and().body("_embedded.books._links.add.href", contains(endsWith("/books"), endsWith("/books")))
.and().body("_embedded.books._links.list.href", contains(endsWith("/books"), endsWith("/books")))
.and().body("_embedded.books.id", contains(CRIME_AND_PUNISHMENT_ID, IDIOT_ID, DEMONS_ID, THE_ADOLESCENT_ID))
.and()
.body("_embedded.books.title",
contains(CRIME_AND_PUNISHMENT_TITLE, IDIOT_TITLE, DEMONS_TITLE, THE_ADOLESCENT_TITLE))
.and().body("_embedded.books.author.id", contains(DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID))
.and()
.body("_embedded.books.author.name",
contains(DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME))
.and()
.body("_embedded.books.author.dob", contains(DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB))
.and()
.body("_embedded.books._links.add.href",
contains(endsWith("/books"), endsWith("/books"), endsWith("/books"), endsWith("/books")))
.and()
.body("_embedded.books._links.list.href",
contains(endsWith("/books"), endsWith("/books"), endsWith("/books"), endsWith("/books")))
.and().body("_embedded.books._links.self.href",
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + IDIOT_ID),
endsWith("/books/" + DEMONS_ID), endsWith("/books/" + THE_ADOLESCENT_ID)))
.and().body("_embedded.books._links.update.href",
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + IDIOT_ID),
endsWith("/books/" + DEMONS_ID), endsWith("/books/" + THE_ADOLESCENT_ID)))
.and().body("_embedded.books._links.remove.href",
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + IDIOT_ID),
endsWith("/books/" + DEMONS_ID), endsWith("/books/" + THE_ADOLESCENT_ID)))
.and().body("_links.add.href", endsWith("/books"))
.and().body("_links.list.href", endsWith("/books"));
given().accept("application/hal+json")
.when().get("/books?sort=title")
.then().statusCode(200)
.and().body("_embedded.books.id", contains(CRIME_AND_PUNISHMENT_ID, DEMONS_ID, IDIOT_ID, THE_ADOLESCENT_ID))
.and()
.body("_embedded.books.title",
contains(CRIME_AND_PUNISHMENT_TITLE, DEMONS_TITLE, IDIOT_TITLE, THE_ADOLESCENT_TITLE))
.and().body("_embedded.books.author.id", contains(DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID, DOSTOEVSKY_ID))
.and()
.body("_embedded.books.author.name",
contains(DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME, DOSTOEVSKY_NAME))
.and()
.body("_embedded.books.author.dob", contains(DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB, DOSTOEVSKY_DOB))
.and()
.body("_embedded.books._links.add.href",
contains(endsWith("/books"), endsWith("/books"), endsWith("/books"), endsWith("/books")))
.and()
.body("_embedded.books._links.list.href",
contains(endsWith("/books"), endsWith("/books"), endsWith("/books"), endsWith("/books")))
.and().body("_embedded.books._links.self.href",
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + IDIOT_ID)))
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + DEMONS_ID),
endsWith("/books/" + IDIOT_ID), endsWith("/books/" + THE_ADOLESCENT_ID)))
.and().body("_embedded.books._links.update.href",
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + IDIOT_ID)))
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + DEMONS_ID),
endsWith("/books/" + IDIOT_ID), endsWith("/books/" + THE_ADOLESCENT_ID)))
.and().body("_embedded.books._links.remove.href",
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + IDIOT_ID)))
contains(endsWith("/books/" + CRIME_AND_PUNISHMENT_ID), endsWith("/books/" + DEMONS_ID),
endsWith("/books/" + IDIOT_ID), endsWith("/books/" + THE_ADOLESCENT_ID)))
.and().body("_links.add.href", endsWith("/books"))
.and().body("_links.list.href", endsWith("/books"));
}
Expand Down