首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Uni中的变量

如何使用Uni中的变量
EN

Stack Overflow用户
提问于 2022-03-14 14:36:56
回答 1查看 598关注 0票数 1

我用Quarkus和Mutiny创建了一个反应性的resteasy服务。在POST方法中,我在PostgreSQL表中插入对象并得到一个Uni< id >作为回报。我需要将这个id设置为包含f.ex的响应类的一部分。" id“、"errorCode”、"errorString“和其他变量,但由于id作为Uni对象出现,我不知道如何从其中提取id。

创建方法:

代码语言:javascript
复制
  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值的RetailPlaceResponse:

代码语言:javascript
复制
public RetailPlaceResponse(String errorCode, long id, boolean isSuccessful) {
    this.errorCode = errorCode;
    this.id = id;
    this.isSuccessful = isSuccessful;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-16 15:24:13

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

代码语言:javascript
复制
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));
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71469490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档