Skip to content

Commit

Permalink
Working login module.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 26, 2016
1 parent 9184ba2 commit b8efd56
Show file tree
Hide file tree
Showing 13 changed files with 235 additions and 97 deletions.
14 changes: 4 additions & 10 deletions app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void Console::loadViews(const fs::path &viewsFolder)
void Console::loadActions()
{
// TODO: load actions via config file
vector<char> actions = { 'q', 'b', 'n', 'c' };
vector<char> actions = { 'q', 'b', 'n' };

this->actions = actions;
}
Expand All @@ -129,14 +129,11 @@ void Console::handleView()
buffer << viewFile.rdbuf();
this->currentView.setRawFormat(buffer.str());

vector<string> emptyErrorBag = {};
Controller::setErrorsBag(emptyErrorBag);

// If there's any validation error catch it and reload the view.
this->theController = Controller(this->currentView.getViewName(), buffer.str(), View::getViewExtension());
if (!Controller::getErrorsBag().empty()) {
string message = "\nPlease correct the following: ";
printVector(Controller::getErrorsBag(), message);
if (!Controller::getErrorBag().empty()) {
toast(string("There were some issues:"), string("error"));
printVector(Controller::getErrorBag());

sleepAndClearBuffer(3 * this->delay);
this->reloadView();
Expand Down Expand Up @@ -197,9 +194,6 @@ void Console::takeActionOrNext()
case 'n':
// TODO: implement pagination for the questions index view
break;
case 'c':
// TODO: implement confirmation action for input decisions
break;
default:
break;
}
Expand Down
35 changes: 22 additions & 13 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

//---Controller---
//----------------
string Controller::viewInputFormat = "@input-";
string Controller::viewOutputFormat = "@output-";
string Controller::userInputString = "@input-";
string Controller::outputString = "@output-";
string Controller::middlewareString = "@guard-";

vector<string> Controller::errorsBag = {};
vector<string> Controller::errorBag = {};

void Controller::justShow()
{
Expand Down Expand Up @@ -40,7 +41,7 @@ void Controller::prepareView()
}
else if (this->hasInput(copyChunk) || this->hasOutput(copyChunk))
{
if (copyChunk.find(Controller::viewInputFormat) < copyChunk.find(Controller::viewOutputFormat))
if (copyChunk.find(Controller::userInputString) < copyChunk.find(Controller::outputString))
{
this->chopChunkAndGetAlias(copyChunk);
}
Expand Down Expand Up @@ -77,14 +78,21 @@ void Controller::prepareViewInput(const string &subChunk, const string &inputAli
this->userInputs[inputAlias] = userInput;
}

void Controller::setErrorsBag(vector<string> &errorsBag)
void Controller::pushError(string &error)
{
Controller::errorsBag = errorsBag;
if (error == "")
{
Controller::errorBag = vector<string>({});
}
else
{
Controller::errorBag.push_back(error);
}
}

vector<string> Controller::getErrorsBag()
vector<string> Controller::getErrorBag()
{
return Controller::errorsBag;
return Controller::errorBag;
}

Controller::Controller()
Expand Down Expand Up @@ -123,10 +131,10 @@ char *Controller::getControllerName()
void Controller::chopChunkAndGetAlias(string &chunk)
{
// Splice the alias (take the part after the "-" in the template string).
string inputAlias = chunk.substr(chunk.find(Controller::viewInputFormat) + Controller::viewInputFormat.size(),
chunk.find("\n", chunk.find(Controller::viewInputFormat)) - (chunk.find(Controller::viewInputFormat) + Controller::viewInputFormat.size()));
string inputAlias = chunk.substr(chunk.find(Controller::userInputString) + Controller::userInputString.size(),
chunk.find("\n", chunk.find(Controller::userInputString)) - (chunk.find(Controller::userInputString) + Controller::userInputString.size()));

this->prepareViewInput(chunk.substr(0, chunk.find(Controller::viewInputFormat) - 1), inputAlias);
this->prepareViewInput(chunk.substr(0, chunk.find(Controller::userInputString) - 1), inputAlias);

chunk.erase(0, chunk.find(inputAlias) + inputAlias.size() + 2);
}
Expand All @@ -137,18 +145,19 @@ void Controller::operator=(const Controller &controller)
delete[] this->controllerName;
this->controllerName = new char[strlen(controller.controllerName) + 1];
strcpy(this->controllerName, controller.controllerName);

this->viewChunk = controller.viewChunk;
this->controllerAttributions = controller.controllerAttributions;
}

bool Controller::hasInput(const string &raw)
{
return raw.find(Controller::viewInputFormat) != string::npos;
return raw.find(Controller::userInputString) != string::npos;
}

bool Controller::hasOutput(const string &raw)
{
return raw.find(Controller::viewOutputFormat) != string::npos;
return raw.find(Controller::outputString) != string::npos;
}

Controller::~Controller()
Expand Down
12 changes: 7 additions & 5 deletions app/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ using namespace std;

class Controller {
private:
static string viewInputFormat;
static string viewOutputFormat;
static vector<string> errorsBag;
static string userInputString;
static string outputString;
static string middlewareString;

static vector<string> errorBag;

char *controllerName;
vector<string> controllerAttributions;
Expand All @@ -24,8 +26,8 @@ class Controller {
void prepareViewInput(const string &, const string &);
// TODO: take the chunk one by one for output and request output from model for each output variable
public:
static void setErrorsBag(vector<string> &);
static vector<string> getErrorsBag();
static void pushError(string &);
static vector<string> getErrorBag();

Controller();
Controller(char *, string &, string &);
Expand Down
27 changes: 21 additions & 6 deletions app/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,34 @@ inline bool isInMap(map<FirstT, SecondT> &haystack, FirstT needle)
}

template<typename V>
inline void printVector(vector<V> &v, string &message)
inline void printVector(vector<V> &v)
{
if (message != "")
for (vector<V>::iterator it = v.begin(); it != v.end(); it++)
{
cout << message
cout << (*it)
<< endl;
}
}

for (vector<V>::iterator it = v.begin(); it != v.end(); it++)
template<typename T>
inline void toast(T content, string &status)
{
cout << endl << endl;

if (status == "success")
{
cout << (*it)
<< endl;
cout << "++ " << content;
}
else if (status == "error")
{
cout << "!! " << content;
}
else
{
cout << "** " << content;
}

cout << endl;
}

void toLowerCase(string &);
Expand Down
8 changes: 0 additions & 8 deletions app/RepositoryInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include "Helpers.h"

#include "ModelInterface.h"

using namespace std;

//---Repository---
Expand All @@ -19,14 +17,8 @@ class RepositoryInterface {
protected:
map<string, string> ValidationRules;
map<string, string> ValidationErrors;

ModelInterface *model;

vector<string> errorBag;
public:
virtual void validateItems(map<string, string> &) = 0;
virtual void retrieveItemForActive() = 0;
virtual void retrieveAll() = 0;

virtual ~RepositoryInterface();
};
82 changes: 68 additions & 14 deletions app/UserModel.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,44 @@
#include "UserModel.h"

// Serialize the object when writing a new record.
void UserModel::save()
User UserModel::getAfterUser(string &username)
{
//this->io.write(reinterpret_cast<char *>(&this->User), sizeof(this->User));
this->io.seekg(0, this->io.beg);

do
{
this->io.read(reinterpret_cast<char *>(&this->user), sizeof(User));

if (this->user.username == username)
{
return this->user;
}
} while (!this->io.eof());

throw("Username not found!");
}

User UserModel::getAfterId(int id)
{
this->io.seekg((id - 1) * sizeof(User), this->io.beg);
this->io.read(reinterpret_cast<char *>(&this->user), sizeof(User));

return this->user;
}

void UserModel::markAs(string &status, int id)
{
this->getAfterId(id);

this->user.active = (status == "active") ? true : false;

cout << "Theoretically wrote the data into the file"
<< endl;
this->save();
}

// Serialize the object when saving a user.
void UserModel::save()
{
this->io.seekp((this->user.id - 1) * sizeof(User), this->io.beg);
this->io.write(reinterpret_cast<char *>(&this->user), sizeof(User));
}

void UserModel::setAttributes(map<string, string> &cleanInputs)
Expand All @@ -19,28 +51,30 @@ void UserModel::setAttributes(map<string, string> &cleanInputs)
}
else if (it->first == "fullname")
{
strcpy(this->User.full_name, it->second.c_str());
strcpy(this->user.full_name, it->second.c_str());
}
else if (it->first == "email")
{
strcpy(this->User.email, it->second.c_str());
strcpy(this->user.email, it->second.c_str());
}
else if (it->first == "username")
{
strcpy(this->User.username, it->second.c_str());
strcpy(this->user.username, it->second.c_str());
}
else if (it->first == "password")
{
strcpy(this->User.password, it->second.c_str());
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)
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));
strftime(this->user.created_at, sizeof(this->user.created_at), "%c", localtime(&t));
}

