mirror of
https://github.com/sebastiandine/Card-Collection-Manager-3.git
synced 2026-08-29 01:08:49 +00:00
major: initial release
* initial development * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * pipeline * ci/cd * ci/cd * ci/cd * ci/cd * ci/cd * ci/cd * ci/cd * pokemon * pokemon * pokemon * pokemon * pokemon * pokemon * improvements * improvements * ci/cd * ci/cd * improvements * improvements * improvements * improvements * improvements * improvements * improvements * improvements * improvements --------- Co-authored-by: sdine <sdine@sdine.com>
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
// CprHttpClient: cpr-based implementation of IHttpClient.
|
||||
// All cpr/libcurl symbols stay confined to the .cpp file - any TU that just
|
||||
// needs to make HTTP calls only depends on the IHttpClient port.
|
||||
|
||||
#include "ccm/ports/IHttpClient.hpp"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace ccm {
|
||||
|
||||
class CprHttpClient final : public IHttpClient {
|
||||
public:
|
||||
explicit CprHttpClient(std::chrono::milliseconds timeout = std::chrono::milliseconds{30000});
|
||||
|
||||
Result<std::string> get(std::string_view url) override;
|
||||
|
||||
private:
|
||||
std::chrono::milliseconds timeout_;
|
||||
};
|
||||
|
||||
} // namespace ccm
|
||||
@@ -0,0 +1,85 @@
|
||||
#pragma once
|
||||
|
||||
// JsonCollectionRepository<T>: persists std::map<id, TCard> as a JSON object
|
||||
// keyed by stringified id. Layout matches the established collection file:
|
||||
//
|
||||
// <dataStorage>/<gameDir>/collection.json -> { "0": {...}, "1": {...} }
|
||||
//
|
||||
// Header-only because it's a template over the card type.
|
||||
|
||||
#include "ccm/domain/Configuration.hpp"
|
||||
#include "ccm/domain/Enums.hpp"
|
||||
#include "ccm/games/IGameModule.hpp"
|
||||
#include "ccm/ports/ICollectionRepository.hpp"
|
||||
#include "ccm/ports/IFileSystem.hpp"
|
||||
#include "ccm/services/ConfigService.hpp"
|
||||
#include "ccm/util/Result.hpp"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace ccm {
|
||||
|
||||
template <typename TCard>
|
||||
class JsonCollectionRepository final : public ICollectionRepository<TCard> {
|
||||
public:
|
||||
using Map = typename ICollectionRepository<TCard>::Map;
|
||||
// The repository needs a way to translate a Game enum into the per-game
|
||||
// subdirectory name ("magic" / "pokemon"); a small lookup function keeps
|
||||
// this layer free of concrete game module types.
|
||||
using DirNameFn = std::function<std::string(Game)>;
|
||||
|
||||
JsonCollectionRepository(IFileSystem& fs, ConfigService& config, DirNameFn dirName)
|
||||
: fs_(fs), config_(config), dirName_(std::move(dirName)) {}
|
||||
|
||||
Result<Map> load(Game game) override {
|
||||
const auto p = collectionPath(game);
|
||||
if (!fs_.exists(p)) {
|
||||
// Mirror the Rust "create on first read" semantics so the UI
|
||||
// never sees a missing-file error on a fresh install.
|
||||
Map empty;
|
||||
auto saved = save(game, empty);
|
||||
if (!saved) return Result<Map>::err(saved.error());
|
||||
return Result<Map>::ok(std::move(empty));
|
||||
}
|
||||
auto text = fs_.readText(p);
|
||||
if (!text) return Result<Map>::err(text.error());
|
||||
try {
|
||||
auto j = nlohmann::json::parse(text.value());
|
||||
Map out;
|
||||
for (auto it = j.begin(); it != j.end(); ++it) {
|
||||
std::uint32_t key = static_cast<std::uint32_t>(std::stoul(it.key()));
|
||||
out.emplace(key, it.value().template get<TCard>());
|
||||
}
|
||||
return Result<Map>::ok(std::move(out));
|
||||
} catch (const std::exception& e) {
|
||||
return Result<Map>::err(std::string("JSON parse error: ") + e.what());
|
||||
}
|
||||
}
|
||||
|
||||
Result<void> save(Game game, const Map& collection) override {
|
||||
nlohmann::json j = nlohmann::json::object();
|
||||
for (const auto& [id, card] : collection) {
|
||||
j[std::to_string(id)] = card;
|
||||
}
|
||||
const auto p = collectionPath(game);
|
||||
auto dirRes = fs_.ensureDirectory(p.parent_path());
|
||||
if (!dirRes) return dirRes;
|
||||
return fs_.writeText(p, j.dump(2));
|
||||
}
|
||||
|
||||
private:
|
||||
std::filesystem::path collectionPath(Game game) const {
|
||||
return std::filesystem::path(config_.current().dataStorage) /
|
||||
dirName_(game) / "collection.json";
|
||||
}
|
||||
|
||||
IFileSystem& fs_;
|
||||
ConfigService& config_;
|
||||
DirNameFn dirName_;
|
||||
};
|
||||
|
||||
} // namespace ccm
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
// JsonSetRepository: persists vector<Set> to `<dataStorage>/<game>/sets.json`.
|
||||
|
||||
#include "ccm/games/IGameModule.hpp"
|
||||
#include "ccm/ports/IFileSystem.hpp"
|
||||
#include "ccm/ports/ISetRepository.hpp"
|
||||
#include "ccm/services/ConfigService.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace ccm {
|
||||
|
||||
class JsonSetRepository final : public ISetRepository {
|
||||
public:
|
||||
using DirNameFn = std::function<std::string(Game)>;
|
||||
|
||||
JsonSetRepository(IFileSystem& fs, ConfigService& config, DirNameFn dirName);
|
||||
|
||||
Result<std::vector<Set>> load(Game game) override;
|
||||
Result<void> save(Game game, const std::vector<Set>& sets) override;
|
||||
|
||||
private:
|
||||
IFileSystem& fs_;
|
||||
ConfigService& config_;
|
||||
DirNameFn dirName_;
|
||||
|
||||
[[nodiscard]] std::filesystem::path setsPath(Game game) const;
|
||||
};
|
||||
|
||||
} // namespace ccm
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
// LocalImageStore: stores card images under
|
||||
// `<dataStorage>/<game>/images/<filename>`.
|
||||
// Implements IImageStore, used by ImageService.
|
||||
|
||||
#include "ccm/games/IGameModule.hpp"
|
||||
#include "ccm/ports/IFileSystem.hpp"
|
||||
#include "ccm/ports/IImageStore.hpp"
|
||||
#include "ccm/services/ConfigService.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
namespace ccm {
|
||||
|
||||
class LocalImageStore final : public IImageStore {
|
||||
public:
|
||||
using DirNameFn = std::function<std::string(Game)>;
|
||||
|
||||
LocalImageStore(IFileSystem& fs, ConfigService& config, DirNameFn dirName);
|
||||
|
||||
Result<std::string> copyIn(Game game,
|
||||
const std::filesystem::path& srcPath,
|
||||
const std::string& targetName) override;
|
||||
Result<void> remove(Game game, const std::string& imageName) override;
|
||||
std::filesystem::path resolvePath(Game game, const std::string& imageName) const override;
|
||||
|
||||
private:
|
||||
IFileSystem& fs_;
|
||||
ConfigService& config_;
|
||||
DirNameFn dirName_;
|
||||
|
||||
[[nodiscard]] std::filesystem::path gameImageDir(Game game) const;
|
||||
};
|
||||
|
||||
} // namespace ccm
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
// std::filesystem-backed implementation of IFileSystem.
|
||||
|
||||
#include "ccm/ports/IFileSystem.hpp"
|
||||
|
||||
namespace ccm {
|
||||
|
||||
class StdFileSystem final : public IFileSystem {
|
||||
public:
|
||||
[[nodiscard]] bool exists(const std::filesystem::path& p) const override;
|
||||
[[nodiscard]] bool isDirectory(const std::filesystem::path& p) const override;
|
||||
|
||||
Result<void> ensureDirectory(const std::filesystem::path& p) override;
|
||||
Result<std::string> readText(const std::filesystem::path& p) override;
|
||||
Result<void> writeText(const std::filesystem::path& p, std::string_view contents) override;
|
||||
Result<void> copyFile(const std::filesystem::path& from,
|
||||
const std::filesystem::path& to,
|
||||
bool overwrite) override;
|
||||
Result<void> remove(const std::filesystem::path& p) override;
|
||||
Result<std::vector<std::filesystem::path>> listDirectory(
|
||||
const std::filesystem::path& p) override;
|
||||
};
|
||||
|
||||
} // namespace ccm
|
||||
Reference in New Issue
Block a user