userver: samples/ydb_service/views/upsert-2rows/post/view.cpp
Loading...
Searching...
No Matches
samples/ydb_service/views/upsert-2rows/post/view.cpp
#include "view.hpp"
namespace sample {
formats::json::
Value
Upsert2RowsHandler::HandleRequestJsonThrow(const server::http::HttpRequest& http_request, const formats::json::Value& request, server::request::RequestContext&) const {
http_request.GetHttpResponse().SetContentType(http::content_type::kApplicationJson);
static const ydb::Query kUpsertQuery{
R"(
--!syntax_v1
DECLARE $id_key AS String;
DECLARE $name_key AS Utf8;
DECLARE $service_key AS String;
DECLARE $channel_key AS Int64;
DECLARE $state_key AS Json?;
UPSERT INTO events (id, name, service, channel, created, state)
VALUES ($id_key, $name_key, $service_key, $channel_key, CurrentUtcTimestamp(), $state_key);
)",
ydb::Query::NameLiteral{"upsert-2rows"},
};
Ydb().RetryTx("trx", {.tx_mode = ydb::TransactionMode::kSerializableRW}, [&](ydb::TxActor& tx) {
for (auto i : {1, 2}) {
auto response = tx.Execute(
kUpsertQuery, //
"$id_key",
request["id"].As<std::string>() + std::to_string(i), //
"$name_key",
ydb::Utf8{request["name"].As<std::string>() + std::to_string(i)}, //
"$service_key",
request["service"].As<std::string>(), //
"$channel_key",
request["channel"].As<int64_t>(), //
"$state_key",
request["state"].As<std::optional<formats::json::Value>>() //
);
if (response.GetCursorCount() != 0) {
throw std::runtime_error("Unexpected response data");
}
}
return ydb::TxAction::kCommit;
});
}
} // namespace sample