* @brief Find a dataset by title (optionally narrowed by sourceId / groupId). */
| 3374 | * @brief Find a dataset by title (optionally narrowed by sourceId / groupId). |
| 3375 | */ |
| 3376 | API::CommandResponse API::Handlers::ProjectHandler::datasetGetByTitle(const QString& id, |
| 3377 | const QJsonObject& params) |
| 3378 | { |
| 3379 | if (!params.contains(Keys::Title)) |
| 3380 | return CommandResponse::makeError( |
| 3381 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: title")); |
| 3382 | |
| 3383 | const QString title = params.value(Keys::Title).toString(); |
| 3384 | const bool hasSourceFilter = params.contains(Keys::SourceId); |
| 3385 | const bool hasGroupFilter = params.contains(Keys::GroupId); |
| 3386 | const int filterSourceId = hasSourceFilter ? params.value(Keys::SourceId).toInt() : 0; |
| 3387 | const int filterGroupId = hasGroupFilter ? params.value(Keys::GroupId).toInt() : 0; |
| 3388 | |
| 3389 | if (title.isEmpty()) |
| 3390 | return CommandResponse::makeError( |
| 3391 | id, ErrorCode::InvalidParam, QStringLiteral("title cannot be empty")); |
| 3392 | |
| 3393 | const auto& groups = DataModel::ProjectModel::instance().groups(); |
| 3394 | |
| 3395 | QJsonArray matches; |
| 3396 | for (const auto& group : groups) { |
| 3397 | if (hasGroupFilter && group.groupId != filterGroupId) |
| 3398 | continue; |
| 3399 | |
| 3400 | for (const auto& dataset : group.datasets) { |
| 3401 | if (hasSourceFilter && dataset.sourceId != filterSourceId) |
| 3402 | continue; |
| 3403 | |
| 3404 | if (dataset.title == title) |
| 3405 | matches.append(buildDatasetObject(dataset, group)); |
| 3406 | } |
| 3407 | } |
| 3408 | |
| 3409 | if (matches.isEmpty()) |
| 3410 | return CommandResponse::makeError( |
| 3411 | id, ErrorCode::InvalidParam, QStringLiteral("No dataset matched title '%1'").arg(title)); |
| 3412 | |
| 3413 | if (matches.size() > 1) { |
| 3414 | QJsonObject extra; |
| 3415 | extra[QStringLiteral("matches")] = matches; |
| 3416 | extra[QStringLiteral("hint")] = |
| 3417 | QStringLiteral("Multiple datasets match this title. Pass sourceId or groupId to " |
| 3418 | "disambiguate, or call project.dataset.getByPath."); |
| 3419 | return CommandResponse::makeError(id, |
| 3420 | ErrorCode::InvalidParam, |
| 3421 | QStringLiteral("Ambiguous title '%1' (%2 matches)") |
| 3422 | .arg(title, QString::number(matches.size())), |
| 3423 | extra); |
| 3424 | } |
| 3425 | |
| 3426 | return CommandResponse::makeSuccess(id, matches.first().toObject()); |
| 3427 | } |
| 3428 | |
| 3429 | /** |
| 3430 | * @brief Find a dataset by 'Group/Dataset' or 'Source/Group/Dataset' path. |