feat(data): ApiProjectRepository 实现 5 个导航接口
This commit is contained in:
parent
695aa8c310
commit
fa4bbf08b3
|
|
@ -3,7 +3,8 @@ find_package(Qt6 COMPONENTS Core REQUIRED)
|
|||
add_library(geopro_data STATIC
|
||||
parse/SampleParsers.cpp
|
||||
repo/LocalSampleRepository.cpp
|
||||
dto/NavDto.cpp)
|
||||
dto/NavDto.cpp
|
||||
api/ApiProjectRepository.cpp)
|
||||
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_compile_features(geopro_data PUBLIC cxx_std_17)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
Loading…
Reference in New Issue