diff options
Diffstat (limited to 'core/src/Model/Project.cpp')
-rw-r--r-- | core/src/Model/Project.cpp | 106 |
1 files changed, 100 insertions, 6 deletions
diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp index 74e7142..2d7c82a 100644 --- a/core/src/Model/Project.cpp +++ b/core/src/Model/Project.cpp @@ -1,5 +1,8 @@ #include "Project.hpp" +#include "Model/Workflow/Workflow.hpp" +#include "Utils/Macros.hpp" + #include <json/reader.h> #include <json/value.h> #include <json/writer.h> @@ -25,7 +28,7 @@ void ReadItemList(ItemList<T>& list, const fs::path& filePath) Project::Project(const fs::path& rootPath) : mRootPath{ rootPath } , mRootPathString{ mRootPath.string() } - , mDb(*this) + , Database(*this) { // TODO better diagnostic const char* kInvalidFormatErr = "Failed to load project: invalid format."; @@ -59,13 +62,30 @@ Project::Project(const fs::path& rootPath) ReadItemList(Products, itemsDir / "products.json"); ReadItemList(Factories, itemsDir / "factories.json"); ReadItemList(Customers, itemsDir / "customers.json"); + + auto workflowsDir = mRootPath / "workflows"; + fs::create_directories(workflowsDir); + + for (auto& entry : fs::directory_iterator(workflowsDir)) { + if (!entry.is_regular_file()) continue; + auto& path = entry.path(); + if (path.extension() != ".cplt-workflow") continue; + + auto name = path.stem().string(); + auto [it, DISCARD] = mWorkflows.insert(name, WorkflowInfo{}); + auto& info = it.value(); + + info.Name = std::move(name); + info.PathStringCache = path.string(); + info.Path = path; + } } Project::Project(std::filesystem::path rootPath, std::string name) : mRootPath{ std::move(rootPath) } , mRootPathString{ mRootPath.string() } , mName{ std::move(name) } - , mDb(*this) + , Database(*this) { } @@ -89,14 +109,68 @@ void Project::SetName(std::string name) mName = std::move(name); } -const TransactionModel& Project::GetTransactionsModel() const +const decltype(Project::mWorkflows)& Project::GetWorkflows() const +{ + return mWorkflows; +} + +std::unique_ptr<Workflow> Project::LoadWorkflow(std::string_view name) +{ + auto iter = mWorkflows.find(name); + if (iter == mWorkflows.end()) { + return iter.value().LoadFromDisk(); + } else { + return nullptr; + } +} + +std::unique_ptr<Workflow> Project::CreateWorkflow(std::string_view name) { - return mDb; + if (mWorkflows.find(name) != mWorkflows.end()) { + // Workflow with name already exists + return nullptr; + } + + auto workflow = std::make_unique<Workflow>(); + auto [it, DISCARD] = mWorkflows.insert(name, WorkflowInfo{}); + auto& info = it.value(); + + info.Name = name; + info.Path = GetWorkflowPath(name); + + return workflow; } -TransactionModel& Project::GetTransactionsModel() +bool Project::RemoveWorkflow(std::string_view name) { - return mDb; + auto iter = mWorkflows.find(name); + if (iter == mWorkflows.end()) { + return false; + } + auto& info = iter.value(); + + fs::remove(info.Path); + mWorkflows.erase(iter); + + return true; +} + +bool Project::RenameWorkflow(std::string_view name, std::string_view newName) +{ + auto iter = mWorkflows.find(name); + if (iter == mWorkflows.end()) return false; + + auto info = std::move(iter.value()); + + auto& oldPath = info.Path; + auto newPath = GetWorkflowPath(newName); + fs::rename(oldPath, newPath); + info.Path = std::move(newPath); + + mWorkflows.insert(newName, std::move(info)); + mWorkflows.erase(iter); + + return true; } Json::Value Project::Serialize() @@ -127,3 +201,23 @@ void Project::WriteToDisk() WriteItemList(Factories, itemsDir / "factories.json"); WriteItemList(Customers, itemsDir / "customers.json"); } + +std::filesystem::path Project::GetDatabasesDirectory() const +{ + return mRootPath / "databases"; +} + +std::filesystem::path Project::GetItemsDirectory() const +{ + return mRootPath / "items"; +} + +std::filesystem::path Project::GetWorkflowsDirectory() const +{ + return mRootPath / "workflows"; +} + +std::filesystem::path Project::GetWorkflowPath(std::string_view name) const +{ + return (mRootPath / "workflows" / name).concat(".cplt-workflow"); +} |