feat(data): ApiProjectRepository 实现 5 个导航接口

This commit is contained in:
gaozheng 2026-06-09 11:40:40 +08:00
parent 695aa8c310
commit fa4bbf08b3
3 changed files with 91 additions and 1 deletions

View File

@ -3,7 +3,8 @@ find_package(Qt6 COMPONENTS Core REQUIRED)
add_library(geopro_data STATIC add_library(geopro_data STATIC
parse/SampleParsers.cpp parse/SampleParsers.cpp
repo/LocalSampleRepository.cpp repo/LocalSampleRepository.cpp
dto/NavDto.cpp) dto/NavDto.cpp
api/ApiProjectRepository.cpp)
target_include_directories(geopro_data PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(geopro_data PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(geopro_data PUBLIC geopro_core geopro_net Qt6::Core PRIVATE nlohmann_json::nlohmann_json) target_link_libraries(geopro_data PUBLIC geopro_core geopro_net Qt6::Core PRIVATE nlohmann_json::nlohmann_json)
target_compile_features(geopro_data PUBLIC cxx_std_17) target_compile_features(geopro_data PUBLIC cxx_std_17)

View File

@ -0,0 +1,66 @@
#include "api/ApiProjectRepository.hpp"
#include <QJsonArray>
#include <QJsonObject>
#include <QString>
#include "ApiClient.hpp"
#include "dto/NavDto.hpp"
namespace geopro::data {
namespace {
constexpr int kCodeSuccess = 200;
bool ok(const net::ApiResponse& r) { return r.code == kCodeSuccess; }
std::string errorOf(const net::ApiResponse& r, const char* fallback) {
if (!r.msg.isEmpty()) return r.msg.toStdString();
if (!r.rawError.isEmpty()) return r.rawError.toStdString();
return fallback;
}
} // namespace
ApiProjectRepository::ApiProjectRepository(net::ApiClient& api) : api_(api) {}
RepoResult<std::vector<Workspace>> ApiProjectRepository::listWorkspaces() {
const net::ApiResponse r =
api_.get(QStringLiteral("/business/system/tenant/enterprise/joined/list"));
if (!ok(r)) return {false, {}, errorOf(r, "listWorkspaces failed")};
return {true, dto::parseWorkspaces(r.data.value(QStringLiteral("value")).toArray()), {}};
}
RepoResult<bool> ApiProjectRepository::switchWorkspace(const std::string& tenantId) {
const QString path =
QStringLiteral("/business/system/tenant/enterprise/switch/%1").arg(QString::fromStdString(tenantId));
const net::ApiResponse r = api_.postJson(path, QJsonObject{});
if (!ok(r)) return {false, false, errorOf(r, "switchWorkspace failed")};
return {true, true, {}};
}
RepoResult<std::vector<ProjectSummary>> ApiProjectRepository::listProjects(
const std::string& lastProjectId) {
const QString path = QStringLiteral("/business/project/queryByUser?lastProjectId=%1")
.arg(QString::fromStdString(lastProjectId));
const net::ApiResponse r = api_.get(path);
if (!ok(r)) return {false, {}, errorOf(r, "listProjects failed")};
return {true, dto::parseProjects(r.data).projects, {}};
}
RepoResult<std::vector<StructNode>> ApiProjectRepository::loadStructure(const std::string& projectId) {
const QJsonObject body{{QStringLiteral("projectId"), QString::fromStdString(projectId)}};
const net::ApiResponse r =
api_.postJson(QStringLiteral("/business/projectWorkbench/queryProjectStruct"), body);
if (!ok(r)) return {false, {}, errorOf(r, "loadStructure failed")};
return {true, dto::parseStructNodes(r.data.value(QStringLiteral("projectStructList")).toArray()), {}};
}
RepoResult<std::vector<DsNode>> ApiProjectRepository::loadDatasetsOfTm(const std::string& tmObjectId) {
const QString path = QStringLiteral("/business/projectWorkbench/queryDsByTmObjectId/%1")
.arg(QString::fromStdString(tmObjectId));
const net::ApiResponse r = api_.get(path);
if (!ok(r)) return {false, {}, errorOf(r, "loadDatasetsOfTm failed")};
return {true, dto::parseDatasets(r.data.value(QStringLiteral("value")).toArray()), {}};
}
} // namespace geopro::data

View File

@ -0,0 +1,23 @@
#pragma once
#include "repo/IProjectRepository.hpp"
namespace geopro::net { class ApiClient; }
namespace geopro::data {
// 用共享会话 ApiClient 实现导航仓储同步阻塞。token 由调用方注入 ApiClient。
class ApiProjectRepository : public IProjectRepository {
public:
explicit ApiProjectRepository(net::ApiClient& api);
RepoResult<std::vector<Workspace>> listWorkspaces() override;
RepoResult<bool> switchWorkspace(const std::string& tenantId) override;
RepoResult<std::vector<ProjectSummary>> listProjects(const std::string& lastProjectId) override;
RepoResult<std::vector<StructNode>> loadStructure(const std::string& projectId) override;
RepoResult<std::vector<DsNode>> loadDatasetsOfTm(const std::string& tmObjectId) override;
private:
net::ApiClient& api_;
};
} // namespace geopro::data