-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model and further serialization. Added virtual destructors for interf…
…aces.
- Loading branch information
1 parent
f21c487
commit 93aa3c6
Showing
16 changed files
with
203 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "ModelInterface.h" | ||
|
||
ModelInterface::~ModelInterface() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <fstream> | ||
#include "Helpers.h" | ||
|
||
using namespace std; | ||
|
||
//---Model--- | ||
//---------------- | ||
// The contents of an entity are serialized as a structure with static components. | ||
// Writing directly objects to files can yield great havoc afterwards as we are possibly writing addresses, not values. | ||
// The model detects what to do with data when writing to file from the state of the attributes. | ||
class ModelInterface { | ||
private: | ||
virtual void openIOStream() = 0; | ||
protected: | ||
map<string, string> attributes; | ||
vector<string> protectedAttributes; | ||
fstream io; | ||
public: | ||
virtual void save() = 0; | ||
virtual void setAttributes(map<string, string> &) = 0; | ||
|
||
virtual ~ModelInterface(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include "RepositoryInterface.h" | ||
|
||
RepositoryInterface::~RepositoryInterface() | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,29 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <map> | ||
|
||
#include "Helpers.h" | ||
#include "ModelInterface.h" | ||
|
||
using namespace std; | ||
|
||
//---Entity--- | ||
//------------ | ||
// This entity acts like a repository interface between the accessor model and the actual model. | ||
//---Repository--- | ||
//---------------- | ||
// This entity acts like a glue between the accessor model and the actual model. | ||
// An example would be if we want to get all the records for a certain model. | ||
// The controller sends the request, the accessor model determines which model is needed and through | ||
// the repostory calls the methods responsible for retrieveing the data. | ||
// the repository it calls the methods responsible for retrieving your data. | ||
class RepositoryInterface { | ||
private: | ||
virtual void defineValidation() = 0; | ||
virtual void receiveCleanInput(map<string, string> &) = 0; | ||
protected: | ||
map<string, string> ValidationRules; | ||
map<string, string> ValidationErrors; | ||
virtual void receiveCleanInput(map<string, string> &) = 0; | ||
|
||
ModelInterface *model; | ||
public: | ||
virtual void writeNewRecord() = 0; | ||
virtual void validateItems(map<string, string> &) = 0; | ||
|
||
virtual void retrieveItemForActive() = 0; | ||
virtual void retrieveAll() = 0; | ||
|
||
virtual ~RepositoryInterface(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "UserModel.h" | ||
|
||
void UserModel::save() | ||
{ | ||
//this->io.write(reinterpret_cast<char *>(&this->User), sizeof(this->User)); | ||
|
||
cout << "Theoretically wrote the data into the file" | ||
<< endl; | ||
} | ||
|
||
void UserModel::setAttributes(map<string, string> &cleanInputs) | ||
{ | ||
for (map<string, string>::iterator it = cleanInputs.begin(); it != cleanInputs.end(); it++) | ||
{ | ||
if (isInVector(this->protectedAttributes, it->first)) | ||
{ | ||
continue; | ||
} | ||
else if (it->first == "fullname") | ||
{ | ||
strcpy(this->User.full_name, it->second.c_str()); | ||
} | ||
else if (it->first == "email") | ||
{ | ||
strcpy(this->User.email, it->second.c_str()); | ||
} | ||
else if (it->first == "username") | ||
{ | ||
strcpy(this->User.username, it->second.c_str()); | ||
} | ||
else if (it->first == "password") | ||
{ | ||
strcpy(this->User.password, it->second.c_str()); | ||
} | ||
} | ||
|
||
// If there's a new user, assign created_at with the current date. | ||
if (strlen(this->User.full_name) != 0) | ||
{ | ||
time_t t = time(nullptr); | ||
strftime(this->User.created_at, sizeof(this->User.created_at), "%c", localtime(&t)); | ||
} | ||
} | ||
|
||
UserModel::~UserModel() | ||
{ | ||
this->io.close(); | ||
} | ||
|
||
string UserModel::pathToFile = "..\\database\\users.store"; | ||
void UserModel::openIOStream() | ||
{ | ||
this->io.open(UserModel::pathToFile, ios::in | ios::out | ios::app | ios::binary); | ||
|
||
if (!this->io.is_open()) | ||
{ | ||
cout << "Couldn't open " + UserModel::pathToFile; | ||
} | ||
} | ||
|
||
UserModel::UserModel() | ||
{ | ||
this->openIOStream(); | ||
this->protectedAttributes = { "created_at", "deleted_at", "role", "active" }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
#include <regex> | ||
#include <iomanip> | ||
#include <ctime> | ||
#include <locale> | ||
|
||
#include "ModelInterface.h" | ||
|
||
class UserModel : public ModelInterface { | ||
private: | ||
static string pathToFile; | ||
|
||
struct { | ||
char full_name[255] = ""; | ||
char email[255] = ""; | ||
char username[20] = ""; | ||
char password[17] = ""; | ||
char created_at[50] = ""; | ||
|
||
// Soft deletes | ||
char deleted_at[50] = ""; | ||
char role[10] = "user"; | ||
bool active = 1; | ||
} User; | ||
|
||
void openIOStream(); | ||
public: | ||
// Open the IOStream and assign the protected attributes that shouldn't be changed by the user | ||
UserModel(); | ||
|
||
void save(); | ||
void setAttributes(map<string, string> &); | ||
|
||
// Set the active to false on logout | ||
~UserModel(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.