pagination in community-projects api

This commit is contained in:
Michele Artini 2023-06-20 09:10:14 +02:00
parent 6e8f461bec
commit f3ac95b54f
3 changed files with 41 additions and 23 deletions

View File

@ -12,11 +12,13 @@ import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import eu.dnetlib.openaire.community.db.CommunityService;
@ -93,7 +95,7 @@ public class CommunityApiController {
communityService.setCommunity(id, properties);
}
@RequestMapping(value = "/community/{id}/projects", produces = {
@RequestMapping(value = "/community/{id}/projects/{page}/{size}", produces = {
"application/json"
}, method = RequestMethod.GET)
@Operation(summary = "get community projects", description = "get community projects", tags = {
@ -104,8 +106,9 @@ public class CommunityApiController {
@ApiResponse(responseCode = "404", description = "not found"),
@ApiResponse(responseCode = "500", description = "unexpected error")
})
public List<CommunityProject> getCommunityProjects(@PathVariable final String id) throws CommunityException, ResourceNotFoundException {
return communityService.getCommunityProjects(id);
public Page<CommunityProject> getCommunityProjects(@PathVariable final String id, @PathVariable final int page, final int size)
throws CommunityException, ResourceNotFoundException {
return communityService.getCommunityProjects(id, page, size);
}
@RequestMapping(value = "/community/{id}/projects", produces = {
@ -477,9 +480,10 @@ public class CommunityApiController {
})
public CommunityDetails addCommunityZenodoCommunity(
@PathVariable final String id,
@RequestParam(required = false, defaultValue = "false") final boolean main,
@RequestBody final String zenodocommunity) throws CommunityException, ResourceNotFoundException {
return communityService.addCommunityZenodoCommunity(id, zenodocommunity);
return communityService.addCommunityZenodoCommunity(id, zenodocommunity, main);
}
@ -496,10 +500,10 @@ public class CommunityApiController {
})
public void removeCommunityZenodoCommunity(
@PathVariable final String id,
@RequestParam(required = false, defaultValue = "false") final boolean main,
@RequestBody final String zenodoCommId) throws CommunityException, ResourceNotFoundException {
communityService.removeCommunityZenodoCommunity(id, zenodoCommId);
communityService.removeCommunityZenodoCommunity(id, zenodoCommId, main);
}
@RequestMapping(value = "/community/{zenodoId}/openairecommunities", produces = {

View File

@ -12,6 +12,8 @@ import javax.transaction.Transactional;
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.stereotype.Service;
import eu.dnetlib.openaire.community.db.model.DbCommunity;
@ -44,9 +46,7 @@ public class CommunityService {
// 1) Subcommunities: modello e repository
// 2) Subcommunities: importazione tramite profili
// 3) Subcommunities: visualizzazione tramite le context api
// 4) nuova api per settare the mainzenodocommunity
// 5) ricontrollare se tutti i campi del database sono esposti dalle api (modello + mapping)
// 6) Gestire paginazione dei progetti -- getCommunityProjects deve ritornare una pagina
@Autowired
private DbCommunityRepository dbCommunityRepository;
@ -81,11 +81,8 @@ public class CommunityService {
// TODO Auto-generated method stub
}
public List<CommunityProject> getCommunityProjects(final String id) throws CommunityException, ResourceNotFoundException {
return dbProjectRepository.findByCommunity(id)
.stream()
.map(ConvertionUtils::toCommunityProject)
.collect(Collectors.toList());
public Page<CommunityProject> getCommunityProjects(final String id, final int page, final int size) throws CommunityException, ResourceNotFoundException {
return dbProjectRepository.findByCommunity(id, PageRequest.of(page, size)).map(ConvertionUtils::toCommunityProject);
}
public CommunityProject addCommunityProject(final String id, final CommunityProject project) throws CommunityException, ResourceNotFoundException {
@ -103,7 +100,7 @@ public class CommunityService {
dbProjectRepository.saveAll(list);
return getCommunityProjects(id);
return projectList;
}
public void removeCommunityProject(final String id, final String projectId) throws CommunityException, ResourceNotFoundException {
@ -140,7 +137,7 @@ public class CommunityService {
dbDatasourceRepository.saveAll(list);
return getCommunityContentproviders(id);
return contentprovidersList;
}
public void removeCommunityContentProvider(final String id, final String contentproviderId) throws CommunityException, ResourceNotFoundException {
@ -182,7 +179,7 @@ public class CommunityService {
dbSupportOrgRepository.saveAll(list);
return getCommunityOrganizations(id);
return orgList;
}
@Transactional
@ -232,14 +229,31 @@ public class CommunityService {
return getCommunity(id);
}
public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity) throws CommunityException, ResourceNotFoundException {
public CommunityDetails removeCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
throws CommunityException, ResourceNotFoundException {
if (isMain) {
return updateElementToSimpleField(id, c -> c.getMainZenodoCommunity(), (c, val) -> c.setMainZenodoCommunity(val), null);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), true, zenodoCommunity);
}
}
public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity)
public CommunityDetails addCommunityZenodoCommunity(final String id, final String zenodoCommunity, final boolean isMain)
throws CommunityException, ResourceNotFoundException {
if (isMain) {
return updateElementToSimpleField(id, c -> c.getMainZenodoCommunity(), (c, val) -> c.setMainZenodoCommunity(val), zenodoCommunity);
} else {
return modifyElementToArrayField(id, c -> c.getOtherZenodoCommunities(), (c, arr) -> c.setOtherZenodoCommunities(arr), false, zenodoCommunity);
}
}
private CommunityDetails updateElementToSimpleField(final String id,
final Function<DbCommunity, String> getter,
final BiConsumer<DbCommunity, String> setter,
final String value) {
// TODO Auto-generated method stub
return null;
}
private CommunityDetails modifyElementToArrayField(final String id,
final Function<DbCommunity, String[]> getter,

View File

@ -1,8 +1,8 @@
package eu.dnetlib.openaire.community.db.repository;
import java.util.List;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
import eu.dnetlib.openaire.community.db.model.DbProject;
@ -11,6 +11,6 @@ import eu.dnetlib.openaire.community.db.model.DbProjectPK;
@ConditionalOnProperty(value = "openaire.exporter.enable.community", havingValue = "true")
public interface DbProjectRepository extends JpaRepository<DbProject, DbProjectPK> {
List<DbProject> findByCommunity(String community);
Page<DbProject> findByCommunity(String community, PageRequest page);
}