Mantis App v0.1.13
Loading...
Searching...
No Matches
tables.h
Go to the documentation of this file.
1//
2// Created by allan on 13/05/2025.
3//
4
5#ifndef TABLES_H
6#define TABLES_H
7
8#include <memory>
9#include <nlohmann/json.hpp>
10
11#include "../models/models.h"
12#include "../http.h"
13#include "../crud/crud.h"
14#include "../jwt.h"
15#include "../../app/app.h"
16
17namespace mantis
18{
19 using json = nlohmann::json;
20
30 class TableUnit : public CrudInterface<json>
31 {
32 public:
33 explicit TableUnit(std::string tableName,
34 std::string tableId,
35 std::string tableType = "base");
36 explicit TableUnit(const json& schema = json::object());
37
38 virtual ~TableUnit() override = default;
39
40 // CRUD endpoints handlers
41 virtual void fetchRecord(const Request& req, Response& res, Context& ctx);
42 virtual void fetchRecords(const Request& req, Response& res, Context& ctx);
43 virtual void createRecord(const Request& req, Response& res, Context& ctx);
44 virtual void updateRecord(const Request& req, Response& res, Context& ctx);
45 virtual void deleteRecord(const Request& req, Response& res, Context& ctx);
46
47 // Auth Routes Handlers
48 virtual void authWithEmailAndPassword(const Request& req, Response& res, Context& ctx);
49 virtual void resetPassword(const Request& req, Response& res, Context& ctx);
50
51 // Router setup
52 virtual bool setupRoutes();
53
54 // Override route display name
55 void setRouteDisplayName(const std::string& routeName);
56
57 // Middleware
58 static bool getAuthToken(const Request& req, Response& res, Context& ctx);
59 virtual bool hasAccess(const Request& req, Response& res, Context& ctx);
60
61 // Getters
62 std::string tableName();
63 void setTableName(const std::string& name);
64
65 std::string tableId();
66 void setTableId(const std::string& id);
67
68 std::string tableType();
69 void fromJson(const json& j);
70
71 std::vector<json> fields() const;
72 void setFields(const std::vector<json>& fields);
73
74 bool isSystem() const;
75 void setIsSystemTable(bool isSystemTable);
76
77 // Store the rules cached
78 Rule listRule();
79 void setListRule(const Rule& rule);
80
81 Rule getRule();
82 void setGetRule(const Rule& rule);
83
84 Rule addRule();
85 void setAddRule(const Rule& rule);
86
88 void setUpdateRule(const Rule& rule);
89
91 void setDeleteRule(const Rule& rule);
92
93 // CRUD endpoints
94 // Create/read/list/update/delete record(s), use opts to config optional params
95 json create(const json& entity, const json& opts) override;
96 std::optional<json> read(const std::string& id, const json& opts) override;
97 json update(const std::string& id, const json& entity, const json& opts) override;
98 bool remove(const std::string& id, const json& opts) override;
99 std::vector<json> list(const json& opts) override;
100
101 // Helper methods
102 static std::string generateTableId(const std::string& tablename);
103 std::string getColTypeFromName(const std::string& col, const std::vector<json>& fields) const;
104 json parseDbRowToJson(const soci::row& row) const;
105 std::optional<json> validateRequestBody(const json& body) const;
106 std::optional<json> validateUpdateRequestBody(const json& body) const;
107 bool recordExists(const std::string& id) const;
108 std::optional<json> findFieldByKey(const std::string& key) const;
109 json checkValueInColumns(const std::string& value, const std::vector<std::string>& columns) const;
110
111 const std::string __class_name__ = "TableUnit";
112 protected:
113 std::string m_tableName;
114 std::string m_tableId;
115 std::string m_tableType;
116 std::string m_routeName;
117 bool m_isSystem = false;
118 std::vector<json> m_fields = {};
119
120 // Store the rules cached
126 };
127}
128
129#endif //TABLES_H
Definition http.h:59
Definition crud.h:17
Class to model the behaviour of a table in database.
Definition tables.h:31
Rule getRule()
Definition tables.cpp:119
std::vector< json > list(const json &opts) override
Definition tables_crud.cpp:613
virtual void updateRecord(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:334
Rule addRule()
Definition tables.cpp:129
std::string tableName()
Definition tables.cpp:63
json parseDbRowToJson(const soci::row &row) const
Definition tables_utils.cpp:64
std::optional< json > validateRequestBody(const json &body) const
Definition tables_validation.cpp:14
Rule updateRule()
Definition tables.cpp:139
virtual void fetchRecords(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:224
Rule m_getRule
Definition tables.h:122
const std::string __class_name__
Definition tables.h:111
std::string getColTypeFromName(const std::string &col, const std::vector< json > &fields) const
Definition tables_utils.cpp:160
virtual bool setupRoutes()
Definition tables_routes.cpp:14
json checkValueInColumns(const std::string &value, const std::vector< std::string > &columns) const
Definition tables_utils.cpp:26
json update(const std::string &id, const json &entity, const json &opts) override
Definition tables_crud.cpp:315
void setTableName(const std::string &name)
Definition tables.cpp:68
Rule m_deleteRule
Definition tables.h:125
void setDeleteRule(const Rule &rule)
Definition tables.cpp:154
static bool getAuthToken(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:162
std::string m_routeName
Definition tables.h:116
Rule m_updateRule
Definition tables.h:124
Rule listRule()
Definition tables.cpp:109
std::vector< json > fields() const
Definition tables.cpp:88
void fromJson(const json &j)
Definition tables.cpp:32
virtual ~TableUnit() override=default
static std::string generateTableId(const std::string &tablename)
Definition tables_utils.cpp:155
std::vector< json > m_fields
Definition tables.h:118
std::string m_tableId
Definition tables.h:114
void setAddRule(const Rule &rule)
Definition tables.cpp:134
void setIsSystemTable(bool isSystemTable)
Definition tables.cpp:104
std::string m_tableName
Definition tables.h:113
bool m_isSystem
Definition tables.h:117
std::string tableId()
Definition tables.cpp:73
virtual void deleteRecord(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:433
bool recordExists(const std::string &id) const
Definition tables_utils.cpp:172
std::string tableType()
Definition tables.cpp:83
virtual void fetchRecord(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:158
void setGetRule(const Rule &rule)
Definition tables.cpp:124
virtual bool hasAccess(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:188
json create(const json &entity, const json &opts) override
Definition tables_crud.cpp:14
Rule m_listRule
Definition tables.h:121
void setFields(const std::vector< json > &fields)
Definition tables.cpp:93
void setUpdateRule(const Rule &rule)
Definition tables.cpp:144
virtual void createRecord(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:262
Rule m_addRule
Definition tables.h:123
void setListRule(const Rule &rule)
Definition tables.cpp:114
void setRouteDisplayName(const std::string &routeName)
Definition tables.cpp:55
std::string m_tableType
Definition tables.h:115
std::optional< json > read(const std::string &id, const json &opts) override
Definition tables_crud.cpp:292
Rule deleteRule()
Definition tables.cpp:149
virtual void authWithEmailAndPassword(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:14
virtual void resetPassword(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:153
bool isSystem() const
Definition tables.cpp:99
std::optional< json > validateUpdateRequestBody(const json &body) const
Definition tables_validation.cpp:164
std::optional< json > findFieldByKey(const std::string &key) const
Definition tables_utils.cpp:14
void setTableId(const std::string &id)
Definition tables.cpp:78
bool remove(const std::string &id, const json &opts) override
Definition tables_crud.cpp:586
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:30
nlohmann::json json
Shorten JSON namespace.
Definition crud.h:14
std::string Rule
Definition models.h:96
httplib::Response Response
‍Shorthand for httplib::Response
Definition http.h:121
httplib::Request Request
‍Shorthand for httplib::Request
Definition http.h:118