Chuyển lưu trữ dữ liệu sang data base
Some checks failed
Test / test (push) Has been cancelled

This commit is contained in:
2026-06-17 11:16:30 +07:00
parent 4054d81aaf
commit 098e1b2b69
45 changed files with 1971 additions and 1657 deletions

View File

@@ -2,6 +2,7 @@
#include "domain/layout_profile.hpp"
#include "domain/layout_schema.hpp"
#include "storage/database.hpp"
#include "util/file_util.hpp"
#include "util/id_util.hpp"
#include "util/string_util.hpp"
@@ -20,6 +21,8 @@ std::filesystem::path StateRepository::profileFilePath(const std::string& id) co
std::optional<nlohmann::json> StateRepository::loadProfileFromDisk(const std::string& id) const
{
if (auto profile = db_.getLayoutProfile(id))
return profile;
const auto raw = FileUtil::readBinary(profileFilePath(id));
if (raw.empty())
return std::nullopt;
@@ -37,15 +40,12 @@ bool StateRepository::saveProfileToDisk(const nlohmann::json& profile) const
{
if (!profile.is_object() || !profile.contains("id") || !profile["id"].is_string())
return false;
std::error_code ec;
std::filesystem::create_directories(modelsDir(), ec);
auto body = profile.dump(2);
body.push_back('\n');
return FileUtil::writeBinaryAtomic(profileFilePath(profile["id"].get<std::string>()), body);
return db_.setLayoutProfile(profile);
}
bool StateRepository::deleteProfileFile(const std::string& id) const
{
db_.deleteLayoutProfile(id);
std::error_code ec;
std::filesystem::remove(profileFilePath(id), ec);
return true;
@@ -243,15 +243,14 @@ void StateRepository::bootstrapDefaultState()
app_.state["imus"] = profile.contains("imus") ? profile["imus"] : nlohmann::json::array();
}
StateRepository::StateRepository(std::filesystem::path data_path)
StateRepository::StateRepository(std::filesystem::path data_path, Database& db) : db_(db)
{
app_.data_path = std::move(data_path);
}
bool StateRepository::load()
{
const auto raw = FileUtil::readBinary(app_.data_path);
if (raw.empty())
if (!db_.getDocument("state", app_.state))
{
bootstrapDefaultState();
save();
@@ -259,7 +258,6 @@ bool StateRepository::load()
}
try
{
app_.state = nlohmann::json::parse(raw);
ensureSchema();
save();
return true;
@@ -309,9 +307,7 @@ bool StateRepository::save() const
try
{
const nlohmann::json disk = globalStateForDisk(app_.state);
auto raw = disk.dump(2);
raw.push_back('\n');
return FileUtil::writeBinaryAtomic(app_.data_path, raw);
return db_.setDocument("state", disk);
}
catch (...)
{