Skip to content

Commit

Permalink
Added: endpoint for listing featured items
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Dec 26, 2024
1 parent 9cc6117 commit 11a0592
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/Dataverse.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ public void setMetadataBlockFacets(List<DataverseMetadataBlockFacet> metadataBlo
this.metadataBlockFacets = metadataBlockFacets;
}

@OneToMany(mappedBy = "dataverse")
private List<DataverseFeaturedItem> dataverseFeaturedItems = new ArrayList<>();

public List<DataverseFeaturedItem> getDataverseFeaturedItems() {
return this.dataverseFeaturedItems;
}

public List<Guestbook> getParentGuestbooks() {
List<Guestbook> retList = new ArrayList<>();
Dataverse testDV = this;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Dataverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -1759,4 +1759,17 @@ public Response createFeaturedItem(@Context ContainerRequestContext crc,
return e.getResponse();
}
}

@GET
@AuthRequired
@Path("{identifier}/featuredItems")
public Response listFeaturedItems(@Context ContainerRequestContext crc, @PathParam("identifier") String dvIdtf) {
try {
Dataverse dataverse = findDataverseOrDie(dvIdtf);
List<DataverseFeaturedItem> featuredItems = execCommand(new ListDataverseFeaturedItemsCommand(createDataverseRequest(getRequestUser(crc)), dataverse));
return ok(jsonDataverseFeaturedItems(featuredItems));
} catch (WrappedResponse e) {
return e.getResponse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.DataverseFeaturedItem;
import edu.harvard.iq.dataverse.api.dto.NewDataverseFeaturedItemDTO;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
Expand All @@ -21,8 +22,7 @@
import java.nio.file.StandardCopyOption;
import java.util.List;

// TODO: Permissions
@RequiredPermissions({})
@RequiredPermissions({Permission.EditDataverse})
public class CreateDataverseFeaturedItemCommand extends AbstractCommand<DataverseFeaturedItem> {

private final Dataverse dataverse;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package edu.harvard.iq.dataverse.engine.command.impl;

import edu.harvard.iq.dataverse.Dataverse;
import edu.harvard.iq.dataverse.DataverseFeaturedItem;
import edu.harvard.iq.dataverse.authorization.Permission;
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
import edu.harvard.iq.dataverse.engine.command.CommandContext;
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* List the featured items {@link DataverseFeaturedItem} of a {@link Dataverse}.
*/
public class ListDataverseFeaturedItemsCommand extends AbstractCommand<List<DataverseFeaturedItem>> {

private final Dataverse dataverse;

public ListDataverseFeaturedItemsCommand(DataverseRequest request, Dataverse dataverse) {
super(request, dataverse);
this.dataverse = dataverse;
}

@Override
public List<DataverseFeaturedItem> execute(CommandContext ctxt) throws CommandException {
return dataverse.getDataverseFeaturedItems();
}

@Override
public Map<String, Set<Permission>> getRequiredPermissions() {
return Collections.singletonMap("",
dataverse.isReleased() ? Collections.emptySet()
: Collections.singleton(Permission.ViewUnpublishedDataverse));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,14 @@ private static JsonObjectBuilder jsonDataverseInputLevel(DataverseFieldTypeInput
return jsonObjectBuilder;
}

public static JsonArrayBuilder jsonDataverseFeaturedItems(List<DataverseFeaturedItem> dataverseFeaturedItems) {
JsonArrayBuilder featuredItemsArrayBuilder = Json.createArrayBuilder();
for (DataverseFeaturedItem dataverseFeaturedItem : dataverseFeaturedItems) {
featuredItemsArrayBuilder.add(json(dataverseFeaturedItem));
}
return featuredItemsArrayBuilder;
}

public static JsonObjectBuilder json(DataverseFeaturedItem dataverseFeaturedItem) {
return jsonObjectBuilder()
.add("id", dataverseFeaturedItem.getId())
Expand Down
8 changes: 5 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/DataversesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ public void testGetUserPermissionsOnDataverse() {
}

@Test
public void testUpdateFeaturedItems() {
public void testCreateFeaturedItem() {
Response createUserResponse = UtilIT.createRandomUser();
String apiToken = UtilIT.getApiTokenFromResponse(createUserResponse);
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken);
Expand All @@ -1577,13 +1577,15 @@ public void testUpdateFeaturedItems() {
.body("data.displayOrder", equalTo(0))
.statusCode(OK.getStatusCode());

// TODO
Response listFeaturedItemsResponse = UtilIT.listDataverseFeaturedItems(dataverseAlias, apiToken);
listFeaturedItemsResponse.prettyPrint();

// Should return error when passing incorrect file type
pathToTestFile = "src/test/resources/tab/test.tab";
updateFeatureItemsResponse = UtilIT.createFeaturedItem(dataverseAlias, apiToken, "test", "test", pathToTestFile);
updateFeatureItemsResponse.prettyPrint();
updateFeatureItemsResponse.then().assertThat()
.body("message", equalTo(BundleUtil.getStringFromBundle("dataverse.create.featuredItem.error.invalidFileType")))
// FIXME
.statusCode(BAD_REQUEST.getStatusCode());
}
}
7 changes: 7 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4315,4 +4315,11 @@ static Response createFeaturedItem(String dataverseAlias, String apiToken, Strin
.when()
.post("/api/dataverses/" + dataverseAlias + "/featuredItem");
}

static Response listDataverseFeaturedItems(String dataverseAlias, String apiToken) {
return given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.contentType("application/json")
.get("/api/dataverses/" + dataverseAlias + "/featuredItems");
}
}

0 comments on commit 11a0592

Please sign in to comment.