* @brief Moves a dataset to a new position within its group. */
| 3504 | * @brief Moves a dataset to a new position within its group. |
| 3505 | */ |
| 3506 | API::CommandResponse API::Handlers::ProjectHandler::datasetMove(const QString& id, |
| 3507 | const QJsonObject& params) |
| 3508 | { |
| 3509 | if (!params.contains(Keys::UniqueId)) |
| 3510 | return CommandResponse::makeError( |
| 3511 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: uniqueId")); |
| 3512 | |
| 3513 | if (!params.contains(QStringLiteral("newPosition"))) |
| 3514 | return CommandResponse::makeError( |
| 3515 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: newPosition")); |
| 3516 | |
| 3517 | const int uniqueId = params.value(Keys::UniqueId).toInt(); |
| 3518 | const int newPosition = params.value(QStringLiteral("newPosition")).toInt(); |
| 3519 | const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false); |
| 3520 | |
| 3521 | auto& pm = DataModel::ProjectModel::instance(); |
| 3522 | const auto& groups = pm.groups(); |
| 3523 | |
| 3524 | const DataModel::Group* matchGroup = nullptr; |
| 3525 | const DataModel::Dataset* matchDataset = nullptr; |
| 3526 | for (const auto& group : groups) { |
| 3527 | for (const auto& dataset : group.datasets) { |
| 3528 | if (dataset.uniqueId == uniqueId) { |
| 3529 | matchGroup = &group; |
| 3530 | matchDataset = &dataset; |
| 3531 | break; |
| 3532 | } |
| 3533 | } |
| 3534 | if (matchDataset) |
| 3535 | break; |
| 3536 | } |
| 3537 | |
| 3538 | if (!matchDataset) |
| 3539 | return CommandResponse::makeError( |
| 3540 | id, |
| 3541 | ErrorCode::InvalidParam, |
| 3542 | QStringLiteral("Dataset not found for uniqueId %1").arg(uniqueId)); |
| 3543 | |
| 3544 | const int oldPosition = matchDataset->datasetId; |
| 3545 | const int clampedNewId = |
| 3546 | std::clamp(newPosition, 0, static_cast<int>(matchGroup->datasets.size()) - 1); |
| 3547 | |
| 3548 | QJsonArray renumbered; |
| 3549 | for (const auto& d : matchGroup->datasets) { |
| 3550 | const int newId = projectedAfterMove(d.datasetId, oldPosition, clampedNewId); |
| 3551 | if (newId == d.datasetId) |
| 3552 | continue; |
| 3553 | |
| 3554 | QJsonObject row; |
| 3555 | row[QStringLiteral("groupId")] = matchGroup->groupId; |
| 3556 | row[QStringLiteral("oldDatasetId")] = d.datasetId; |
| 3557 | row[QStringLiteral("newDatasetId")] = newId; |
| 3558 | row[Keys::UniqueId] = d.uniqueId; |
| 3559 | row[QStringLiteral("title")] = d.title; |
| 3560 | renumbered.append(row); |
| 3561 | } |
| 3562 | |
| 3563 | qint64 preEpoch = 0; |
nothing calls this directly
no test coverage detected