From 2f4b9db39239ed5150094a81743beea42a3eedc2 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Thu, 1 Apr 2021 22:18:46 -0700 Subject: Initial work on SQLite database --- core/src/Model/Project.cpp | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) (limited to 'core/src/Model/Project.cpp') diff --git a/core/src/Model/Project.cpp b/core/src/Model/Project.cpp index cdb88c6..c20e0c8 100644 --- a/core/src/Model/Project.cpp +++ b/core/src/Model/Project.cpp @@ -21,23 +21,22 @@ void ReadItemList(ItemList& list, const fs::path& filePath) { } } -Project Project::Load(const fs::path& projectFilePath) { +Project::Project(const fs::path& rootPath) + : mRootPath{ rootPath } + , mRootPathString{ mRootPath.string() } + , mDb(*this) { // TODO better diagnostic const char* kInvalidFormatErr = "Failed to load project: invalid format."; - std::ifstream ifs(projectFilePath); + std::ifstream ifs(rootPath / "cplt_project.json"); if (!ifs) { std::string message; message += "Failed to load project file at '"; - message += projectFilePath.string(); + message += rootPath.string(); message += "'."; throw std::runtime_error(message); } - Project proj; - proj.mRootPath = projectFilePath.parent_path(); - proj.mRootPathString = proj.mRootPath.string(); - { Json::Value root; ifs >> root; @@ -48,30 +47,23 @@ Project Project::Load(const fs::path& projectFilePath) { } if (auto& name = croot["Name"]; name.isString()) { - proj.mName = name.asString(); + mName = name.asString(); } else { throw std::runtime_error(kInvalidFormatErr); } } - auto itemsDir = proj.mRootPath / "items"; - ReadItemList(proj.Products, itemsDir / "products.json"); - ReadItemList(proj.Factories, itemsDir / "factories.json"); - ReadItemList(proj.Customers, itemsDir / "customers.json"); - - return proj; -} - -Project Project::LoadDir(const std::filesystem::path& projectPath) { - return Load(projectPath / "cplt_project.json"); + auto itemsDir = mRootPath / "items"; + ReadItemList(Products, itemsDir / "products.json"); + ReadItemList(Factories, itemsDir / "factories.json"); + ReadItemList(Customers, itemsDir / "customers.json"); } -Project Project::Create(std::string name, const fs::path& path) { - Project proj; - proj.mRootPath = path; - proj.mRootPathString = path.string(); - proj.mName = std::move(name); - return proj; +Project::Project(std::filesystem::path rootPath, std::string name) + : mRootPath{ std::move(rootPath) } + , mRootPathString{ mRootPath.string() } + , mName{ std::move(name) } + , mDb(*this) { } const fs::path& Project::GetPath() const { -- cgit v1.2.3-70-g09d2