Mantis App v0.2.8
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, const ContentReader& reader, Context& ctx);
44 virtual void updateRecord(const Request& req, Response& res, const ContentReader& reader, 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 { return json::array(); }; // Remove
100 json list_records(const json& opts);
101
102 // Helper methods
103 static std::string generateTableId(const std::string& tablename);
104 std::string getColTypeFromName(const std::string& col, const std::vector<json>& fields) const;
105 json parseDbRowToJson(const soci::row& row) const;
106 json parseDbRowToJson(const soci::row& row, const std::vector<json>& ref_fields) const;
107
115 json getValueFromType(const std::string& type, const std::string& value);
116
117
126 std::optional<json> bindEntityToSociValue(soci::values& vals, const json& entity) const;
127
128 std::optional<std::string> validateRequestBody(const json& body) const;
129 std::optional<std::string> validateUpdateRequestBody(const json& body) const;
130
131 bool recordExists(const std::string& id) const;
132 std::optional<json> findFieldByKey(const std::string& key) const;
133 json checkValueInColumns(const std::string& value, const std::vector<std::string>& columns) const;
134
135 // Validators ...
136 static std::pair<bool, std::string> minimumConstraintCheck(const json& field, const json& entity);
137 static std::pair<bool, std::string> maximumConstraintCheck(const json& field, const json& entity);
138 static std::pair<bool, std::string> requiredConstraintCheck(const json& field, const json& entity);
139 static std::pair<bool, std::string> validatorConstraintCheck(const json& field, const json& entity);
140 static std::pair<bool, std::string> viewTypeSQLCheck(const json& entity);
141
142 static std::optional<json> validateTableSchema(const json& entity);
143
144
145 const std::string __class_name__ = "TableUnit";
146 protected:
147 std::string m_tableName;
148 std::string m_tableId;
149 std::string m_tableType;
150 std::string m_routeName;
151 bool m_isSystem = false;
152 std::vector<json> m_fields = {};
153
154 // Store the rules cached
160 };
161}
162
163#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.h:99
Rule addRule()
Definition tables.cpp:129
std::string tableName()
Definition tables.cpp:63
static std::pair< bool, std::string > requiredConstraintCheck(const json &field, const json &entity)
Definition tables_validation.cpp:174
json parseDbRowToJson(const soci::row &row) const
Definition tables_utils.cpp:66
Rule updateRule()
Definition tables.cpp:139
virtual void fetchRecords(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:229
Rule m_getRule
Definition tables.h:156
const std::string __class_name__
Definition tables.h:145
static std::pair< bool, std::string > minimumConstraintCheck(const json &field, const json &entity)
Definition tables_validation.cpp:112
static std::pair< bool, std::string > validatorConstraintCheck(const json &field, const json &entity)
Definition tables_validation.cpp:188
std::string getColTypeFromName(const std::string &col, const std::vector< json > &fields) const
Definition tables_utils.cpp:347
virtual bool setupRoutes()
Definition tables_routes.cpp:18
json checkValueInColumns(const std::string &value, const std::vector< std::string > &columns) const
Definition tables_utils.cpp:28
json update(const std::string &id, const json &entity, const json &opts) override
Definition tables_crud.cpp:166
void setTableName(const std::string &name)
Definition tables.cpp:68
Rule m_deleteRule
Definition tables.h:159
void setDeleteRule(const Rule &rule)
Definition tables.cpp:154
static bool getAuthToken(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:158
std::string m_routeName
Definition tables.h:150
Rule m_updateRule
Definition tables.h:158
Rule listRule()
Definition tables.cpp:109
std::optional< std::string > validateUpdateRequestBody(const json &body) const
Definition tables_validation.cpp:60
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:342
std::vector< json > m_fields
Definition tables.h:152
std::string m_tableId
Definition tables.h:148
static std::optional< json > validateTableSchema(const json &entity)
Definition tables_validation.cpp:229
void setAddRule(const Rule &rule)
Definition tables.cpp:134
void setIsSystemTable(bool isSystemTable)
Definition tables.cpp:104
json getValueFromType(const std::string &type, const std::string &value)
Definition tables_utils.cpp:171
std::string m_tableName
Definition tables.h:147
bool m_isSystem
Definition tables.h:151
std::string tableId()
Definition tables.cpp:73
virtual void deleteRecord(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:992
bool recordExists(const std::string &id) const
Definition tables_utils.cpp:358
std::string tableType()
Definition tables.cpp:83
virtual void fetchRecord(const Request &req, Response &res, Context &ctx)
Definition tables_routes.cpp:163
void setGetRule(const Rule &rule)
Definition tables.cpp:124
virtual bool hasAccess(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:181
json create(const json &entity, const json &opts) override
Definition tables_crud.cpp:18
Rule m_listRule
Definition tables.h:155
void setFields(const std::vector< json > &fields)
Definition tables.cpp:93
static std::pair< bool, std::string > maximumConstraintCheck(const json &field, const json &entity)
Definition tables_validation.cpp:143
virtual void createRecord(const Request &req, Response &res, const ContentReader &reader, Context &ctx)
Definition tables_routes.cpp:299
static std::pair< bool, std::string > viewTypeSQLCheck(const json &entity)
Definition tables_validation.cpp:215
void setUpdateRule(const Rule &rule)
Definition tables.cpp:144
Rule m_addRule
Definition tables.h:157
std::optional< std::string > validateRequestBody(const json &body) const
Definition tables_validation.cpp:14
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:149
std::optional< json > bindEntityToSociValue(soci::values &vals, const json &entity) const
Helper function to bind json values to soci::values for use in soci::use(...) when executing SQL stat...
Definition tables_utils.cpp:201
std::optional< json > read(const std::string &id, const json &opts) override
Definition tables_crud.cpp:143
Rule deleteRule()
Definition tables.cpp:149
virtual void authWithEmailAndPassword(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:15
virtual void resetPassword(const Request &req, Response &res, Context &ctx)
Definition tables_auth.cpp:147
bool isSystem() const
Definition tables.cpp:99
std::optional< json > findFieldByKey(const std::string &key) const
Definition tables_utils.cpp:16
virtual void updateRecord(const Request &req, Response &res, const ContentReader &reader, Context &ctx)
Definition tables_routes.cpp:634
json list_records(const json &opts)
Definition tables_crud.cpp:432
void setTableId(const std::string &id)
Definition tables.cpp:78
bool remove(const std::string &id, const json &opts) override
Definition tables_crud.cpp:373
nlohmann::json json
Definition mantis.h:35
router.h
Definition app.h:31
nlohmann::json json
Shorten JSON namespace.
Definition crud.h:14
std::string Rule
Definition models.h:100
httplib::ContentReader ContentReader
‍Shorthand for httplib::ContentReader
Definition http.h:124
httplib::Response Response
‍Shorthand for httplib::Response
Definition http.h:121
httplib::Request Request
‍Shorthand for httplib::Request
Definition http.h:118