Adjust /{id} routes to return json format

This commit is contained in:
Serafeim Chatzopoulos 2024-06-14 18:16:05 +03:00
parent 03da473fe0
commit d838a051c4
10 changed files with 29 additions and 18 deletions

View File

@ -47,7 +47,7 @@ public class DataSourceController
@ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") })
}) })
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the data source") String id) { public Datasource getById(@PathVariable @Parameter(description = "The OpenAIRE id of the data source") String id) {
return dataSourceService.getById(id); return dataSourceService.getById(id);
} }

View File

@ -47,7 +47,7 @@ public class OrganizationController
@ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") })
}) })
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) { public Organization getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) {
return organizationService.getById(id); return organizationService.getById(id);
} }

View File

@ -47,7 +47,7 @@ public class ProjectController
@ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") })
}) })
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) { public Project getById(@PathVariable @Parameter(description = "The OpenAIRE id of the project") String id) {
return projectService.getById(id); return projectService.getById(id);
} }

View File

@ -47,7 +47,7 @@ public class ResearchProductsController
@ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") })
}) })
@GetMapping(value = "/{id}") @GetMapping(value = "/{id}")
public Object getById(@PathVariable @Parameter(description = "The OpenAIRE id of the research product") String id) { public GraphResult getById(@PathVariable @Parameter(description = "The OpenAIRE id of the research product") String id) {
return researchProductService.getById(id); return researchProductService.getById(id);
} }
@ -61,7 +61,7 @@ public class ResearchProductsController
@ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") }) @ApiResponse(responseCode = "500", content = { @Content(schema = @Schema(implementation = ErrorResponse.class), mediaType = "application/json") })
}) })
@GetMapping(value = "") @GetMapping(value = "")
public Object search(@Valid @ParameterObject final ResearchProductsRequest request, BindingResult validationResult) { public SearchResponse<GraphResult> search(@Valid @ParameterObject final ResearchProductsRequest request, BindingResult validationResult) {
if (validationResult.hasErrors()) { if (validationResult.hasErrors()) {
throw new BadRequestException(RequestValidator.getErrorMessage(validationResult.getAllErrors())); throw new BadRequestException(RequestValidator.getErrorMessage(validationResult.getAllErrors()));

View File

@ -32,4 +32,18 @@ public class ResponseResultsMapper {
.map(mapFunction) .map(mapFunction)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public <T> T toSingleResult(String jsonPayload, Function<SolrRecord, T> mapFunction) throws RuntimeException {
try {
// Deserialize the JSON field to SolrRecord
SolrRecord solrRecord = objectMapper.readValue(jsonPayload, SolrRecord.class);
// Apply the mapFunction to transform SolrRecord to T
return mapFunction.apply(solrRecord);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error deserializing JSON", e);
}
}
} }

View File

@ -16,7 +16,6 @@ import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -38,14 +37,14 @@ public class DataSourceService {
private final Logger log = LogManager.getLogger(this.getClass()); private final Logger log = LogManager.getLogger(this.getClass());
@SneakyThrows @SneakyThrows
public SolrDocument getById(String id) { public Datasource getById(String id) {
var doc = solrRepository.getById(id); var doc = solrRepository.getById(id);
if (doc == null) { if (doc == null) {
throw new NotFoundException("Data source with id: " + id + " not found"); throw new NotFoundException("Data source with id: " + id + " not found");
} }
return doc; return responseResultsMapper.toSingleResult(doc.get("__json").toString(), datasourceMapper::toGraphDatasource);
} }
@SneakyThrows @SneakyThrows

View File

@ -16,7 +16,6 @@ import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -38,14 +37,14 @@ public class OrganizationService {
private final Logger log = LogManager.getLogger(this.getClass()); private final Logger log = LogManager.getLogger(this.getClass());
@SneakyThrows @SneakyThrows
public SolrDocument getById(String id) { public Organization getById(String id) {
var doc = solrRepository.getById(id); var doc = solrRepository.getById(id);
if (doc == null) { if (doc == null) {
throw new NotFoundException("Organization with id: " + id + " not found"); throw new NotFoundException("Organization with id: " + id + " not found");
} }
return doc; return responseResultsMapper.toSingleResult(doc.get("__json").toString(), organizationMapper::toGraphOrganization);
} }
@SneakyThrows @SneakyThrows

View File

@ -16,7 +16,6 @@ import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -38,14 +37,14 @@ public class ProjectService {
private final Logger log = LogManager.getLogger(this.getClass()); private final Logger log = LogManager.getLogger(this.getClass());
@SneakyThrows @SneakyThrows
public SolrDocument getById(String id) { public Project getById(String id) {
var doc = solrRepository.getById(id); var doc = solrRepository.getById(id);
if (doc == null) { if (doc == null) {
throw new NotFoundException("Project with id: " + id + " not found"); throw new NotFoundException("Project with id: " + id + " not found");
} }
return doc; return responseResultsMapper.toSingleResult(doc.get("__json").toString(), projectMapper::toGraphProject);
} }
@SneakyThrows @SneakyThrows

View File

@ -16,7 +16,6 @@ import lombok.SneakyThrows;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
@ -38,14 +37,15 @@ public class ResearchProductService {
private final Logger log = LogManager.getLogger(this.getClass()); private final Logger log = LogManager.getLogger(this.getClass());
@SneakyThrows @SneakyThrows
public SolrDocument getById(String id) { public GraphResult getById(String id) {
var doc = solrRepository.getById(id); var doc = solrRepository.getById(id);
if (doc == null) { if (doc == null) {
throw new NotFoundException("Research product with id: " + id + " not found"); throw new NotFoundException("Research product with id: " + id + " not found");
} }
return doc; return responseResultsMapper.toSingleResult(doc.get("__json").toString(), researchProductMapper::toGraphResult);
} }
@SneakyThrows @SneakyThrows

View File

@ -4,7 +4,7 @@ import lombok.Data;
import org.apache.solr.client.solrj.beans.Field; import org.apache.solr.client.solrj.beans.Field;
@Data @Data
public class SolrResponseBean<T> { public class SolrResponseBean {
@Field("__json") @Field("__json")
private String jsonField; private String jsonField;