* @brief Renames a table. Returns an error if the target name is taken. */
| 454 | * @brief Renames a table. Returns an error if the target name is taken. |
| 455 | */ |
| 456 | API::CommandResponse API::Handlers::DataTablesHandler::tableRename(const QString& id, |
| 457 | const QJsonObject& params) |
| 458 | { |
| 459 | QString oldName; |
| 460 | QString newName; |
| 461 | if (!requireString(params, QStringLiteral("oldName"), oldName)) |
| 462 | return CommandResponse::makeError( |
| 463 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: oldName")); |
| 464 | |
| 465 | if (!requireString(params, QStringLiteral("newName"), newName)) |
| 466 | return CommandResponse::makeError( |
| 467 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: newName")); |
| 468 | |
| 469 | auto& pm = DataModel::ProjectModel::instance(); |
| 470 | |
| 471 | const auto& preTables = pm.tables(); |
| 472 | const bool hasOld = std::any_of( |
| 473 | preTables.begin(), preTables.end(), [&oldName](const auto& t) { return t.name == oldName; }); |
| 474 | const bool hasNew = std::any_of( |
| 475 | preTables.begin(), preTables.end(), [&newName](const auto& t) { return t.name == newName; }); |
| 476 | const bool collides = hasNew && (oldName != newName); |
| 477 | |
| 478 | bool applied = false; |
| 479 | if (hasOld && !collides) { |
| 480 | pm.renameTable(oldName, newName); |
| 481 | const auto& tables = pm.tables(); |
| 482 | applied = std::any_of( |
| 483 | tables.begin(), tables.end(), [&newName](const auto& t) { return t.name == newName; }); |
| 484 | } |
| 485 | |
| 486 | QJsonObject result; |
| 487 | result[QStringLiteral("oldName")] = oldName; |
| 488 | result[QStringLiteral("newName")] = newName; |
| 489 | result[QStringLiteral("renamed")] = applied; |
| 490 | return CommandResponse::makeSuccess(id, result); |
| 491 | } |
| 492 | |
| 493 | //-------------------------------------------------------------------------------------------------- |
| 494 | // Register mutations |
nothing calls this directly
no test coverage detected