| 474 | } |
| 475 | |
| 476 | void RenderingController::HandlePUT_selectedTime(const httplib::Request& req, httplib::Response& res) const |
| 477 | { |
| 478 | if (req.body.empty()) |
| 479 | { |
| 480 | const auto error = ErrorResponse::InvalidRequest("Request body is required.", req.path); |
| 481 | this->SendErrorResponse(res, 400, error); |
| 482 | return; |
| 483 | } |
| 484 | |
| 485 | nlohmann::json body; |
| 486 | try |
| 487 | { |
| 488 | body = nlohmann::json::parse(req.body); |
| 489 | } |
| 490 | catch (const nlohmann::json::exception&) |
| 491 | { |
| 492 | const auto error = ErrorResponse::InvalidRequest("Invalid JSON body.", req.path); |
| 493 | this->SendErrorResponse(res, 400, error); |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | const bool hasTimepointMs = body.contains("timepoint_ms"); |
| 498 | const bool hasTimestep = body.contains("timestep"); |
| 499 | |
| 500 | if (hasTimepointMs && hasTimestep) |
| 501 | { |
| 502 | const auto error = ErrorResponse::InvalidRequest( |
| 503 | "Provide exactly one of 'timepoint_ms' or 'timestep', not both.", req.path); |
| 504 | this->SendErrorResponse(res, 400, error); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | if (!hasTimepointMs && !hasTimestep) |
| 509 | { |
| 510 | const auto error = ErrorResponse::InvalidRequest( |
| 511 | "Either 'timepoint_ms' (number) or 'timestep' (integer) is required.", req.path); |
| 512 | this->SendErrorResponse(res, 400, error); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | if (hasTimepointMs && !body["timepoint_ms"].is_number()) |
| 517 | { |
| 518 | const auto error = ErrorResponse::InvalidRequest( |
| 519 | "'timepoint_ms' must be a number.", req.path); |
| 520 | this->SendErrorResponse(res, 400, error); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (hasTimestep && !body["timestep"].is_number_integer()) |
| 525 | { |
| 526 | const auto error = ErrorResponse::InvalidRequest( |
| 527 | "'timestep' must be an integer.", req.path); |
| 528 | this->SendErrorResponse(res, 400, error); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | if (hasTimestep && body["timestep"].get<int>() < 0) |
| 533 | { |