mirror of
https://github.com/allankoechke/pocketbase-cpp.git
synced 2026-12-03 09:23:43 +00:00
AdminService structure, CollectionService/Model logic
This commit is contained in:
@@ -21,16 +21,15 @@ add_executable(pocketbase-cpp
|
||||
sdk/services/crudservice.h sdk/services/crudservice.cpp
|
||||
sdk/services/baseservice.h sdk/services/clientresponseerror.h
|
||||
sdk/services/baseauthstore.h sdk/services/baseauthstore.cpp
|
||||
sdk/services/collectionservice.h sdk/services/collectionservice.cpp
|
||||
sdk/services/adminservice.h sdk/services/adminservice.cpp
|
||||
sdk/dtos/basemodel.h sdk/dtos/basemodel.cpp
|
||||
sdk/dtos/listresult.h sdk/dtos/listresult.cpp
|
||||
sdk/dtos/recordmodel.h
|
||||
sdk/dtos/recordmodel.h sdk/dtos/collectionmodel.h
|
||||
sdk/dtos/AdminModel.h
|
||||
|
||||
# sdk/adminservice.h sdk/adminservice.cpp
|
||||
# sdk/collectionservice.h sdk/collectionservice.cpp
|
||||
# sdk/localauthstore.h sdk/localauthstore.cpp
|
||||
# sdk/models/utils/schemafield.h sdk/models/utils/schemafield.cpp
|
||||
# sdk/models/AdminModel.h
|
||||
# sdk/models/collectionmodel.hpp
|
||||
)
|
||||
target_link_libraries(pocketbase-cpp Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Network)
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "services/recordservice.h"
|
||||
#include "services/baseauthstore.h"
|
||||
#include "services/clientresponseerror.h"
|
||||
#include "services/collectionservice.h"
|
||||
|
||||
PocketBase::PocketBase(const QString& baseUrl, const QString& lang, int timeout, QObject* parent)
|
||||
: QObject(parent),
|
||||
@@ -9,7 +10,8 @@ PocketBase::PocketBase(const QString& baseUrl, const QString& lang, int timeout,
|
||||
m_lang(lang),
|
||||
m_timeout(timeout),
|
||||
m_networkManager(new QNetworkAccessManager(this)),
|
||||
m_authStore(new BaseAuthStore("", nullptr, this)) {}
|
||||
m_authStore(new BaseAuthStore("", nullptr, this)),
|
||||
m_collectionService(new CollectionService(this, this)) {}
|
||||
|
||||
RecordService* PocketBase::collection(const QString& idOrName) {
|
||||
if (!m_recordServices.contains(idOrName)) {
|
||||
@@ -19,29 +21,6 @@ RecordService* PocketBase::collection(const QString& idOrName) {
|
||||
return m_recordServices[idOrName];
|
||||
}
|
||||
|
||||
QString PocketBase::filter(const QString &expr, const QMap<QString, QVariant> &query) {
|
||||
if (query.isEmpty()) {
|
||||
return expr;
|
||||
}
|
||||
|
||||
for( const auto &key : query.keys()) {
|
||||
auto value = query.value( key );
|
||||
|
||||
// if ( value.isNull() || value is num || value is bool) {
|
||||
// value = value.toString();
|
||||
// } else if (value is DateTime) {
|
||||
// value = "'${value.toUtc().toIso8601String().replaceFirst("T", " ")}'";
|
||||
// } else if (value is String) {
|
||||
// value = "'${value.replaceAll("'", "\\'")}'";
|
||||
// } else {
|
||||
// value = "'${jsonEncode(value).replaceAll("'", "\\'")}'";
|
||||
// }
|
||||
// expr = expr.replaceAll("{:$key}", value.toString());
|
||||
}
|
||||
|
||||
return expr;
|
||||
}
|
||||
|
||||
QUrl PocketBase::buildUrl(const QString& path) {
|
||||
QUrl url(m_baseurl);
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
class RecordService;
|
||||
class BaseAuthStore;
|
||||
class CollectionService;
|
||||
|
||||
class PocketBase : public QObject
|
||||
{
|
||||
@@ -31,7 +32,6 @@ public:
|
||||
// BaseAuthStore* authStore() { return m_authStore; }
|
||||
QJsonObject send(const QString& path, const QJsonObject params);
|
||||
|
||||
QString filter(const QString& expr, const QMap<QString, QVariant>& query);
|
||||
QString getFileToken();
|
||||
QUrl buildUrl(const QString& path);
|
||||
|
||||
@@ -54,6 +54,7 @@ private:
|
||||
|
||||
QNetworkAccessManager* m_networkManager;
|
||||
BaseAuthStore* m_authStore;
|
||||
CollectionService* m_collectionService;
|
||||
|
||||
QMap<QString, RecordService*> m_recordServices;
|
||||
};
|
||||
|
||||
@@ -2,40 +2,42 @@
|
||||
#define ADMINMODEL_H
|
||||
|
||||
#include <QString>
|
||||
#include "utils/basemodel.h"
|
||||
|
||||
#include "basemodel.h"
|
||||
|
||||
class AdminModel : public BaseModel {
|
||||
public:
|
||||
AdminModel(const QJsonObject& data)
|
||||
: BaseModel(data), avatar(0), email("") {
|
||||
: BaseModel(data), m_avatar(0), m_email("") {
|
||||
load(data);
|
||||
}
|
||||
|
||||
int getAvatar() const {
|
||||
return avatar;
|
||||
return m_avatar;
|
||||
}
|
||||
|
||||
void setAvatar(int avatar) {
|
||||
this->avatar = avatar;
|
||||
this->m_avatar = avatar;
|
||||
}
|
||||
|
||||
QString getEmail() const {
|
||||
return email;
|
||||
return m_email;
|
||||
}
|
||||
|
||||
void setEmail(const QString& email) {
|
||||
this->email = email;
|
||||
this->m_email = email;
|
||||
}
|
||||
|
||||
void load(const QJsonObject& data) {
|
||||
BaseModel::load(data);
|
||||
avatar = data.value("avatar").toInt();
|
||||
email = data.value("email").toString();
|
||||
m_avatar = data.value("avatar").toInt();
|
||||
m_email = data.value("email").toString();
|
||||
m_data = data;
|
||||
}
|
||||
|
||||
private:
|
||||
int avatar;
|
||||
QString email;
|
||||
int m_avatar;
|
||||
QString m_email;
|
||||
};
|
||||
|
||||
|
||||
185
sdk/dtos/collectionmodel.h
Normal file
185
sdk/dtos/collectionmodel.h
Normal file
@@ -0,0 +1,185 @@
|
||||
#ifndef COLLECTIONMODEL_HPP
|
||||
#define COLLECTIONMODEL_HPP
|
||||
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
#include <QList>
|
||||
#include <QMap>
|
||||
|
||||
#include "basemodel.h"
|
||||
#include "schemafield.h"
|
||||
|
||||
class CollectionModel : public BaseModel {
|
||||
public:
|
||||
CollectionModel(const QJsonObject& data = QJsonObject());
|
||||
|
||||
QString getName() const;
|
||||
void setName(const QString& name);
|
||||
|
||||
QString getType() const;
|
||||
void setType(const QString& type);
|
||||
|
||||
QList<SchemaField> getSchema() const;
|
||||
void setSchema(const QList<SchemaField>& schema);
|
||||
|
||||
bool isSystem() const;
|
||||
void setSystem(bool system);
|
||||
|
||||
QString getListRule() const;
|
||||
void setListRule(const QString& listRule);
|
||||
|
||||
QString getViewRule() const;
|
||||
void setViewRule(const QString& viewRule);
|
||||
|
||||
QString getCreateRule() const;
|
||||
void setCreateRule(const QString& createRule);
|
||||
|
||||
QString getUpdateRule() const;
|
||||
void setUpdateRule(const QString& updateRule);
|
||||
|
||||
QString getDeleteRule() const;
|
||||
void setDeleteRule(const QString& deleteRule);
|
||||
|
||||
QMap<QString, QVariant> getOptions() const;
|
||||
void setOptions(const QMap<QString, QVariant>& options);
|
||||
|
||||
void load(const QJsonObject& data);
|
||||
|
||||
bool isBase() const;
|
||||
bool isAuth() const;
|
||||
bool isSingle() const;
|
||||
|
||||
private:
|
||||
QString name;
|
||||
QString type;
|
||||
QList<SchemaField> schema;
|
||||
bool system;
|
||||
QString listRule;
|
||||
QString viewRule;
|
||||
QString createRule;
|
||||
QString updateRule;
|
||||
QString deleteRule;
|
||||
QMap<QString, QVariant> options;
|
||||
};
|
||||
|
||||
CollectionModel::CollectionModel(const QJsonObject& data)
|
||||
: BaseModel(data), system(false) {
|
||||
load(data);
|
||||
}
|
||||
|
||||
QString CollectionModel::getName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
void CollectionModel::setName(const QString& name) {
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
QString CollectionModel::getType() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
void CollectionModel::setType(const QString& type) {
|
||||
this->type = type;
|
||||
}
|
||||
|
||||
QList<SchemaField> CollectionModel::getSchema() const {
|
||||
return schema;
|
||||
}
|
||||
|
||||
void CollectionModel::setSchema(const QList<SchemaField>& schema) {
|
||||
this->schema = schema;
|
||||
}
|
||||
|
||||
bool CollectionModel::isSystem() const {
|
||||
return system;
|
||||
}
|
||||
|
||||
void CollectionModel::setSystem(bool system) {
|
||||
this->system = system;
|
||||
}
|
||||
|
||||
QString CollectionModel::getListRule() const {
|
||||
return listRule;
|
||||
}
|
||||
|
||||
void CollectionModel::setListRule(const QString& listRule) {
|
||||
this->listRule = listRule;
|
||||
}
|
||||
|
||||
QString CollectionModel::getViewRule() const {
|
||||
return viewRule;
|
||||
}
|
||||
|
||||
void CollectionModel::setViewRule(const QString& viewRule) {
|
||||
this->viewRule = viewRule;
|
||||
}
|
||||
|
||||
QString CollectionModel::getCreateRule() const {
|
||||
return createRule;
|
||||
}
|
||||
|
||||
void CollectionModel::setCreateRule(const QString& createRule) {
|
||||
this->createRule = createRule;
|
||||
}
|
||||
|
||||
QString CollectionModel::getUpdateRule() const {
|
||||
return updateRule;
|
||||
}
|
||||
|
||||
void CollectionModel::setUpdateRule(const QString& updateRule) {
|
||||
this->updateRule = updateRule;
|
||||
}
|
||||
|
||||
QString CollectionModel::getDeleteRule() const {
|
||||
return deleteRule;
|
||||
}
|
||||
|
||||
void CollectionModel::setDeleteRule(const QString& deleteRule) {
|
||||
this->deleteRule = deleteRule;
|
||||
}
|
||||
|
||||
QMap<QString, QVariant> CollectionModel::getOptions() const {
|
||||
return options;
|
||||
}
|
||||
|
||||
void CollectionModel::setOptions(const QMap<QString, QVariant>& options) {
|
||||
this->options = options;
|
||||
}
|
||||
|
||||
void CollectionModel::load(const QJsonObject& data) {
|
||||
BaseModel::load(data);
|
||||
name = data.value("name").toString();
|
||||
type = data.value("type").toString("base");
|
||||
system = data.value("system").toBool(false);
|
||||
options = data.value("options").toObject().toVariantMap();
|
||||
|
||||
listRule = data.value("listRule").toString();
|
||||
viewRule = data.value("viewRule").toString();
|
||||
createRule = data.value("createRule").toString();
|
||||
updateRule = data.value("updateRule").toString();
|
||||
deleteRule = data.value("deleteRule").toString();
|
||||
|
||||
QJsonArray schemaArray = data.value("schema").toArray();
|
||||
schema.clear();
|
||||
for (const QJsonValue& fieldValue : schemaArray) {
|
||||
QJsonObject fieldObject = fieldValue.toObject();
|
||||
SchemaField field(fieldObject); // ---- TODO ---
|
||||
schema.append(field);
|
||||
}
|
||||
}
|
||||
|
||||
bool CollectionModel::isBase() const {
|
||||
return type == "base";
|
||||
}
|
||||
|
||||
bool CollectionModel::isAuth() const {
|
||||
return type == "auth";
|
||||
}
|
||||
|
||||
bool CollectionModel::isSingle() const {
|
||||
return type == "single";
|
||||
}
|
||||
|
||||
#endif // COLLECTIONMODEL_HPP
|
||||
111
sdk/services/adminservice.cpp
Normal file
111
sdk/services/adminservice.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
#include "adminservice.h"
|
||||
#include "baseauthstore.h"
|
||||
#include "../client.h"
|
||||
#include "../dtos/adminmodel.h"
|
||||
#include "../dtos/basemodel.h"
|
||||
|
||||
AdminAuthResponse::AdminAuthResponse(const QString& token, AdminModel* admin)
|
||||
: m_token(token), m_admin(admin) {}
|
||||
|
||||
QString AdminAuthResponse::getToken() const {
|
||||
return m_token;
|
||||
}
|
||||
|
||||
AdminModel* AdminAuthResponse::getAdmin() const {
|
||||
return m_admin;
|
||||
}
|
||||
|
||||
AdminService::AdminService(PocketBase* client, QObject* parent)
|
||||
: CrudService(client, parent), client(client) {}
|
||||
|
||||
BaseModel* AdminService::decode(const QJsonObject& data) {
|
||||
return new AdminModel(data);
|
||||
}
|
||||
|
||||
QString AdminService::baseCrudPath() const {
|
||||
return "/api/admins";
|
||||
}
|
||||
|
||||
BaseModel* AdminService::update(const QString& id, const QJsonObject& params) {
|
||||
auto item = CrudService::update(id, params);
|
||||
|
||||
// Create a record model out of a basemodel
|
||||
auto record = new RecordModel(item);
|
||||
|
||||
try {
|
||||
if (client->authStore()->model()->collectionId.isNull() &&
|
||||
item->getId() == client->authStore()->model()->getId()) {
|
||||
client->authStore()->save(client->authStore()->token(), record);
|
||||
}
|
||||
} catch (...) {
|
||||
// Handle exceptions
|
||||
}
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
bool AdminService::deleteItem(const QString& id) {
|
||||
auto ok = CrudService::deleteOne(id);
|
||||
|
||||
try {
|
||||
if (ok && client->authStore()->model()->collectionId.isNull() &&
|
||||
id == client->authStore()->model()->getId()) {
|
||||
client->authStore()->clear();
|
||||
}
|
||||
} catch (...) {
|
||||
// Handle exceptions
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
AdminAuthResponse AdminService::authResponse(const QJsonObject& responseData) {
|
||||
QJsonObject adminData = responseData.value("admin").toObject();
|
||||
QString token = responseData.value("token").toString();
|
||||
auto admin = decode(adminData);
|
||||
if (!token.isEmpty() && admin) {
|
||||
// client->authStore()->save(token, admin);
|
||||
}
|
||||
return AdminAuthResponse(token, admin);
|
||||
}
|
||||
|
||||
QJsonObject AdminService::authWithPassword(const QString& email, const QString& password, const QJsonObject& params) {
|
||||
QJsonObject payload, body, auth;
|
||||
body.insert("identity", email);
|
||||
body.insert("password", password);
|
||||
params.insert("method", "POST");
|
||||
params.insert("params", queryParams);
|
||||
params.insert("body", body);
|
||||
auth.insert("Authorization", "");
|
||||
params.insert("headers", auth);
|
||||
|
||||
auto reply = client->send(baseCrudPath() + "/auth-with-password", params);
|
||||
|
||||
connect(reply, &QNetworkReply::finished, this, [&](){
|
||||
// QJsonObject responseData
|
||||
qDebug() << "[AdminService] Reply finished" << reply->readAll();
|
||||
});
|
||||
// return authResponse(responseData);
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
AdminAuthResponse AdminService::authRefresh(const QJsonObject& bodyParams, const QJsonObject& queryParams) {
|
||||
// QJsonObject responseData = client->send(baseCrudPath() + "/auth-refresh", { {"method", "POST"}, {"params", queryParams}, {"body", bodyParams} });
|
||||
return authResponse(QJsonObject{});
|
||||
}
|
||||
|
||||
bool AdminService::requestPasswordReset(const QString& email, const QJsonObject& bodyParams, const QJsonObject& queryParams) {
|
||||
QJsonObject params = bodyParams;
|
||||
params.insert("email", email);
|
||||
// client->send(baseCrudPath() + "/request-password-reset", { {"method", "POST"}, {"params", queryParams}, {"body", params} });
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AdminService::confirmPasswordReset(const QString& passwordResetToken, const QString& password, const QString& passwordConfirm, const QJsonObject& bodyParams, const QJsonObject& queryParams) {
|
||||
QJsonObject params = bodyParams;
|
||||
params.insert("token", passwordResetToken);
|
||||
params.insert("password", password);
|
||||
params.insert("passwordConfirm", passwordConfirm);
|
||||
// client->send(baseCrudPath() + "/confirm-password-reset", { {"method", "POST"}, {"params", queryParams}, {"body", params} });
|
||||
return true;
|
||||
}
|
||||
47
sdk/services/adminservice.h
Normal file
47
sdk/services/adminservice.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef ADMINSERVICE_H
|
||||
#define ADMINSERVICE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include "crudservice.h"
|
||||
|
||||
class PocketBase;
|
||||
class AdminModel;
|
||||
|
||||
class AdminAuthResponse {
|
||||
public:
|
||||
AdminAuthResponse(const QString& token, AdminModel* admin);
|
||||
|
||||
QString getToken() const;
|
||||
AdminModel* getAdmin() const;
|
||||
|
||||
private:
|
||||
QString m_token;
|
||||
AdminModel* m_admin;
|
||||
};
|
||||
|
||||
class AdminService : public CrudService {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit AdminService(PocketBase* client, QObject* parent = nullptr);
|
||||
|
||||
BaseModel* decode(const QJsonObject& data) override;
|
||||
QString baseCrudPath() const override;
|
||||
|
||||
BaseModel* update(const QString& id, const QJsonObject& params = QJsonObject());
|
||||
bool deleteItem(const QString& id);
|
||||
|
||||
AdminAuthResponse authResponse(const QJsonObject& responseData);
|
||||
QJsonObject authWithPassword(const QString& email, const QString& password, const QJsonObject& params = QJsonObject());
|
||||
AdminAuthResponse authRefresh(const QJsonObject& params = QJsonObject());
|
||||
bool confirmPasswordReset(const QString& passwordResetToken, const QString& password, const QString& passwordConfirm, const QJsonObject& params = QJsonObject());
|
||||
|
||||
private:
|
||||
PocketBase* client;
|
||||
};
|
||||
|
||||
|
||||
#endif // ADMINSERVICE_H
|
||||
28
sdk/services/collectionservice.cpp
Normal file
28
sdk/services/collectionservice.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include "collectionservice.h"
|
||||
#include "../client.h"
|
||||
|
||||
CollectionService::CollectionService(PocketBase* client, QObject *parent)
|
||||
: QObject{parent},
|
||||
CrudService(client, parent),
|
||||
client(client) {}
|
||||
|
||||
QString CollectionService::baseCrudPath() const {
|
||||
return "/api/collections";
|
||||
}
|
||||
|
||||
bool CollectionService::import(QJsonArray& collections, bool deleteMissing, QJsonObject headers, QJsonObject query) {
|
||||
QJsonObject payload, body;
|
||||
body["collections"] = collections;
|
||||
body["deleteMissing"] = deleteMissing;
|
||||
payload["method"] = "PUT";
|
||||
payload["body"] = body;
|
||||
payload["headers"] = headers;
|
||||
payload["query"] = query;
|
||||
|
||||
qDebug() << "[Import] Response: " << client->send(QString("%1/import").arg(baseCrudPath()), payload);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
BaseModel *CollectionService::decode(const QJsonObject &data)
|
||||
{ return new BaseModel(QJsonObject());} // We are not using it
|
||||
23
sdk/services/collectionservice.h
Normal file
23
sdk/services/collectionservice.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef COLLECTIONSERVICE_H
|
||||
#define COLLECTIONSERVICE_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
|
||||
#include "crudservice.h"
|
||||
|
||||
class CollectionService : public QObject, public CrudService
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CollectionService(PocketBase* client, QObject *parent = nullptr);
|
||||
|
||||
QString baseCrudPath() const override;
|
||||
bool import(QJsonArray& collections, bool deleteMissing = false,
|
||||
QJsonObject headers = {}, QJsonObject query = {} );
|
||||
BaseModel* decode(const QJsonObject& data) override;
|
||||
private:
|
||||
PocketBase* client;
|
||||
};
|
||||
|
||||
#endif // COLLECTIONSERVICE_H
|
||||
Reference in New Issue
Block a user