this->user.id = ++this->lastId;
}

UserModel::~UserModel()
Expand All @@ -51,16 +85,36 @@ UserModel::~UserModel()
string UserModel::pathToFile = "..\\database\\users.store";
void UserModel::openIOStream()
{
this->io.open(UserModel::pathToFile, ios::in | ios::out | ios::binary | ios::app);
this->io.open(UserModel::pathToFile, ios::in | ios::out | ios::binary);
this->io.seekp(0, this->io.end);
this->fileSize = this->io.tellp();

if (!this->io.is_open())
{
cout << "Couldn't open the file stream to " + UserModel::pathToFile;
cout << "Couldn't open the file stream path: " << UserModel::pathToFile;
}
}

void UserModel::setLastId()
{
if (this->fileSize == 0)
{
this->lastId = 0;
}
else
{
User lastUser;

this->io.seekg((this->fileSize / sizeof(User) - 1) * sizeof(User), this->io.beg);
this->io.read(reinterpret_cast<char *>(&lastUser), sizeof(lastUser));

this->lastId = lastUser.id;
}
}

UserModel::UserModel()
{
this->openIOStream();
this->protectedAttributes = { "created_at", "deleted_at", "role", "active" };
this->protectedAttributes = { "created_at", "deleted_at", "role", "active", "banned" };
this->setLastId();
}
39 changes: 26 additions & 13 deletions app/UserModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,41 @@

#include "ModelInterface.h"

typedef struct {
int id;

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;
bool banned = 0;
} User;

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;

User user;

void openIOStream();
void setLastId();

int fileSize;
int lastId;
public:
// Open the IOStream and assign the protected attributes that shouldn't be changed by the user
UserModel();

//vector<User> getAll();
User getAfterUser(string &);
User getAfterId(int);
void markAs(string &, int);
void save();
void setAttributes(map<string, string> &);

Expand Down
Loading

0 comments on commit b8efd56

Please sign in to comment.