forked from segoon/upastebin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.cpp
More file actions
41 lines (34 loc) · 1.72 KB
/
store.cpp
File metadata and controls
41 lines (34 loc) · 1.72 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
#include "store.hpp"
#include <userver/components/component_context.hpp>
#include <userver/formats/json/inline.hpp>
#include <userver/storages/postgres/io/chrono.hpp>
#include <userver/http/common_headers.hpp>
#include <userver/server/handlers/exceptions.hpp>
#include <userver/server/handlers/http_handler_base.hpp>
#include <userver/storages/postgres/cluster.hpp>
#include <userver/storages/postgres/component.hpp>
#include <userver/utils/boost_uuid4.hpp>
#include <userver/utils/datetime.hpp>
namespace upastebin {
StoreHandler::StoreHandler(const userver::components::ComponentConfig& config,
const userver::components::ComponentContext& context)
: HttpHandlerBase(config, context),
pg_(context.FindComponent<userver::components::Postgres>("postgres")
.GetCluster()) {}
std::string StoreHandler::HandleRequestThrow(
const userver::server::http::HttpRequest& request,
userver::server::request::RequestContext&) const {
auto uuid = userver::utils::generators::GenerateBoostUuid();
auto author = request.GetArg("author");
auto ip_source = request.GetRemoteAddress().PrimaryAddressString();
auto text = request.RequestBody();
userver::storages::postgres::TimePointTz created_at{userver::utils::datetime::Now()};
pg_->Execute(userver::storages::postgres::ClusterHostType::kMaster,
"INSERT INTO upastebin.texts (uuid, author, ip_source, text, created_at) VALUES "
"($1, $2, $3, $4, $5);",
uuid, author, ip_source, text, created_at);
auto json_response = userver::formats::json::MakeObject("uuid",
userver::utils::ToString(uuid));
return ToString(json_response);
}
} // namespace upastebin