Open prince-chrismc opened 4 years ago
Response builders use inheritance only to avoid code duplication. Because of that response_builder_t<T>
is not designed to be used in class hierarchies.
I think your case can be solved by using your own response-builder class like that:
class edit_response_builder {
restinio::response_builder_t<restinio::restinio_controlled_output_t> builder_;
...
public:
edit_response_builder(restinio::request_t & req) : builder_{req.create_response()} {
add_cors_headers(builder_);
add_api_headers(builder_);
add_edit_headers(builder_);
}
...
edit_response_builder & set_body(std::string body) {
builder_.set_body(std::move(body));
return *this;
}
auto done() { return builder_.done(); }
};
...
restinio::request_handling_status_t edit::operator()(const restinio::request_handle_t &req, restinio::router::route_params_t params) {
auto &user = list_.get(restinio::cast_to<int>(params["id"]));
user_management::user_modifier(user).apply(nlohmann::json::parse(req->body()));
return edit_response_builder{*req}
.set_body(user)
.done();
}
Typically templates are not bothered by inheritance. :thinking:
That's a very interesting solution, I'll give it a shot!
Would it be possible to to extend the template specialization in order to add custom methods and/or default behavior to the constructor?
There's a lot of boiler plate between
create_response()
anddone()
, for example:Now it would be very easy to have...
What I would love to have...
This begs the question
https://github.com/Stiffstream/restinio/blob/a6a07d147a04192cd0f590492ce6dd321597d54c/dev/restinio/message_builders.hpp#L257-L259