Skip to content

Commit

Permalink
support id or name for GET of dataset type #10517
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Aug 13, 2024
1 parent f8e8c4f commit 867f548
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
10 changes: 5 additions & 5 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3047,23 +3047,23 @@ The fully expanded example above (without environment variables) looks like this
.. _api-list-dataset-type:
Get Dataset Type by Name
^^^^^^^^^^^^^^^^^^^^^^^^
Get Dataset Type
^^^^^^^^^^^^^^^^
Show a single dataset type based on its name.
Show a dataset type by passing either its database id (e.g. "2") or its name (e.g. "software").
.. code-block:: bash
export SERVER_URL=https://demo.dataverse.org
export TYPE=software
curl $SERVER_URL/api/datasets/datasetTypes/byName/$TYPE"
curl $SERVER_URL/api/datasets/datasetTypes/$TYPE"
The fully expanded example above (without environment variables) looks like this:
.. code-block:: bash
curl "https://demo.dataverse.org/api/datasets/datasetTypes/byName/software"
curl "https://demo.dataverse.org/api/datasets/datasetTypes/software"
.. _api-add-dataset-type:
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -5092,13 +5092,23 @@ public Response getDatasetTypes() {
}

@GET
@Path("datasetTypes/byName/{name}")
public Response getDatasetTypes(@PathParam("name") String name) {
DatasetType datasetType = datasetTypeSvc.getByName(name);
@Path("datasetTypes/{idOrName}")
public Response getDatasetTypes(@PathParam("idOrName") String idOrName) {
DatasetType datasetType = null;
if (StringUtils.isNumeric(idOrName)) {
try {
long id = Long.parseLong(idOrName);
datasetType = datasetTypeSvc.getById(id);
} catch (NumberFormatException ex) {
return error(NOT_FOUND, "Could not find a dataset type with id " + idOrName);
}
} else {
datasetType = datasetTypeSvc.getByName(idOrName);
}
if (datasetType != null) {
return ok(datasetType.toJson());
} else {
return error(NOT_FOUND, "Could not find a dataset type with name " + name);
return error(NOT_FOUND, "Could not find a dataset type with name " + idOrName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
@NamedQueries({
@NamedQuery(name = "DatasetType.findAll",
query = "SELECT d FROM DatasetType d"),
@NamedQuery(name = "DatasetType.findById",
query = "SELECT d FROM DatasetType d WHERE d.id=:id"),
@NamedQuery(name = "DatasetType.findByName",
query = "SELECT d FROM DatasetType d WHERE d.name=:name"),
@NamedQuery(name = "DatasetType.deleteById",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public List<DatasetType> listAll() {
return em.createNamedQuery("DatasetType.findAll", DatasetType.class).getResultList();
}

public DatasetType getById(long id) {
try {
return em.createNamedQuery("DatasetType.findById", DatasetType.class)
.setParameter("id", id)
.getSingleResult();
} catch (NoResultException noResultException) {
logger.log(Level.WARNING, "Couldn't find a dataset type with id " + id);
return null;
}
}

public DatasetType getByName(String name) {
try {
return em.createNamedQuery("DatasetType.findByName", DatasetType.class)
Expand Down
12 changes: 9 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetTypesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DatasetTypesIT {
public static void setUpClass() {
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri();

Response getSoftwareType = UtilIT.getDatasetTypeByName(DatasetType.DATASET_TYPE_SOFTWARE);
Response getSoftwareType = UtilIT.getDatasetType(DatasetType.DATASET_TYPE_SOFTWARE);
getSoftwareType.prettyPrint();

String typeFound = JsonPath.from(getSoftwareType.getBody().asString()).getString("data.name");
Expand Down Expand Up @@ -175,7 +175,7 @@ public void testGetDatasetTypes() {

@Test
public void testGetDefaultDatasetType() {
Response getType = UtilIT.getDatasetTypeByName(DatasetType.DEFAULT_DATASET_TYPE);
Response getType = UtilIT.getDatasetType(DatasetType.DEFAULT_DATASET_TYPE);
getType.prettyPrint();
getType.then().assertThat()
.statusCode(OK.getStatusCode())
Expand Down Expand Up @@ -203,7 +203,13 @@ public void testAddAndDeleteDatasetType() {

typeAdded.then().assertThat().statusCode(OK.getStatusCode());

long doomed = JsonPath.from(typeAdded.getBody().asString()).getLong("data.id");
Long doomed = JsonPath.from(typeAdded.getBody().asString()).getLong("data.id");

System.out.println("doomed: " + doomed);
Response getTypeById = UtilIT.getDatasetType(doomed.toString());
getTypeById.prettyPrint();
getTypeById.then().assertThat().statusCode(OK.getStatusCode());

System.out.println("deleting type with id " + doomed);
Response typeDeleted = UtilIT.deleteDatasetTypes(doomed, apiToken);
typeDeleted.prettyPrint();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4089,9 +4089,9 @@ public static Response getDatasetTypes() {
return response;
}

static Response getDatasetTypeByName(String name) {
static Response getDatasetType(String idOrName) {
return given()
.get("/api/datasets/datasetTypes/byName/" + name);
.get("/api/datasets/datasetTypes/" + idOrName);
}

static Response addDatasetType(String jsonIn, String apiToken) {
Expand Down

0 comments on commit 867f548

Please sign in to comment.