forked from segoon/upastebin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatest.cpp
More file actions
46 lines (38 loc) · 1.69 KB
/
latest.cpp
File metadata and controls
46 lines (38 loc) · 1.69 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
#include "latest.hpp"
#include <userver/components/component_context.hpp>
#include <userver/formats/json/inline.hpp>
#include <userver/formats/json/value_builder.hpp>
#include <userver/storages/postgres/cluster.hpp>
#include <userver/storages/postgres/component.hpp>
#include <userver/utils/boost_uuid4.hpp>
namespace upastebin {
LatestHandler::LatestHandler(
const userver::components::ComponentConfig& config,
const userver::components::ComponentContext& context)
: HttpHandlerBase(config, context),
pg_(context.FindComponent<userver::components::Postgres>("postgres")
.GetCluster()) {}
std::string LatestHandler::HandleRequestThrow(
const userver::server::http::HttpRequest&,
userver::server::request::RequestContext&) const {
auto result = pg_->Execute(
// userver::storages::postgres::ClusterHostType::kSlave,
userver::storages::postgres::ClusterHostType::kMaster,
"SELECT uuid, author, substring(text for $1::INTEGER) AS text_tr, ip_source "
"FROM "
"upastebin.texts ORDER BY created_at DESC LIMIT $2::INTEGER;",
1000, 10);
userver::formats::json::ValueBuilder response(
userver::formats::common::Type::kArray);
for (const auto& item : result) {
userver::formats::json::ValueBuilder response_item;
response_item["id"] = item["uuid"].As<std::string>();
response_item["author"] = item["author"].As<std::string>();
response_item["ip"] = item["ip_source"].As<std::string>();
response_item["text"] = item["text_tr"].As<std::string>();
response.PushBack(response_item.ExtractValue());
}
return ToString(
userver::formats::json::MakeObject("items", response.ExtractValue()));
}
} // namespace upastebin