Skip to content

Commit

Permalink
Replace all-in-one with paginated endpoint (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi authored Oct 3, 2024
1 parent 507ce46 commit 54a9c2a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.iml
.idea/
target/
logback/
.DS_Store
application.properties
29 changes: 18 additions & 11 deletions src/main/java/org/qubitpi/wilhelm/web/endpoints/DataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.qubitpi.wilhelm.web.endpoints;

import org.qubitpi.wilhelm.config.ApplicationConfig;

import org.aeonbits.owner.ConfigFactory;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
Expand All @@ -25,6 +23,7 @@
import org.neo4j.driver.QueryConfig;
import org.neo4j.driver.Value;
import org.neo4j.driver.internal.types.InternalTypeSystem;
import org.qubitpi.wilhelm.config.ApplicationConfig;

import jakarta.inject.Inject;
import jakarta.inject.Singleton;
Expand All @@ -33,6 +32,7 @@
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import net.jcip.annotations.Immutable;
Expand Down Expand Up @@ -62,10 +62,10 @@ public class DataServlet {
private static final String NEO4J_DATABASE = APPLICATION_CONFIG.neo4jDatabase();

private static final Map<String, String> LANGUAGES = Stream.of(
new AbstractMap.SimpleImmutableEntry<>("german", "German"),
new AbstractMap.SimpleImmutableEntry<>("ancientGreek", "Ancient Greek"),
new AbstractMap.SimpleImmutableEntry<>("latin", "Latin")
)
new AbstractMap.SimpleImmutableEntry<>("german", "German"),
new AbstractMap.SimpleImmutableEntry<>("ancientGreek", "Ancient Greek"),
new AbstractMap.SimpleImmutableEntry<>("latin", "Latin")
)
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));


Expand All @@ -91,17 +91,23 @@ public Response healthcheck() {
}

/**
* Get all vocabularies of a language.
* Get paginated vocabularies of a language.
*
* @param language The language. Must be one of "german", "ancientGreek", or "latin". Otherwise a 404 response is
* returned
* @param perPage Requested number of words to be displayed on each page of results
* @param page Requested page of results desired
*
* @return the Neo4J query results in JSON format
* @return the paginated Neo4J query results in JSON format
*/
@GET
@Path("/languages/{language}")
@Produces(MediaType.APPLICATION_JSON)
public Response getVocabularyByLanguage(@NotNull @PathParam("language") final String language) {
public Response getVocabularyByLanguagePaged(
@NotNull @PathParam("language") final String language,
@NotNull @QueryParam("perPage") final String perPage,
@NotNull @QueryParam("page") final String page
) {
if (!LANGUAGES.containsKey(Objects.requireNonNull(language, "language"))) {
return Response
.status(Response.Status.BAD_REQUEST)
Expand All @@ -116,8 +122,9 @@ public Response getVocabularyByLanguage(@NotNull @PathParam("language") final St

final String query = String.format(
"MATCH (t:Term WHERE t.language = '%s')-[r]->(d:Definition) " +
"RETURN t.name AS term, d.name AS definition",
LANGUAGES.get(language)
"RETURN t.name AS term, d.name AS definition " +
"SKIP %s LIMIT %s",
LANGUAGES.get(language), (Integer.parseInt(page) - 1) * Integer.parseInt(perPage), perPage
);

try (Driver driver = GraphDatabase.driver(NEO4J_URL, AuthTokens.basic(NEO4J_USERNAME, NEO4J_PASSWORD))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ class DataServletITSpec extends Specification {
.statusCode(200)
}

def "Get vocabulary by language"() {
def "Get vocabulary by language and retrieve the very first word"() {
expect:
RestAssured
.given()
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.queryParams([perPage: "10", page: "1"])
.when()
.get("/data/languages/german")
.then()
Expand Down

0 comments on commit 54a9c2a

Please sign in to comment.