Services
Services sit between controllers and models. They hold business logic that doesn't belong in either — validation, multi-table operations, calls to external APIs.
BaseService
The base class mirrors BaseContoller but with all methods virtual:
class BaseService : public Helper
{
public:
virtual json index();
virtual json create(json model);
json create(const crow::request &req); // convenience: parse body for you
virtual json read(string id);
virtual json update(json model);
virtual void erease();
virtual json getById(string id);
};
Using a service in a controller
Instantiate the service inside the controller method, or inject it through the constructor:
json PostController::index()
{
PostService svc;
auto posts = svc.index();
// post-processing: hide private fields, attach pagination metadata, etc.
json out;
out["data"] = posts;
out["count"] = posts.size();
return out;
}
The service layer also makes testing easier — controllers become thin shims and services can be exercised without spinning up the HTTP server.