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

Add sorting entity feature #107

Merged
merged 1 commit into from
Nov 6, 2020
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
5 changes: 5 additions & 0 deletions generators/entity-server/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ const serverFiles = {
file: 'package/web/rest/vm/PageRequestVM.java',
renameTo: generator => `${generator.packageFolder}/web/rest/vm/PageRequestVM.java`,
useBluePrint: true
},
{
file: 'package/web/rest/vm/SortRequestVM.java',
renameTo: generator => `${generator.packageFolder}/web/rest/vm/SortRequestVM.java`,
useBluePrint: true
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
@Transactional
<%_ } _%>
<%_ if (hasPagination) { _%>
public Response getAll<%= entityClassPlural %>(@BeanParam PageRequestVM pageRequest, @Context UriInfo uriInfo<% if (reactive) { %>, ServerHttpRequest request<% } %><% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) String filter<% } %><% if (fieldsContainOwnerManyToMany) { %>, @QueryParam(value = "eagerload") boolean eagerload<% } %>) {
public Response getAll<%= entityClassPlural %>(@BeanParam PageRequestVM pageRequest, @BeanParam SortRequestVM sortRequest, @Context UriInfo uriInfo<% if (reactive) { %>, ServerHttpRequest request<% } %><% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) String filter<% } %><% if (fieldsContainOwnerManyToMany) { %>, @QueryParam(value = "eagerload") boolean eagerload<% } %>) {
log.debug("REST request to get a page of <%= entityClassPlural %>");
var page = pageRequest.toPage();
var sort = sortRequest.toSort();
<%_ if (viaService) { _%>
<%_ if (fieldsContainOwnerManyToMany) { _%>
Paged<<%= entityOrDtoClass %>> result;
Expand All @@ -43,7 +44,7 @@
var pageCount = <%= dataAccessObject %>.findAll().page(page).pageCount();
result = new Paged<>(page.index, page.size, totalCount, pageCount, <%= entityInstancePlural %>);
} else {
result = new Paged<>(<%= dataAccessObject %>.findAll().page(page));
result = new Paged<>(<%= dataAccessObject %>.findAll(sort).page(page));
}
<%_ } else { _%>
result = <%= dataAccessObject %>.findAll(page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
@Transactional
<%_ } _%>
<%_ if (hasPagination) { _%>
public Response getAll<%= entityClassPlural %>(@BeanParam PageRequestVM pageRequest, @Context UriInfo uriInfo<% if (reactive) { %>, ServerHttpRequest request<% } %><% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) String filter<% } %><% if (fieldsContainOwnerManyToMany) { %>, @QueryParam(value = "eagerload") boolean eagerload<% } %>) {
public Response getAll<%= entityClassPlural %>(@BeanParam PageRequestVM pageRequest, @BeanParam SortRequestVM sortRequest, @Context UriInfo uriInfo<% if (reactive) { %>, ServerHttpRequest request<% } %><% if (fieldsContainNoOwnerOneToOne) { %>, @RequestParam(required = false) String filter<% } %><% if (fieldsContainOwnerManyToMany) { %>, @QueryParam(value = "eagerload") boolean eagerload<% } %>) {
log.debug("REST request to get a page of <%= entityClassPlural %>");
var page = pageRequest.toPage();
var sort = sortRequest.toSort();
<%_ if (viaService) { _%>
<%_ if (fieldsContainOwnerManyToMany) { _%>
Paged<<%= entityOrDtoClass %>> result;
Expand All @@ -54,10 +55,10 @@
var pageCount = <%= dataAccessObject %>.findAll().page(page).pageCount();
result = new Paged<>(page.index, page.size, totalCount, pageCount, <%= entityInstancePlural %>);
} else {
result = new Paged<>(<%= dataAccessObject %>.findAll().page(page));
result = new Paged<>(<%= dataAccessObject %>.findAll(sort).page(page));
}
<%_ } else { _%>
var result = new Paged<>(<%= dataAccessObject %>.findAll().page(page));
var result = new Paged<>(<%= dataAccessObject %>.findAll(sort).page(page));
<%_ } _%>
<%_ if (hasDto) { _%>
result = result.map(<%= entityInstance %> -> <%= mapper %>.toDto(<%= entityInstance %>));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import org.slf4j.LoggerFactory;
<%_ if (hasPagination) { _%>
import <%= packageName %>.service.Paged;
import <%= packageName %>.web.rest.vm.PageRequestVM;
import <%= packageName %>.web.rest.vm.SortRequestVM;
import <%= packageName %>.web.util.PaginationUtil;
<%_ } _%>
<%_ if (reactiveRepositories) { _%>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<%#
Copyright 2013-2020 the original author or authors from the JHipster project.

This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-%>
package <%= packageName %>.web.rest.vm;

import io.quarkus.panache.common.Sort;

import javax.ws.rs.QueryParam;
import java.util.List;

public class SortRequestVM {

@QueryParam("sort")
public List<String> sort;

public Sort toSort() {
Sort resultSort = null;

for(String currentSort : sort) {
var sortDetails = currentSort.split(",");
var columnName = sortDetails[0];
var direction = sortDetails.length > 1 ? sortDetails[1] : null;

if(resultSort == null) {
resultSort = Sort.by(columnName, toDirection(direction));
} else {
resultSort.and(columnName, toDirection(direction));
}
}

return resultSort;
}

private Sort.Direction toDirection(String direction) {
return "asc".equals(direction) ? Sort.Direction.Ascending : Sort.Direction.Descending;
}

@Override
public String toString() {
return "SortRequestVM{" +
"sort='" + sort + '\'' +
'}';
}
}
4 changes: 3 additions & 1 deletion test/entity.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,13 @@ describe('Subgenerator entity of quarkus JHipster blueprint', () => {

assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/Paged.java`);
assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/vm/PageRequestVM.java`);
assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/vm/SortRequestVM.java`);
assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/util/PaginationUtil.java`);
});
it('update Web finder signature', () => {
assert.fileContent(
`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/FooResource.java`,
'public Response getAllFoos(@BeanParam PageRequestVM pageRequest, @Context UriInfo uriInfo)'
'public Response getAllFoos(@BeanParam PageRequestVM pageRequest, @BeanParam SortRequestVM sortRequest, @Context UriInfo uriInfo)'
);
});
});
Expand Down Expand Up @@ -309,6 +310,7 @@ describe('Subgenerator entity of quarkus JHipster blueprint', () => {

assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/service/Paged.java`);
assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/vm/PageRequestVM.java`);
assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/rest/vm/SortRequestVM.java`);
assert.file(`${SERVER_MAIN_SRC_DIR}com/mycompany/myapp/web/util/PaginationUtil.java`);
});
it('update service finder signature', () => {
Expand Down