1#include <userver/utest/using_namespace_userver.hpp>
3#include <userver/easy.hpp>
4#include "schemas/key_value.hpp"
6constexpr std::string_view kSchema = R"~(
7CREATE TABLE IF NOT EXISTS key_value_table (
8 key integer PRIMARY KEY,
9 value VARCHAR
10)
11)~";
13int main(
int argc,
char* argv[]) {
14 easy::HttpWith<easy::PgDep>(argc, argv)
18 [](formats::json::Value request_json,
const easy::PgDep& dep) {
20 auto key = request_json.As<schemas::KeyRequest>().key;
22 auto res = dep.pg().Execute(
23 storages::postgres::ClusterHostType::kSlave,
"SELECT value FROM key_value_table WHERE key=$1", key
26 schemas::KeyValue response{key, res[0][0].As<std::string>()};
27 return formats::json::ValueBuilder{response}.ExtractValue();
30 .Post(
"/kv", [](formats::json::Value request_json, easy::PgDep dep) {
32 auto key_value = request_json.As<schemas::KeyValue>();
35 storages::postgres::ClusterHostType::kMaster,
36 "INSERT INTO key_value_table(key, value) VALUES($1, $2) ON CONFLICT (key) DO UPDATE SET value = $2",
41 return formats::json::Value{};