add orderBy parameter in community projects api
This commit is contained in:
parent
d5fd29c3d0
commit
807d2e493b
|
@ -142,9 +142,10 @@ public class CommunityApiController extends AbstractDnetController {
|
|||
@PathVariable final Integer page,
|
||||
@PathVariable final Integer size,
|
||||
@RequestParam(required = false) final String funder,
|
||||
@RequestParam(required = false) final String searchFilter)
|
||||
@RequestParam(required = false) final String searchFilter,
|
||||
@RequestParam(required = false) final String orderBy)
|
||||
throws CommunityException {
|
||||
return communityService.getCommunityProjects(id, funder, searchFilter, page, size);
|
||||
return communityService.getCommunityProjects(id, funder, searchFilter, page, size, orderBy);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/community/{id}/projects", produces = {
|
||||
|
|
|
@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
@ -133,15 +134,37 @@ public class CommunityService {
|
|||
}
|
||||
|
||||
@Transactional
|
||||
public Page<CommunityProject> getCommunityProjects(final String id, final String funder, final String filter, final int page, final int size)
|
||||
public Page<CommunityProject> getCommunityProjects(final String id,
|
||||
final String funder,
|
||||
final String filter,
|
||||
final int page,
|
||||
final int size,
|
||||
final String orderBy)
|
||||
|
||||
throws CommunityException {
|
||||
if (StringUtils.isBlank(id)) { throw new CommunityException("Empty ID"); }
|
||||
try {
|
||||
final Sort sort;
|
||||
if (StringUtils.isBlank(orderBy)) {
|
||||
sort = Sort.by("projectName");
|
||||
} else if (orderBy.equalsIgnoreCase("funder")) {
|
||||
sort = Sort.by("projectFunder").and(Sort.by("projectName"));
|
||||
} else if (orderBy.equalsIgnoreCase("grantId")) {
|
||||
sort = Sort.by("projectCode");
|
||||
} else if (orderBy.equalsIgnoreCase("acronym")) {
|
||||
sort = Sort.by("projectAcronym");
|
||||
} else if (orderBy.equalsIgnoreCase("openaireId")) {
|
||||
sort = Sort.by("projectId");
|
||||
} else {
|
||||
sort = Sort.by("projectName");
|
||||
}
|
||||
|
||||
final PageRequest pageable = PageRequest.of(page, size, sort);
|
||||
if (StringUtils.isAllBlank(filter, funder)) {
|
||||
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
|
||||
return dbProjectRepository.findByCommunity(id, pageable).map(CommunityMappingUtils::toCommunityProject);
|
||||
} else {
|
||||
final Specification<DbProject> projSpec = prepareProjectSpec(id, funder, filter);
|
||||
return dbProjectRepository.findAll(projSpec, PageRequest.of(page, size)).map(CommunityMappingUtils::toCommunityProject);
|
||||
return dbProjectRepository.findAll(projSpec, pageable).map(CommunityMappingUtils::toCommunityProject);
|
||||
}
|
||||
} catch (final Throwable e) {
|
||||
log.error(e);
|
||||
|
|
Loading…
Reference in New Issue