Search in sources :

Example 81 with JsonArray

use of io.vertx.core.json.JsonArray in project okapi by folio-org.

the class PostgresTable method insert.

public void insert(T dd, Handler<ExtendedAsyncResult<Void>> fut) {
    PostgresQuery q = pg.getQuery();
    final String sql = "INSERT INTO " + table + "(" + jsonColumn + ") VALUES (?::JSONB)";
    String s = Json.encode(dd);
    JsonObject doc = new JsonObject(s);
    JsonArray jsa = new JsonArray();
    jsa.add(doc.encode());
    q.queryWithParams(sql, jsa, res -> {
        if (res.failed()) {
            fut.handle(new Failure<>(res.getType(), res.cause()));
        } else {
            q.close();
            fut.handle(new Success<>());
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Example 82 with JsonArray

use of io.vertx.core.json.JsonArray in project okapi by folio-org.

the class PostgresTable method update.

public void update(T md, Handler<ExtendedAsyncResult<Void>> fut) {
    PostgresQuery q = pg.getQuery();
    String sql = "INSERT INTO " + table + "(" + jsonColumn + ") VALUES (?::JSONB)" + " ON CONFLICT ((" + idIndex + ")) DO UPDATE SET " + jsonColumn + "= ?::JSONB";
    String s = Json.encode(md);
    JsonObject doc = new JsonObject(s);
    JsonArray jsa = new JsonArray();
    jsa.add(doc.encode());
    jsa.add(doc.encode());
    q.updateWithParams(sql, jsa, res -> {
        if (res.failed()) {
            fut.handle(new Failure<>(INTERNAL, res.cause()));
        } else {
            q.close();
            fut.handle(new Success<>());
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Example 83 with JsonArray

use of io.vertx.core.json.JsonArray in project okapi by folio-org.

the class PostgresTable method delete.

public void delete(String id, Handler<ExtendedAsyncResult<Void>> fut) {
    PostgresQuery q = pg.getQuery();
    String sql = "DELETE FROM " + table + " WHERE " + idSelect;
    JsonArray jsa = new JsonArray();
    jsa.add(id);
    q.updateWithParams(sql, jsa, res -> {
        if (res.failed()) {
            fut.handle(new Failure<>(INTERNAL, res.cause()));
        } else {
            UpdateResult result = res.result();
            if (result.getUpdated() > 0) {
                fut.handle(new Success<>());
            } else {
                fut.handle(new Failure<>(NOT_FOUND, id));
            }
            q.close();
        }
    });
}
Also used : JsonArray(io.vertx.core.json.JsonArray) UpdateResult(io.vertx.ext.sql.UpdateResult)

Example 84 with JsonArray

use of io.vertx.core.json.JsonArray in project okapi by folio-org.

the class TenantStorePostgres method updateModuleR.

private void updateModuleR(PostgresQuery q, String id, SortedMap<String, Boolean> enabled, Iterator<JsonObject> it, Handler<ExtendedAsyncResult<Void>> fut) {
    if (it.hasNext()) {
        JsonObject r = it.next();
        String sql = "UPDATE " + TABLE + " SET " + JSON_COLUMN + " = ? WHERE " + ID_SELECT;
        String tj = r.getString(JSON_COLUMN);
        Tenant t = Json.decodeValue(tj, Tenant.class);
        t.setEnabled(enabled);
        String s = Json.encode(t);
        JsonObject doc = new JsonObject(s);
        JsonArray jsa = new JsonArray();
        jsa.add(doc.encode());
        jsa.add(id);
        q.queryWithParams(sql, jsa, res -> {
            if (res.failed()) {
                fut.handle(new Failure<>(INTERNAL, res.cause()));
            } else {
                updateModuleR(q, id, enabled, it, fut);
            }
        });
    } else {
        fut.handle(new Success<>());
        q.close();
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) Tenant(org.folio.okapi.bean.Tenant) JsonObject(io.vertx.core.json.JsonObject)

Example 85 with JsonArray

use of io.vertx.core.json.JsonArray in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testCookieFormNoexplodeArray.

/**
 * Test: cookie_form_noexplode_array
 * Expected parameters sent:
 * color: color=blue,black,brown
 * Expected response: {"color":["blue","black","brown"]}
 * @throws Exception
 */
@Test
public void testCookieFormNoexplodeArray() throws Exception {
    routerFactory.addHandlerByOperationId("cookie_form_noexplode_array", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_cookie = params.cookieParameter("color");
        assertNotNull(color_cookie);
        assertTrue(color_cookie.isArray());
        res.put("color", new JsonArray(color_cookie.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    List<Object> color_cookie;
    color_cookie = new ArrayList<>();
    color_cookie.add("blue");
    color_cookie.add("black");
    color_cookie.add("brown");
    startServer();
    apiClient.cookieFormNoexplodeArray(color_cookie, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpResponse(io.vertx.ext.web.client.HttpResponse) HashMap(java.util.HashMap) RoutingContext(io.vertx.ext.web.RoutingContext) OpenAPI3RouterFactoryImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl) ArrayList(java.util.ArrayList) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) RequestParameters(io.vertx.ext.web.api.RequestParameters) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Files(java.nio.file.Files) RequestParameter(io.vertx.ext.web.api.RequestParameter) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) WebTestValidationBase(io.vertx.ext.web.api.validation.WebTestValidationBase) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) ExternalResource(org.junit.rules.ExternalResource) Paths(java.nio.file.Paths) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Aggregations

JsonArray (io.vertx.core.json.JsonArray)535 JsonObject (io.vertx.core.json.JsonObject)379 Test (org.junit.Test)185 List (java.util.List)69 ArrayList (java.util.ArrayList)66 Map (java.util.Map)52 Handler (io.vertx.core.Handler)49 HashMap (java.util.HashMap)42 Collectors (java.util.stream.Collectors)42 Test (org.junit.jupiter.api.Test)41 Future (io.vertx.core.Future)37 IOException (java.io.IOException)35 AsyncResult (io.vertx.core.AsyncResult)34 Buffer (io.vertx.core.buffer.Buffer)33 HttpURLConnection (java.net.HttpURLConnection)30 StandardCharsets (java.nio.charset.StandardCharsets)29 RoutingContext (io.vertx.ext.web.RoutingContext)26 Objects (java.util.Objects)25 Instant (java.time.Instant)24 Rule (org.junit.Rule)22