forked from userver-framework/userver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.cpp
More file actions
62 lines (49 loc) · 1.73 KB
/
view.cpp
File metadata and controls
62 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "view.hpp"
#include <userver/engine/sleep.hpp>
#include <userver/formats/json.hpp>
#include <userver/logging/log.hpp>
#include <userver/utest/using_namespace_userver.hpp>
#include <userver/utils/datetime.hpp>
#include <userver/ydb/table.hpp>
namespace {
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::Name{"upsert-row"},
};
} // namespace
namespace sample {
formats::json::Value
UpsertRowHandler::HandleRequestJsonThrow(const server::http::HttpRequest&, const formats::json::Value& request, server::request::RequestContext&)
const {
engine::SleepFor(std::chrono::milliseconds(10));
Ydb().RetryTx("trx", {.tx_mode = ydb::TransactionMode::kSerializableRW}, [&](ydb::TxActor& tx) {
auto response = tx.Execute(
kUpsertQuery, //
"$id_key",
request["id"].As<std::string>(), //
"$name_key",
ydb::Utf8{request["name"].As<std::string>()}, //
"$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()) {
throw std::runtime_error("Unexpected response data");
}
return ydb::TxAction::kCommit;
});
return formats::json::MakeObject();
}
} // namespace sample