userver: /data/code/userver/libraries/easy/samples/3_json/main.cpp Source File
Loading...
Searching...
No Matches
main.cpp
1#include <userver/utest/using_namespace_userver.hpp> // Note: this is for the purposes of samples only
2
3#include <userver/easy.hpp>
4#include "schemas/key_value.hpp"
5
6constexpr std::string_view kSchema = R"~(
7CREATE TABLE IF NOT EXISTS key_value_table (
8 key integer PRIMARY KEY,
9 value VARCHAR
10)
11)~";
12
13int main(int argc, char* argv[]) {
14 easy::HttpWith<easy::PgDep>(argc, argv)
15 .DbSchema(kSchema)
16 .Get(
17 "/kv",
18 [](schemas::KeyRequest&& request, const easy::PgDep& dep) {
19 auto res = dep.pg().Execute(
21 "SELECT value FROM key_value_table WHERE key=$1",
22 request.key
23 );
24
25 return schemas::KeyValue{std::move(request.key), res[0][0].As<std::string>()};
26 }
27 )
28 .Post("/kv", [](schemas::KeyValue key_value, easy::PgDep dep) {
29 dep.pg().Execute(
31 "INSERT INTO key_value_table(key, value) VALUES($1, $2) ON CONFLICT (key) DO UPDATE SET value = $2",
32 key_value.key,
33 key_value.value
34 );
35
36 return formats::json::Value{};
37 });
38}