Cleaned up Resource and User.

This commit is contained in:
Relintai 2021-08-22 22:37:53 +02:00
parent 58e3855a4d
commit 4546d583d1
4 changed files with 1 additions and 210 deletions

View File

@ -1,101 +1,5 @@
#include "resource.h" #include "resource.h"
#include <vector>
#include "rapidjson/rapidjson.h"
#include <tinydir/tinydir.h>
#include <cstdio>
#if DATABASES_ENABLED
#include "core/database/database.h"
#include "core/database/database_manager.h"
#endif
void Resource::changed() {
dirty = true;
}
void Resource::save() {
#if DATABASES_ENABLED
sql_save();
#else
file_save();
#endif
}
void Resource::load() {
#if DATABASES_ENABLED
sql_load();
#else
file_load();
#endif
}
void Resource::migrate() {
#if DATABASES_ENABLED
//todo!
sql_delete_tables();
sql_create_tables();
//sql_migrate();
#else
file_ensure_directory_exist();
#endif
}
#if DATABASES_ENABLED
void Resource::sql_save() {
sql_save(DatabaseManager::get_singleton()->ddb);
}
void Resource::sql_load() {
sql_load(DatabaseManager::get_singleton()->ddb);
}
void Resource::sql_migrate() {
sql_migrate(DatabaseManager::get_singleton()->ddb);
}
void Resource::sql_create_tables() {
sql_create_tables(DatabaseManager::get_singleton()->ddb);
}
void Resource::sql_delete_tables() {
sql_delete_tables(DatabaseManager::get_singleton()->ddb);
}
void Resource::sql_save(Database *db) {
}
void Resource::sql_load(Database *db) {
}
void Resource::sql_migrate(Database *db) {
}
void Resource::sql_create_tables(Database *db) {
}
void Resource::sql_delete_tables(Database *db) {
}
#endif
void Resource::file_save() {
/*
//todo sanitize name!
_file_path = _resource_name + get_id();
FILE *fp = fopen(_file_path.c_str(), "w");
char writeBuffer[65536];
rapidjson::FileWriteStream os(fp, writeBuffer, sizeof(writeBuffer));
rapidjson::Writer<rapidjson::FileWriteStream> writer(os);
document.Accept(writer);
fclose(fp);
*/
}
void Resource::file_load() {
}
void Resource::file_ensure_directory_exist() {
}
std::string Resource::file_get_base_path() {
return "./resources/";
}
std::string Resource::to_json(rapidjson::Document *into) { std::string Resource::to_json(rapidjson::Document *into) {
return ""; return "";
} }
@ -105,8 +9,6 @@ void Resource::from_json(const std::string &data) {
Resource::Resource() : Resource::Resource() :
Reference() { Reference() {
id = 0; id = 0;
dirty = false;
resource_name = get_class();
} }
Resource::~Resource() { Resource::~Resource() {

View File

@ -1,12 +1,9 @@
#ifndef RESOURCE_H #ifndef RESOURCE_H
#define RESOURCE_H #define RESOURCE_H
#include <functional>
#include <map>
#include <string> #include <string>
#include "reference.h" #include "reference.h"
#include "rapidjson/document.h" #include "rapidjson/document.h"
#if DATABASES_ENABLED #if DATABASES_ENABLED
@ -18,33 +15,6 @@ class Resource : public Reference {
public: public:
int id; int id;
bool dirty;
std::string resource_name;
void changed();
virtual void save();
virtual void load();
virtual void migrate();
#if DATABASES_ENABLED
virtual void sql_save();
virtual void sql_load();
virtual void sql_migrate();
virtual void sql_create_tables();
virtual void sql_delete_tables();
virtual void sql_save(Database *db);
virtual void sql_load(Database *db);
virtual void sql_migrate(Database *db);
virtual void sql_create_tables(Database *db);
virtual void sql_delete_tables(Database *db);
#endif
virtual void file_save();
virtual void file_load();
virtual void file_ensure_directory_exist();
virtual std::string file_get_base_path();
virtual std::string to_json(rapidjson::Document *into = nullptr); virtual std::string to_json(rapidjson::Document *into = nullptr);
virtual void from_json(const std::string &data); virtual void from_json(const std::string &data);

View File

@ -22,79 +22,6 @@
#include "core/utils.h" #include "core/utils.h"
#include "user_model.h" #include "user_model.h"
void User::file_save() {
}
void User::file_load() {
FILE *f = fopen(_file_path.c_str(), "r");
if (!f) {
printf("FileBasedUser::load: Error opening file! %s\n", _file_path.c_str());
return;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); // same as rewind(f);
std::string fd;
fd.resize(fsize);
fread(&fd[0], 1, fsize, f);
fclose(f);
from_json(fd);
}
void User::file_ensure_directory_exist() {
}
std::string User::file_get_base_path() {
return _path;
}
void User::file_load_all() {
/*
tinydir_dir dir;
if (tinydir_open(&dir, _path.c_str()) == -1) {
return;
}
while (dir.has_next) {
tinydir_file file;
if (tinydir_readfile(&dir, &file) == -1) {
tinydir_next(&dir);
continue;
}
if (!file.is_dir) {
std::string np = file.path;
np = np.substr(_path.size(), np.size() - _path.size());
User *u = new User();
u->load(np);
UserManager::get_singleton()->add_user(u);
}
tinydir_next(&dir);
}
tinydir_close(&dir);
*/
}
std::string User::file_get_path() {
return _path;
}
void User::file_set_path(const std::string &path) {
_path = path;
if (_path.size() > 0) {
if (_path[_path.size() - 1] != '/') {
_path += '/';
}
}
}
std::string User::to_json(rapidjson::Document *into) { std::string User::to_json(rapidjson::Document *into) {
rapidjson::Document *document; rapidjson::Document *document;
@ -132,6 +59,7 @@ std::string User::to_json(rapidjson::Document *into) {
return s; return s;
} }
void User::from_json(const std::string &p_data) { void User::from_json(const std::string &p_data) {
rapidjson::Document data; rapidjson::Document data;
@ -162,6 +90,3 @@ User::User() :
User::~User() { User::~User() {
} }
std::string User::_path = "./";
std::string User::_table_name = "users";

View File

@ -36,12 +36,6 @@ public:
User(); User();
~User(); ~User();
protected:
std::string _file_path;
static std::string _path;
static std::string _table_name;
}; };
#endif #endif