Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
FrameworkModel.cpp
Go to the documentation of this file.
1
21#include "framework_config.h"
22#include "DebugTrace.h"
24
25
26FrameworkModel::FrameworkModel(const std::string& path)
27 : storagePath(path) {}
28
31{
33 if(!jsonService)
34 {
35 printf("Failed to get JsonService\n");
36 return false;
37 }
38 if(storagePath.empty())
39 {
40 printf("[FrameworkModel] No storage path set, cannot load data\n");
41 return false;
42 }
43 TRACE("[FrameworkModel] Loading data from %s\n", storagePath.c_str());
45 return false;
46 collection = jsonService->data().value("items", nlohmann::json::array());
47 if (collection.empty())
48 {
49 TRACE("No items found in %s\n", storagePath.c_str());
50 return false;
51 }
52 else{
53 TRACE("collection: %s\n", collection.dump(4).c_str());
54 }
55 return true;
56}
57
65
67std::vector<nlohmann::json> FrameworkModel::all() const
68{
69 if (collection.empty())
70 {
71 return {};
72 }
73 std::vector<nlohmann::json> items;
74 for (const auto &item : collection)
75 {
76 TRACE("[FrameworkModel::all] Item: %s\n", item.dump(4).c_str());
77 items.push_back(item);
78 }
79 return items;
80}
81
83std::optional<nlohmann::json> FrameworkModel::find(const std::string &id) const
84{
85 std::string idField = getIdField();
86 for (const auto &item : collection)
87 {
88 if (item.contains(idField) && item[idField] == id)
89 {
90 return item;
91 }
92 }
93 return std::nullopt;
94}
95
97bool FrameworkModel::create(const nlohmann::json &item)
98{
99 std::string idField = getIdField();
100 if (!item.contains(idField))
101 return false;
102 std::string id = item[idField];
103 if (find(id))
104 return false;
105 collection.push_back(item);
106 return true;
107}
108
110bool FrameworkModel::update(const std::string &id, const nlohmann::json &updatedItem)
111{
112 std::string idField = getIdField();
113 for (auto &item : collection)
114 {
115 if (item.contains(idField) && item[idField] == id)
116 {
117 item = updatedItem;
118 return true;
119 }
120 }
121 return false;
122}
123
125bool FrameworkModel::remove(const std::string &id)
126{
127 std::string idField = getIdField();
128 for (auto it = collection.begin(); it != collection.end(); ++it)
129 {
130 if (it->contains(idField) && (*it)[idField] == id)
131 {
132 collection.erase(it);
133 return true;
134 }
135 }
136 return false;
137}
138
140nlohmann::json FrameworkModel::findAsJson(const std::string &id) const
141{
142 auto result = find(id);
143 return result ? *result : nlohmann::json(nullptr);
144}
145
147bool FrameworkModel::save(const std::string &id, const nlohmann::json &data)
148{
149 std::string idField = getIdField();
150
151 for (auto &item : collection)
152 {
153 if (item.contains(idField) && item[idField] == id)
154 {
155 item = data;
156 return save();
157 }
158 }
159
160 // If not found, append
161 collection.push_back(data);
162 return save();
163}
164
166bool FrameworkModel::createFromJson(const nlohmann::json &obj)
167{
168 std::string idField = getIdField();
169 if (!obj.contains(idField))
170 return false;
171 return save(obj[idField], obj);
172}
173
175bool FrameworkModel::updateFromJson(const std::string &id, const nlohmann::json &updates)
176{
177 std::string idField = getIdField();
178 for (auto &item : collection)
179 {
180 if (item.contains(idField) && item[idField] == id)
181 {
182 for (auto &el : updates.items())
183 {
184 item[el.key()] = el.value(); // Patch keys
185 }
186 return save(id, item);
187 }
188 }
189 return false;
190}
191
193nlohmann::json FrameworkModel::deleteAsJson(const std::string &id)
194{
195 std::string idField = getIdField();
196 for (auto it = collection.begin(); it != collection.end(); ++it)
197 {
198 if (it->contains(idField) && (*it)[idField] == id)
199 {
200 nlohmann::json removed = *it;
201 collection.erase(it);
202 return saveAll() ? removed : nullptr;
203 }
204 }
205 return nullptr;
206}
207
Macro-based debug trace system with optional SD file logging.
#define TRACE_INIT(MODULE_NAME)
Declare trace usage in a source file for a given module.
Definition DebugTrace.h:170
#define TRACE(...)
Default trace (INFO level).
Definition DebugTrace.h:187
Base model class for persistent JSON collections.
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)
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.
bool save()
Saves the current collection to storage.
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.
nlohmann::json & data()
Access the internal JSON object.
bool save(const std::string &path) const
Save the current JSON data to storage.
bool load(const std::string &path)
Load a JSON file from storage.
Delegates to user or system configuration.