Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkModel.h
Go to the documentation of this file.
1
18#pragma once
19#include <nlohmann/json.hpp>
20using json = nlohmann::json;
21
22#include <string>
23#include <vector>
24#include <optional>
25#include "storage/JsonService.h"
28
37{
38public:
45 FrameworkModel(const std::string &path);
46
52 bool load();
53
59 bool save();
60
64 std::vector<nlohmann::json> all() const;
65
72 std::optional<nlohmann::json> find(const std::string &id) const;
73
80 bool create(const nlohmann::json &item);
81
89 bool update(const std::string &id, const nlohmann::json &updatedItem);
90
97 bool remove(const std::string &id);
98
100 nlohmann::json toJson() const
101 {
102 return nlohmann::json(collection);
103 }
104
106 nlohmann::json findAsJson(const std::string &id) const;
107
109 bool save(const std::string &id, const json &data);
110
111 bool createFromJson(const nlohmann::json &obj);
112
113 bool updateFromJson(const std::string &id, const nlohmann::json &updates);
114
115 nlohmann::json deleteAsJson(const std::string &id);
116
117 bool saveAll();
118
128 template <typename T>
129 T getValue(const std::string &key, const T &defaultValue = T())
130 {
132 if(!jsonService)
133 {
134 printf("[FrameworkModel] JsonService not available\n");
135 return defaultValue;
136 }
137 const auto &data = jsonService->data();
138 if (data.contains(key))
139 {
140 return data[key].get<T>();
141 }
142 return defaultValue;
143 }
144
153 template <typename T>
154 void setValue(const std::string &key, const T &value)
155 {
157 if(!jsonService)
158 {
159 printf("[FrameworkModel] JsonService not available\n");
160 return;
161 }
162 jsonService->data()[key] = value;
163 }
164
165
166protected:
172 virtual std::string getIdField() const { return "id"; }
173
174 nlohmann::json collection = nlohmann::json::array();
175
176private:
178 std::string storagePath;
179};
nlohmann::json json
Lightweight JSON wrapper for persistent config management using StorageManager.
Abstract interface for file and directory storage backends.
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
Provides a basic JSON-backed record model.
bool load()
Loads the JSON collection from storage.
bool createFromJson(const nlohmann::json &obj)
nlohmann::json deleteAsJson(const std::string &id)
bool updateFromJson(const std::string &id, const nlohmann::json &updates)
nlohmann::json toJson() const
Returns the full collection as a JSON array object.
virtual std::string getIdField() const
Returns the JSON key used as the record ID.
std::optional< nlohmann::json > find(const std::string &id) const
Finds an item by ID.
nlohmann::json collection
In-memory array of records.
JsonService * jsonService
Underlying JSON persistence layer
bool create(const nlohmann::json &item)
Adds a new item to the collection.
bool update(const std::string &id, const nlohmann::json &updatedItem)
Updates an item by ID.
nlohmann::json findAsJson(const std::string &id) const
Finds a single item by ID and returns it as JSON or null.
T getValue(const std::string &key, const T &defaultValue=T())
Reads a single top-level key from the model file.
bool save()
Saves the current collection to storage.
void setValue(const std::string &key, const T &value)
Sets a single top-level key in the model file.
std::string storagePath
File path for this model.
std::vector< nlohmann::json > all() const
Returns all items in the collection.
bool remove(const std::string &id)
Removes an item by ID.
Manages loading and saving of a single JSON document using StorageManager.
Definition JsonService.h:28
nlohmann::json & data()
Access the internal JSON object.