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

Method templateApply

app/src/API/Handlers/ProjectHandler.cpp:5606–5654  ·  view source on GitHub ↗

* @brief Loads a template into the project model. */

Source from the content-addressed store, hash-verified

5604 * @brief Loads a template into the project model.
5605 */
5606API::CommandResponse API::Handlers::ProjectHandler::templateApply(const QString& id,
5607 const QJsonObject& params)
5608{
5609 if (!params.contains(QStringLiteral("templateId")))
5610 return CommandResponse::makeError(
5611 id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: templateId"));
5612
5613 const auto templateId = params.value(QStringLiteral("templateId")).toString();
5614 const auto body = loadTemplateBodyById(templateId);
5615 if (body.isNull())
5616 return CommandResponse::makeError(
5617 id,
5618 ErrorCode::InvalidParam,
5619 QStringLiteral("Template not found: %1. Call project.template.list for the "
5620 "available ids.")
5621 .arg(templateId));
5622
5623 const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false);
5624 if (isDryRun) {
5625 QJsonObject result;
5626 result[QStringLiteral("dryRun")] = true;
5627 result[QStringLiteral("templateId")] = templateId;
5628 result[QStringLiteral("wouldDiscard")] = summarizeCurrentProject();
5629 result[QStringLiteral("wouldApply")] = summarizeProjectJson(body.object());
5630 result[QStringLiteral("warning")] =
5631 QStringLiteral("DRY RUN: template not applied. wouldDiscard shows what would be lost; "
5632 "wouldApply shows what would replace it. Confirm before re-issuing "
5633 "without dryRun:true.");
5634 return CommandResponse::makeSuccess(id, result);
5635 }
5636
5637 auto& project = DataModel::ProjectModel::instance();
5638 project.setSuppressMessageBoxes(true);
5639 const bool ok = project.loadFromJsonDocument(body);
5640 project.setSuppressMessageBoxes(false);
5641
5642 if (!ok)
5643 return CommandResponse::makeError(
5644 id,
5645 ErrorCode::ExecutionError,
5646 QStringLiteral("Template applied but project failed to validate"));
5647
5648 QJsonObject result;
5649 result[QStringLiteral("templateId")] = templateId;
5650 result[QStringLiteral("title")] = project.title();
5651 result[QStringLiteral("groupCount")] = project.groupCount();
5652 result[QStringLiteral("datasetCount")] = project.datasetCount();
5653 return CommandResponse::makeSuccess(id, result);
5654}
5655
5656//--------------------------------------------------------------------------------------------------
5657// Project consistency validation

Callers

nothing calls this directly

Calls 10

loadTemplateBodyByIdFunction · 0.85
summarizeCurrentProjectFunction · 0.85
summarizeProjectJsonFunction · 0.85
loadFromJsonDocumentMethod · 0.80
titleMethod · 0.80
datasetCountMethod · 0.80
containsMethod · 0.45
valueMethod · 0.45
groupCountMethod · 0.45

Tested by

no test coverage detected