* @brief Delete a group by id */
| 1719 | * @brief Delete a group by id |
| 1720 | */ |
| 1721 | API::CommandResponse API::Handlers::ProjectHandler::groupDelete(const QString& id, |
| 1722 | const QJsonObject& params) |
| 1723 | { |
| 1724 | if (!params.contains(QStringLiteral("groupId"))) |
| 1725 | return CommandResponse::makeError( |
| 1726 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: groupId")); |
| 1727 | |
| 1728 | const int groupId = params.value(QStringLiteral("groupId")).toInt(); |
| 1729 | auto& project = DataModel::ProjectModel::instance(); |
| 1730 | const auto& groups = project.groups(); |
| 1731 | if (groupId < 0 || static_cast<size_t>(groupId) >= groups.size()) |
| 1732 | return CommandResponse::makeError( |
| 1733 | id, ErrorCode::InvalidParam, QStringLiteral("Group id not found: %1").arg(groupId)); |
| 1734 | |
| 1735 | const auto& targetGroup = groups[groupId]; |
| 1736 | QJsonArray childDatasets; |
| 1737 | for (const auto& d : targetGroup.datasets) { |
| 1738 | QJsonObject row; |
| 1739 | row[Keys::DatasetId] = d.datasetId; |
| 1740 | row[Keys::UniqueId] = d.uniqueId; |
| 1741 | row[QStringLiteral("title")] = d.title; |
| 1742 | childDatasets.append(row); |
| 1743 | } |
| 1744 | |
| 1745 | QJsonObject deleted; |
| 1746 | deleted[QStringLiteral("groupId")] = groupId; |
| 1747 | deleted[QStringLiteral("title")] = targetGroup.title; |
| 1748 | deleted[QStringLiteral("widget")] = targetGroup.widget; |
| 1749 | deleted[QStringLiteral("datasetCount")] = childDatasets.size(); |
| 1750 | deleted[QStringLiteral("datasets")] = childDatasets; |
| 1751 | |
| 1752 | const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false); |
| 1753 | |
| 1754 | QJsonArray renumbered; |
| 1755 | for (const auto& g : groups) { |
| 1756 | if (g.groupId <= groupId) |
| 1757 | continue; |
| 1758 | |
| 1759 | QJsonObject row; |
| 1760 | row[QStringLiteral("oldGroupId")] = g.groupId; |
| 1761 | row[QStringLiteral("newGroupId")] = g.groupId - 1; |
| 1762 | row[QStringLiteral("title")] = g.title; |
| 1763 | row[QStringLiteral("datasetCount")] = static_cast<int>(g.datasets.size()); |
| 1764 | renumbered.append(row); |
| 1765 | } |
| 1766 | |
| 1767 | QString backupPath; |
| 1768 | qint64 preEpoch = 0; |
| 1769 | if (!isDryRun) { |
| 1770 | backupPath = Misc::BackupManager::instance().snapshot(QStringLiteral("pre-groupDelete")); |
| 1771 | preEpoch = captureProjectEpoch(); |
| 1772 | project.deleteGroup(groupId); |
| 1773 | } |
| 1774 | |
| 1775 | QJsonObject result; |
| 1776 | if (isDryRun) |
| 1777 | result[QStringLiteral("dryRun")] = true; |
| 1778 |
nothing calls this directly
no test coverage detected