如何使用 Uni、Mutiny 中的变量

How to use a variable from Uni, Mutiny

我使用 Quarkus 和 Mutiny 创建了一个响应式 resteasy 服务。在 POST 方法中,我在我的 PostgreSQL table 中插入对象并在 return 中获取一个 Uni< id >。我需要将此 ID 设置为包含 f.ex 的响应 class 的一部分。 “id”、“errorCode”、“errorString”和其他变量,但我很挣扎,因为 id 作为 Uni 对象出现,我不知道如何从中提取 id。

创建方法:

  public static Uni<Long> create(PgPool client, RetailPlace retailPlace) {
        return client.withTransaction(conn -> conn
                .preparedQuery("INSERT INTO retail_place (title) VALUES () RETURNING id")
                .execute(Tuple.of(retailPlace.getRetailPlaceTitle()))
                .onItem().transformToUni(id -> conn.
                        preparedQuery("INSERT INTO retail_place_address (retail_place_id,region_code,city,locality_id,apartment,house,region,street) " +
                                "VALUES (,,,,,,,) returning retail_place_id")
                        .execute(Tuple.tuple(Arrays.asList(id.iterator().next().getLong("id"), retailPlace.getRetailPlaceAddress().getRegionCode(),
                                retailPlace.getRetailPlaceAddress().getCity(), retailPlace.getRetailPlaceAddress().getLocalityId(),
                                retailPlace.getRetailPlaceAddress().getApartment(), retailPlace.getRetailPlaceAddress().getHouse(),
                                retailPlace.getRetailPlaceAddress().getRegion(), retailPlace.getRetailPlaceAddress().getStreet())))))
                .onItem().transform(pgRowSet -> pgRowSet.iterator().next().getLong("retail_place_id"));
    }

我在 return 中得到了 id 的 long 值。现在我需要 return 一个带有 id 值的 RetailPlaceResponse:

public RetailPlaceResponse(String errorCode, long id, boolean isSuccessful) {
    this.errorCode = errorCode;
    this.id = id;
    this.isSuccessful = isSuccessful;
}

如果您需要成功和 non-successful 响应映射到相同的 class,您可以编写单独的转换。一个成功,一个失败。

public static Uni<RetailPlaceResponse> create(PgPool client, RetailPlace retailPlace) {
    return client.withTransaction(conn -> conn
            .preparedQuery("INSERT INTO retail_place (title) VALUES () RETURNING id")
            .execute(Tuple.of(retailPlace.getRetailPlaceTitle()))
            .onItem().transformToUni(id -> conn.
                    preparedQuery("INSERT INTO retail_place_address (retail_place_id,region_code,city,locality_id,apartment,house,region,street) " +
                            "VALUES (,,,,,,,) returning retail_place_id")
                    .execute(Tuple.tuple(Arrays.asList(id.iterator().next().getLong("id"), retailPlace.getRetailPlaceAddress().getRegionCode(),
                            retailPlace.getRetailPlaceAddress().getCity(), retailPlace.getRetailPlaceAddress().getLocalityId(),
                            retailPlace.getRetailPlaceAddress().getApartment(), retailPlace.getRetailPlaceAddress().getHouse(),
                            retailPlace.getRetailPlaceAddress().getRegion(), retailPlace.getRetailPlaceAddress().getStreet())))))
            .onItem().transform(pgRowSet -> new RetailPlaceResponse(null, pgRowSet.iterator().next().getLong("retail_place_id"), true))
            .onFailure().transform(error -> new RetailPlaceResponse(error.toString(), 0, false));
}