如何使用 Uni、Mutiny 中的变量

发布于 2025-01-13 23:57:18 字数 1764 浏览 2 评论 0原文

我使用 Quarkus 和 Mutiny 创建了一个反应式 Resteasy 服务。在 POST 方法中,我在 PostgreSQL 表中插入对象并得到 Uni< id>回报。我需要将此 id 设置为包含 f.ex 的 Response 类的一部分。 “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 ($1) 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 ($1,$2,$3,$4,$5,$6,$7,$8) 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"));
    }

我得到 id 的长值作为回报。现在我需要返回一个包含 id 值的 RetailPlaceResponse:

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

I created a reactive resteasy service with Quarkus and Mutiny. In POST method I insert object in my PostgreSQL table and get an Uni< id > in return. I need to set this id as part of my Response class which contains f.ex. "id", "errorCode", "errorString" and other variables, but I struggle as id comes as Uni object and I don't know how to extract id from it.

Create method:

  public static Uni<Long> create(PgPool client, RetailPlace retailPlace) {
        return client.withTransaction(conn -> conn
                .preparedQuery("INSERT INTO retail_place (title) VALUES ($1) 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 ($1,$2,$3,$4,$5,$6,$7,$8) 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"));
    }

I get long value of id in return. Now I need to return a RetailPlaceResponse with id value in it:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2025-01-20 23:57:18

如果您需要成功和不成功的响应来映射到同一个类,您可以编写单独的转换。一种是成功,一种是失败。

public static Uni<RetailPlaceResponse> create(PgPool client, RetailPlace retailPlace) {
    return client.withTransaction(conn -> conn
            .preparedQuery("INSERT INTO retail_place (title) VALUES ($1) 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 ($1,$2,$3,$4,$5,$6,$7,$8) 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));
}

If you need both successful and non-successful responses to map to the same class, you could write separate transforms. One for success and one for failure.

public static Uni<RetailPlaceResponse> create(PgPool client, RetailPlace retailPlace) {
    return client.withTransaction(conn -> conn
            .preparedQuery("INSERT INTO retail_place (title) VALUES ($1) 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 ($1,$2,$3,$4,$5,$6,$7,$8) 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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文