34void add_cors_headers(crow::response &res,
const rest_server_context &ctx) {
60std::string patients_to_json(
const std::vector<storage::patient_record> &patients,
145 std::shared_ptr<rest_server_context> ctx) {
147 CROW_ROUTE(app,
"/api/v1/patients")
148 .methods(crow::HTTPMethod::GET)([ctx](
const crow::request &req) {
150 res.add_header(
"Content-Type",
"application/json");
151 add_cors_headers(res, *ctx);
153 if (!ctx->database) {
161 auto [limit, offset] = parse_pagination(req);
166 query.offset = offset;
168 auto patient_id = req.url_params.get(
"patient_id");
170 query.patient_id = patient_id;
173 auto patient_name = req.url_params.get(
"patient_name");
175 query.patient_name = patient_name;
178 auto birth_date = req.url_params.get(
"birth_date");
180 query.birth_date = birth_date;
183 auto birth_date_from = req.url_params.get(
"birth_date_from");
184 if (birth_date_from) {
185 query.birth_date_from = birth_date_from;
188 auto birth_date_to = req.url_params.get(
"birth_date_to");
190 query.birth_date_to = birth_date_to;
193 auto sex = req.url_params.get(
"sex");
200 count_query.
limit = 0;
202 auto all_patients_result = ctx->database->search_patients(count_query);
203 if (!all_patients_result.is_ok()) {
206 all_patients_result.error().message);
209 size_t total_count = all_patients_result.value().size();
212 auto patients_result = ctx->database->search_patients(query);
213 if (!patients_result.is_ok()) {
216 patients_result.error().message);
221 res.body = patients_to_json(patients_result.value(), total_count);
226 CROW_ROUTE(app,
"/api/v1/patients/<string>")
227 .methods(crow::HTTPMethod::GET)(
228 [ctx](
const crow::request & ,
const std::string &patient_id) {
230 res.add_header(
"Content-Type",
"application/json");
231 add_cors_headers(res, *ctx);
233 if (!ctx->database) {
236 "Database not configured");
240 auto patient = ctx->database->find_patient(patient_id);
248 res.body = patient_to_json(*patient);
253 CROW_ROUTE(app,
"/api/v1/patients/<string>/studies")
254 .methods(crow::HTTPMethod::GET)(
255 [ctx](
const crow::request & ,
const std::string &patient_id) {
257 res.add_header(
"Content-Type",
"application/json");
258 add_cors_headers(res, *ctx);
260 if (!ctx->database) {
263 "Database not configured");
268 auto patient = ctx->database->find_patient(patient_id);
275 auto studies_result = ctx->database->list_studies(patient_id);
276 if (!studies_result.is_ok()) {
279 studies_result.error().message);
284 res.body = studies_to_json(studies_result.value());