Skip to content

Commit

Permalink
Add server example that use id as an attribute (not part of the route)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfleury2 committed Jun 29, 2022
1 parent c0a7b4b commit 79b9f18
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions examples/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ int main()
server.add_route("/person")
// Get the list of available ids
.get([](const auto& req, auto& res) {
for (const auto& [id, _] : storage) {
res.body() += id + "\n";

auto id = req.a("id").as_string();
if (id.empty()) {
for (const auto& [id, _] : storage) {
res.body() += id + "\n";
}
}
else {
std::string response = "Id: " + id + " not found";
if (auto found = storage.find(id); found != end(storage)) {
res.body() = "id: " + id + ", name: " + found->second + "\n";
}
else {
res.body() = "Id: " + id + " NOT FOUND";
}
}
})
// Add a id/name
Expand Down

0 comments on commit 79b9f18

Please sign in to comment.