Skip to content

Commit

Permalink
Add get /api/sources/{sourceIdentifier} to be able to get a source by…
Browse files Browse the repository at this point in the history
… id.

Moved /api/sources/{type} to Moved /api/sources?type=type as it conflicted with /api/sources/{sourceIdentifier}
  • Loading branch information
ianwallen authored and github-actions[bot] committed Jan 29, 2025
1 parent 2bc6567 commit 6689472
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ public interface SourceRepository extends GeonetRepository<Source, String>, JpaS

public
@Nullable
List<Source> findByGroupOwner(@Nonnull int groupOwner);
List<Source> findByGroupOwner(@Nonnull int groupOwner, Sort sort);

public
@Nullable
List<Source> findByGroupOwnerAndType(@Nonnull int groupOwner, @Nonnull SourceType sourceType, Sort sort);
public
@Nullable
List<Source> findByGroupOwnerIn(Set<Integer> groupOwner);
Expand Down
43 changes: 36 additions & 7 deletions services/src/main/java/org/fao/geonet/api/sources/SourcesApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import jeeves.server.context.ServiceContext;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.fao.geonet.api.ApiError;
import org.fao.geonet.api.ApiParams;
import org.fao.geonet.api.ApiUtils;
import org.fao.geonet.api.exception.ResourceNotFoundException;
Expand Down Expand Up @@ -108,15 +109,23 @@ public ResponseEntity<?> getSources(
value = "group",
required = false)
Integer group,
@RequestParam(
value = "type",
required = false)
SourceType type,
@Parameter(hidden = true)
HttpServletResponse response,
@Parameter(hidden = true)
HttpServletRequest request
) throws Exception {
setHeaderVaryOnAccept(response);
List<Source> sources;
if (group != null) {
sources = sourceRepository.findByGroupOwner(group);
if (group != null && type != null) {
sources = sourceRepository.findByGroupOwnerAndType(group, type, SortUtils.createSort(Source_.name));
} else if (group != null) {
sources = sourceRepository.findByGroupOwner(group, SortUtils.createSort(Source_.name));
} else if (type != null) {
sources = sourceRepository.findByType(type, SortUtils.createSort(Source_.name));
} else {
sources = sourceRepository.findAll(SortUtils.createSort(Source_.name));
}
Expand Down Expand Up @@ -145,15 +154,35 @@ private String getSourcesAsHtml(List<Source> sources) throws Exception {
}

@io.swagger.v3.oas.annotations.Operation(
summary = "Get all sources by type",
description = "Sources are the local catalogue, subportal, external catalogue (when importing MEF files) or harvesters.")
summary = "Get a source",
description = "")
@RequestMapping(
value = "/{type}",
value = "/{sourceIdentifier}",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Source found."),
@ApiResponse(responseCode = "404", description = "Source not found.",
content = @Content(schema = @Schema(implementation = ApiError.class)))
})
@ResponseBody
public List<Source> getSourcesByType(@PathVariable SourceType type) throws Exception {
return sourceRepository.findByType(type, SortUtils.createSort(Source_.name));
public Source getSource(
@Parameter(
description = "Source identifier",
required = true
)
@PathVariable
String sourceIdentifier) throws Exception {
Optional<Source> existingSource = sourceRepository.findById(sourceIdentifier);
if (existingSource.isPresent()) {
return existingSource.get();
} else {
throw new ResourceNotFoundException(String.format(
"Source with uuid '%s' does not exist.",
sourceIdentifier
));
}
}

@io.swagger.v3.oas.annotations.Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,38 @@ public void setUp() throws Exception {
createTestData();
}

@Test
public void getSource() throws Exception {
Source source = sourceRepo.findOneByName("source-test");
Assert.assertNotNull(source);

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

this.mockHttpSession = loginAsAdmin();

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
this.mockMvc.perform(get("/srv/api/sources/" + source.getUuid())
.accept(MediaType.parseMediaType("application/json")))
.andExpect(status().isOk())
.andExpect(content().contentType(API_JSON_EXPECTED_ENCODING));
}

@Test
public void getNonExistingSource() throws Exception {
Source sourceToUpdate = sourceRepo.findOneByName("source-test-2");
Assert.assertNull(sourceToUpdate);

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

this.mockHttpSession = loginAsAdmin();

this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
this.mockMvc.perform(get("/srv/api/sources/source-test-2")
.accept(MediaType.parseMediaType("application/json")))
.andExpect(content().contentType(API_JSON_EXPECTED_ENCODING))
.andExpect(status().is(404));
}

@Test
public void getSources() throws Exception {
Long sourcesCount = sourceRepo.count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
gnGlobalSettings.gnCfg.mods.header.showPortalSwitcher;

function getPortals() {
var url = "../api/sources/subportal";
var url = "../api/sources?type=subportal";
$http.get(url).then(function (response) {
scope.portals = response.data.filter(function (p) {
return p.uuid != scope.nodeId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
usersearches = $http.get("../api/usersearches/featured?type=" + type);
var apiCalls = [usersearches];
if (withPortal) {
apiCalls.push($http.get("../api/sources/subportal"));
apiCalls.push($http.get("../api/sources?type=subportal"));
}
$q.all(apiCalls).then(function (alldata) {
var usersearches = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
$scope.cswTests = response.data;
});

$http.get("../api/sources/subportal").then(function (response) {
$http.get("../api/sources?type=subportal").then(function (response) {
$scope.cswVirtual = response.data;
});
}
Expand Down

0 comments on commit 6689472

Please sign in to comment.