namespace samples::pg {
public:
static constexpr std::string_view kName = "handler-key-value";
std::string HandleRequestThrow(
private:
std::string GetValue(std::string_view key,
std::string PostValue(std::string_view key,
std::string DeleteValue(std::string_view key) const;
};
}
namespace samples::pg {
: HttpHandlerBase(config, context),
pg_cluster_(
context.FindComponent<
components::Postgres>(
"key-value-database")
.GetCluster()) {
constexpr auto kCreateTable = R"~(
CREATE TABLE IF NOT EXISTS key_value_table (
key VARCHAR PRIMARY KEY,
value VARCHAR
)
)~";
pg_cluster_->Execute(ClusterHostType::kMaster, kCreateTable);
}
std::string KeyValue::HandleRequestThrow(
const auto& key = request.
GetArg(
"key");
if (key.empty()) {
}
switch (request.
GetMethod()) {
case server::http::HttpMethod::kGet:
return GetValue(key, request);
case server::http::HttpMethod::kPost:
return PostValue(key, request);
case server::http::HttpMethod::kDelete:
return DeleteValue(key);
default:
fmt::format("Unsupported method {}", request.GetMethod())});
}
}
"SELECT value FROM key_value_table WHERE key=$1",
};
std::string KeyValue::GetValue(std::string_view key,
return {};
}
}
"INSERT INTO key_value_table (key, value) "
"VALUES ($1, $2) "
"ON CONFLICT DO NOTHING",
};
std::string KeyValue::PostValue(
const auto& value = request.
GetArg(
"value");
pg_cluster_->Begin("sample_transaction_insert_key_value",
auto res = transaction.
Execute(kInsertValue, key, value);
if (res.
RowsAffected()) {
return std::string{value};
}
res = transaction.Execute(kSelectValue, key);
if (result != value) {
}
}
std::string KeyValue::DeleteValue(std::string_view key) const {
auto res =
"DELETE FROM key_value_table WHERE key=$1", key);
return std::to_string(res.RowsAffected());
}
}
int main(int argc, char* argv[]) {
const auto component_list =
.
Append<samples::pg::KeyValue>()
.Append<components::Postgres>("key-value-database")
.Append<clients::dns::Component>();
}