MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / registerDelete

Method registerDelete

app/src/API/Handlers/DataTablesHandler.cpp:535–583  ·  view source on GitHub ↗

* @brief Removes a register from a table. With dryRun:true, reports what would be deleted * without mutating. */

Source from the content-addressed store, hash-verified

533 * without mutating.
534 */
535API::CommandResponse API::Handlers::DataTablesHandler::registerDelete(const QString& id,
536 const QJsonObject& params)
537{
538 QString table;
539 QString name;
540 if (!requireString(params, QStringLiteral("table"), table))
541 return CommandResponse::makeError(
542 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: table"));
543
544 if (!requireString(params, QStringLiteral("name"), name))
545 return CommandResponse::makeError(
546 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: name"));
547
548 auto& pm = DataModel::ProjectModel::instance();
549 const auto& tables = pm.tables();
550 const auto tit =
551 std::find_if(tables.begin(), tables.end(), [&table](const auto& t) { return t.name == table; });
552
553 if (tit == tables.end())
554 return CommandResponse::makeError(
555 id, ErrorCode::InvalidParam, QStringLiteral("Table not found: %1").arg(table));
556
557 const auto rit = std::find_if(tit->registers.begin(),
558 tit->registers.end(),
559 [&name](const auto& r) { return r.name == name; });
560
561 if (rit == tit->registers.end())
562 return CommandResponse::makeError(
563 id, ErrorCode::InvalidParam, QStringLiteral("Register not found: %1/%2").arg(table, name));
564
565 const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false);
566
567 QJsonObject result;
568 result[QStringLiteral("table")] = table;
569 result[QStringLiteral("name")] = name;
570 result[QStringLiteral("computed")] = (rit->type == DataModel::RegisterType::Computed);
571
572 if (isDryRun) {
573 result[QStringLiteral("dryRun")] = true;
574 result[QStringLiteral("deleted")] = false;
575 result[QStringLiteral("warning")] = QStringLiteral(
576 "DRY RUN: no changes were written. Re-call without dryRun:true to delete the register.");
577 return CommandResponse::makeSuccess(id, result);
578 }
579
580 pm.deleteRegister(table, name);
581 result[QStringLiteral("deleted")] = true;
582 return CommandResponse::makeSuccess(id, result);
583}
584
585/**
586 * @brief Updates an existing register. Unspecified fields retain their value.

Callers

nothing calls this directly

Calls 4

requireStringFunction · 0.85
beginMethod · 0.80
deleteRegisterMethod · 0.80
valueMethod · 0.45

Tested by

no test coverage detected