* @brief Deletes a table by name. With dryRun:true, reports what would be deleted without * mutating; the delete itself is a no-op if the table is not found. */
| 409 | * mutating; the delete itself is a no-op if the table is not found. |
| 410 | */ |
| 411 | API::CommandResponse API::Handlers::DataTablesHandler::tableDelete(const QString& id, |
| 412 | const QJsonObject& params) |
| 413 | { |
| 414 | QString name; |
| 415 | if (!requireString(params, QStringLiteral("name"), name)) |
| 416 | return CommandResponse::makeError( |
| 417 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: name")); |
| 418 | |
| 419 | auto& pm = DataModel::ProjectModel::instance(); |
| 420 | const auto& tables = pm.tables(); |
| 421 | const auto it = |
| 422 | std::find_if(tables.begin(), tables.end(), [&name](const auto& t) { return t.name == name; }); |
| 423 | |
| 424 | if (it == tables.end()) |
| 425 | return CommandResponse::makeError( |
| 426 | id, ErrorCode::InvalidParam, QStringLiteral("Table not found: %1").arg(name)); |
| 427 | |
| 428 | const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false); |
| 429 | |
| 430 | QJsonArray registers; |
| 431 | for (const auto& r : it->registers) |
| 432 | registers.append(r.name); |
| 433 | |
| 434 | QJsonObject result; |
| 435 | result[QStringLiteral("name")] = name; |
| 436 | result[QStringLiteral("registerCount")] = static_cast<int>(it->registers.size()); |
| 437 | result[QStringLiteral("registers")] = registers; |
| 438 | |
| 439 | if (isDryRun) { |
| 440 | result[QStringLiteral("dryRun")] = true; |
| 441 | result[QStringLiteral("deleted")] = false; |
| 442 | result[QStringLiteral("warning")] = QStringLiteral( |
| 443 | "DRY RUN: no changes were written. Re-call without dryRun:true to delete the table " |
| 444 | "and all its registers."); |
| 445 | return CommandResponse::makeSuccess(id, result); |
| 446 | } |
| 447 | |
| 448 | pm.deleteTable(name); |
| 449 | result[QStringLiteral("deleted")] = true; |
| 450 | return CommandResponse::makeSuccess(id, result); |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * @brief Renames a table. Returns an error if the target name is taken. |
nothing calls this directly
no test coverage detected