* @brief Bulk-creates N datasets in one call with optional title/index patterns. */
| 1960 | * @brief Bulk-creates N datasets in one call with optional title/index patterns. |
| 1961 | */ |
| 1962 | API::CommandResponse API::Handlers::ProjectHandler::datasetAddMany(const QString& id, |
| 1963 | const QJsonObject& params) |
| 1964 | { |
| 1965 | constexpr int kMaxAddManyCount = 1024; |
| 1966 | |
| 1967 | for (const auto& key : |
| 1968 | {QStringLiteral("groupId"), QStringLiteral("count"), QStringLiteral("options")}) { |
| 1969 | if (!params.contains(key)) |
| 1970 | return CommandResponse::makeError( |
| 1971 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: %1").arg(key)); |
| 1972 | } |
| 1973 | |
| 1974 | const int groupId = params.value(QStringLiteral("groupId")).toInt(); |
| 1975 | const int count = params.value(QStringLiteral("count")).toInt(); |
| 1976 | const int options = params.value(QStringLiteral("options")).toInt(); |
| 1977 | |
| 1978 | if (count <= 0 || count > kMaxAddManyCount) |
| 1979 | return CommandResponse::makeError( |
| 1980 | id, |
| 1981 | ErrorCode::InvalidParam, |
| 1982 | QStringLiteral("Invalid count: must be 1..%1 (got %2)") |
| 1983 | .arg(QString::number(kMaxAddManyCount), QString::number(count))); |
| 1984 | |
| 1985 | if (options < 0 || options > 0b111111111) |
| 1986 | return CommandResponse::makeError( |
| 1987 | id, ErrorCode::InvalidParam, QStringLiteral("Invalid options: must be 0-511 (bit flags)")); |
| 1988 | |
| 1989 | auto& project = DataModel::ProjectModel::instance(); |
| 1990 | const auto& groups = project.groups(); |
| 1991 | if (groupId < 0 || static_cast<size_t>(groupId) >= groups.size()) |
| 1992 | return CommandResponse::makeError( |
| 1993 | id, ErrorCode::InvalidParam, QStringLiteral("Group id not found: %1").arg(groupId)); |
| 1994 | |
| 1995 | const QString titlePattern = params.value(QStringLiteral("titlePattern")).toString(); |
| 1996 | const int startNumber = params.contains(QStringLiteral("startNumber")) |
| 1997 | ? params.value(QStringLiteral("startNumber")).toInt() |
| 1998 | : 1; |
| 1999 | const int startIndex = params.contains(QStringLiteral("startIndex")) |
| 2000 | ? params.value(QStringLiteral("startIndex")).toInt() |
| 2001 | : -1; |
| 2002 | |
| 2003 | if (startIndex < -1) |
| 2004 | return CommandResponse::makeError( |
| 2005 | id, |
| 2006 | ErrorCode::InvalidParam, |
| 2007 | QStringLiteral("Invalid startIndex: must be -1 (auto), 0, or 1+")); |
| 2008 | |
| 2009 | const auto headline = pickHeadlineDatasetOption(options); |
| 2010 | const int remainingBits = options & ~static_cast<int>(headline); |
| 2011 | |
| 2012 | project.setAutoSaveSuspended(true); |
| 2013 | project.setSelectedGroup(groups[groupId]); |
| 2014 | |
| 2015 | QJsonArray created; |
| 2016 | for (int i = 0; i < count; ++i) { |
| 2017 | project.addDataset(headline); |
| 2018 | |
| 2019 | const auto& post = project.groups(); |
nothing calls this directly
no test coverage detected