|
| 1 | +// PBRecords.cpp |
| 2 | +// Record CRUD methods, URL helpers, and file URL builder. |
| 3 | + |
| 4 | +#include "PocketbaseExtended.h" |
| 5 | + |
| 6 | +// --------------------------------------------------------------------------- |
| 7 | +// Private URL helpers |
| 8 | +// --------------------------------------------------------------------------- |
| 9 | + |
| 10 | +String PocketbaseExtended::_buildRecordsUrl(const char* recordId) { |
| 11 | + String url = _baseUrl + "collections/" + _collection + "/records"; |
| 12 | + if (recordId != nullptr && strlen(recordId) > 0) { |
| 13 | + url += "/"; |
| 14 | + url += recordId; |
| 15 | + } |
| 16 | + return url; |
| 17 | +} |
| 18 | + |
| 19 | +String PocketbaseExtended::_appendParam(const String& url, const char* key, const char* value) { |
| 20 | + if (value == nullptr || strlen(value) == 0) return url; |
| 21 | + String result = url; |
| 22 | + result += (result.indexOf('?') == -1) ? "?" : "&"; |
| 23 | + result += key; |
| 24 | + result += "="; |
| 25 | + result += value; |
| 26 | + return result; |
| 27 | +} |
| 28 | + |
| 29 | +// --------------------------------------------------------------------------- |
| 30 | +// File URL builder (no HTTP request) |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | + |
| 33 | +String PocketbaseExtended::getFileUrl(const char* recordId, |
| 34 | + const char* filename, |
| 35 | + const char* thumb) { |
| 36 | + String url = _baseUrl + "files/" + _collection + "/" + recordId + "/" + filename; |
| 37 | + url = _appendParam(url, "thumb", thumb); |
| 38 | + return url; |
| 39 | +} |
| 40 | + |
| 41 | +// --------------------------------------------------------------------------- |
| 42 | +// Extended record methods (return PBResponse) |
| 43 | +// --------------------------------------------------------------------------- |
| 44 | + |
| 45 | +PBResponse PocketbaseExtended::getOneEx(const char* recordId, |
| 46 | + const char* expand, |
| 47 | + const char* fields) { |
| 48 | + if (recordId == nullptr || strlen(recordId) == 0) { |
| 49 | + PBResponse r; r.ok = false; r.statusCode = 0; |
| 50 | + r.error = "recordId is required"; |
| 51 | + return r; |
| 52 | + } |
| 53 | + String url = _buildRecordsUrl(recordId); |
| 54 | + url = _appendParam(url, "expand", expand); |
| 55 | + url = _appendParam(url, "fields", fields); |
| 56 | + return _request("GET", url); |
| 57 | +} |
| 58 | + |
| 59 | +PBResponse PocketbaseExtended::getListEx(const char* page, |
| 60 | + const char* perPage, |
| 61 | + const char* sort, |
| 62 | + const char* filter, |
| 63 | + const char* skipTotal, |
| 64 | + const char* expand, |
| 65 | + const char* fields) { |
| 66 | + String url = _buildRecordsUrl(); |
| 67 | + url = _appendParam(url, "page", page); |
| 68 | + url = _appendParam(url, "perPage", perPage); |
| 69 | + url = _appendParam(url, "sort", sort); |
| 70 | + url = _appendParam(url, "filter", filter); |
| 71 | + url = _appendParam(url, "skipTotal", skipTotal); |
| 72 | + url = _appendParam(url, "expand", expand); |
| 73 | + url = _appendParam(url, "fields", fields); |
| 74 | + return _request("GET", url); |
| 75 | +} |
| 76 | + |
| 77 | +PBResponse PocketbaseExtended::createEx(const String& requestBody) { |
| 78 | + String url = _buildRecordsUrl(); |
| 79 | + return _request("POST", url, requestBody); |
| 80 | +} |
| 81 | + |
| 82 | +PBResponse PocketbaseExtended::updateEx(const char* recordId, const String& requestBody) { |
| 83 | + if (recordId == nullptr || strlen(recordId) == 0) { |
| 84 | + PBResponse r; r.ok = false; r.statusCode = 0; |
| 85 | + r.error = "recordId is required"; |
| 86 | + return r; |
| 87 | + } |
| 88 | + String url = _buildRecordsUrl(recordId); |
| 89 | + return _request("PATCH", url, requestBody); |
| 90 | +} |
| 91 | + |
| 92 | +PBResponse PocketbaseExtended::deleteRecordEx(const char* recordId) { |
| 93 | + if (recordId == nullptr || strlen(recordId) == 0) { |
| 94 | + PBResponse r; r.ok = false; r.statusCode = 0; |
| 95 | + r.error = "recordId is required"; |
| 96 | + return r; |
| 97 | + } |
| 98 | + String url = _buildRecordsUrl(recordId); |
| 99 | + return _request("DELETE", url); |
| 100 | +} |
| 101 | + |
| 102 | +// --------------------------------------------------------------------------- |
| 103 | +// Convenience wrappers (return body String for backward compatibility) |
| 104 | +// --------------------------------------------------------------------------- |
| 105 | + |
| 106 | +String PocketbaseExtended::getOne(const char* recordId, |
| 107 | + const char* expand, |
| 108 | + const char* fields) { |
| 109 | + return getOneEx(recordId, expand, fields).body; |
| 110 | +} |
| 111 | + |
| 112 | +String PocketbaseExtended::getList(const char* page, |
| 113 | + const char* perPage, |
| 114 | + const char* sort, |
| 115 | + const char* filter, |
| 116 | + const char* skipTotal, |
| 117 | + const char* expand, |
| 118 | + const char* fields) { |
| 119 | + return getListEx(page, perPage, sort, filter, skipTotal, expand, fields).body; |
| 120 | +} |
| 121 | + |
| 122 | +String PocketbaseExtended::create(const String& requestBody) { |
| 123 | + return createEx(requestBody).body; |
| 124 | +} |
| 125 | + |
| 126 | +String PocketbaseExtended::update(const char* recordId, const String& requestBody) { |
| 127 | + return updateEx(recordId, requestBody).body; |
| 128 | +} |
| 129 | + |
| 130 | +String PocketbaseExtended::deleteRecord(const char* recordId) { |
| 131 | + return deleteRecordEx(recordId).body; |
| 132 | +} |
0 commit comments