195 crow::SimpleApp &app,
196 std::shared_ptr<rest_server_context> ctx) {
198 CROW_ROUTE(app,
"/api/v1/worklist")
199 .methods(crow::HTTPMethod::GET)([ctx](
const crow::request &req) {
201 res.add_header(
"Content-Type",
"application/json");
202 add_cors_headers(res, *ctx);
204 if (!ctx->database) {
212 auto [limit, offset] = parse_pagination(req);
217 query.offset = offset;
219 auto station_ae = req.url_params.get(
"station_ae");
221 query.station_ae = station_ae;
224 auto modality = req.url_params.get(
"modality");
226 query.modality = modality;
229 auto scheduled_date_from = req.url_params.get(
"scheduled_date_from");
230 if (scheduled_date_from) {
231 query.scheduled_date_from = scheduled_date_from;
234 auto scheduled_date_to = req.url_params.get(
"scheduled_date_to");
235 if (scheduled_date_to) {
236 query.scheduled_date_to = scheduled_date_to;
239 auto patient_id = req.url_params.get(
"patient_id");
241 query.patient_id = patient_id;
244 auto patient_name = req.url_params.get(
"patient_name");
246 query.patient_name = patient_name;
249 auto accession_no = req.url_params.get(
"accession_no");
251 query.accession_no = accession_no;
254 auto step_id = req.url_params.get(
"step_id");
256 query.step_id = step_id;
260 auto include_all = req.url_params.get(
"include_all_status");
261 if (include_all && std::string(include_all) ==
"true") {
262 query.include_all_status =
true;
267 count_query.
limit = 0;
269 auto all_items_result = ctx->database->query_worklist(count_query);
270 if (!all_items_result.is_ok()) {
273 all_items_result.error().message);
276 size_t total_count = all_items_result.value().size();
279 auto items_result = ctx->database->query_worklist(query);
280 if (!items_result.is_ok()) {
283 items_result.error().message);
288 res.body = worklist_items_to_json(items_result.value(), total_count);
293 CROW_ROUTE(app,
"/api/v1/worklist")
294 .methods(crow::HTTPMethod::POST)([ctx](
const crow::request &req) {
296 res.add_header(
"Content-Type",
"application/json");
297 add_cors_headers(res, *ctx);
299 if (!ctx->database) {
307 auto item = parse_worklist_item_json(req.body);
312 "Missing required fields: step_id, patient_id, modality, "
313 "scheduled_datetime");
318 auto result = ctx->database->add_worklist_item(*item);
319 if (result.is_err()) {
326 auto created_item = ctx->database->find_worklist_by_pk(result.value());
329 res.body = worklist_item_to_json(*created_item);
338 CROW_ROUTE(app,
"/api/v1/worklist/<int>")
339 .methods(crow::HTTPMethod::GET)(
340 [ctx](
const crow::request & ,
int pk) {
342 res.add_header(
"Content-Type",
"application/json");
343 add_cors_headers(res, *ctx);
345 if (!ctx->database) {
348 "Database not configured");
352 auto item = ctx->database->find_worklist_by_pk(pk);
360 res.body = worklist_item_to_json(*item);
365 CROW_ROUTE(app,
"/api/v1/worklist/<int>")
366 .methods(crow::HTTPMethod::PUT)(
367 [ctx](
const crow::request &req,
int pk) {
369 res.add_header(
"Content-Type",
"application/json");
370 add_cors_headers(res, *ctx);
372 if (!ctx->database) {
375 "Database not configured");
380 auto existing_item = ctx->database->find_worklist_by_pk(pk);
381 if (!existing_item) {
388 auto get_json_string = [&req](
const std::string &key) -> std::string {
389 std::string search_key =
"\"" + key +
"\":\"";
390 auto pos = req.body.find(search_key);
391 if (pos == std::string::npos) {
394 pos += search_key.length();
395 auto end_pos = req.body.find(
"\"", pos);
396 if (end_pos == std::string::npos) {
399 return req.body.substr(pos, end_pos - pos);
402 std::string new_status = get_json_string(
"step_status");
403 if (new_status.empty()) {
406 "step_status is required for update");
411 auto result = ctx->database->update_worklist_status(
412 existing_item->step_id,
413 existing_item->accession_no,
416 if (result.is_err()) {
423 auto updated_item = ctx->database->find_worklist_by_pk(pk);
426 res.body = worklist_item_to_json(*updated_item);
435 CROW_ROUTE(app,
"/api/v1/worklist/<int>")
436 .methods(crow::HTTPMethod::DELETE)(
437 [ctx](
const crow::request & ,
int pk) {
439 res.add_header(
"Content-Type",
"application/json");
440 add_cors_headers(res, *ctx);
442 if (!ctx->database) {
445 "Database not configured");
450 auto item = ctx->database->find_worklist_by_pk(pk);
457 auto result = ctx->database->delete_worklist_item(
458 item->step_id, item->accession_no);
459 if (result.is_err()) {