* @brief Resolve a workspace by id (workspaceId) or case-insensitive title (workspace). */
| 146 | * @brief Resolve a workspace by id (workspaceId) or case-insensitive title (workspace). |
| 147 | */ |
| 148 | API::CommandResponse API::Handlers::AssistantHandler::workspaceResolve(const QString& id, |
| 149 | const QJsonObject& params) |
| 150 | { |
| 151 | const bool hasId = params.contains(QStringLiteral("workspaceId")); |
| 152 | const bool hasTitle = params.contains(QStringLiteral("workspace")); |
| 153 | if (!hasId && !hasTitle) |
| 154 | return CommandResponse::makeError( |
| 155 | id, |
| 156 | ErrorCode::MissingParam, |
| 157 | QStringLiteral("Provide one of: workspaceId (integer) or workspace (title).")); |
| 158 | |
| 159 | const auto listResp = |
| 160 | forward(QStringLiteral("project.workspace.list"), QStringLiteral("inner"), QJsonObject()); |
| 161 | if (!listResp.success) |
| 162 | return rewrap(id, listResp); |
| 163 | |
| 164 | const auto workspaces = workspacesFromListResponse(listResp); |
| 165 | |
| 166 | QJsonArray matches; |
| 167 | if (hasId) { |
| 168 | const int wid = params.value(QStringLiteral("workspaceId")).toInt(); |
| 169 | for (const auto& v : workspaces) { |
| 170 | const auto ws = v.toObject(); |
| 171 | if (ws.value(QStringLiteral("id")).toInt() == wid) |
| 172 | matches.append(ws); |
| 173 | } |
| 174 | } else { |
| 175 | const auto needle = params.value(QStringLiteral("workspace")).toString(); |
| 176 | for (const auto& v : workspaces) { |
| 177 | const auto ws = v.toObject(); |
| 178 | if (ws.value(QStringLiteral("title")).toString().compare(needle, Qt::CaseInsensitive) == 0) |
| 179 | matches.append(ws); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | if (matches.isEmpty()) { |
| 184 | QJsonObject data; |
| 185 | data[QStringLiteral("candidates")] = workspaces; |
| 186 | return CommandResponse::makeError( |
| 187 | id, |
| 188 | ErrorCode::InvalidParam, |
| 189 | QStringLiteral("No workspace matched. Pick one from data.candidates by id or title."), |
| 190 | data); |
| 191 | } |
| 192 | |
| 193 | if (matches.size() > 1) { |
| 194 | QJsonObject data; |
| 195 | data[QStringLiteral("matches")] = matches; |
| 196 | return CommandResponse::makeError( |
| 197 | id, |
| 198 | ErrorCode::InvalidParam, |
| 199 | QStringLiteral("Ambiguous match (%1 workspaces share that title). " |
| 200 | "Re-call with workspaceId.") |
| 201 | .arg(matches.size()), |
| 202 | data); |
| 203 | } |
| 204 | |
| 205 | QJsonObject result; |
nothing calls this directly
no test coverage detected