Skip to content

Commit

Permalink
EntityInterface and accessor model in order to validate the input and…
Browse files Browse the repository at this point in the history
… forward the sanitized input to the main model.
  • Loading branch information
cristianfrasineanu committed Nov 23, 2016
1 parent 7da1238 commit 2c5f5cf
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 38 deletions.
1 change: 0 additions & 1 deletion app/Bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "Console.h"
#include <vld>

using namespace std;

Expand Down
7 changes: 6 additions & 1 deletion app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//---Console---
//-------------
// We need an initial state, because we don't have any means like mapping views to HTTP routes.
// The Console class represents an entry point for all the other components, like View
// The Console class represents an entry point for all the other components, like View.
string Console::initialView = "home.view";
string Console::viewsFolder = "..\\views";

Expand Down Expand Up @@ -88,6 +88,11 @@ char Console::getLastInput()
return this->lastInput;
}

string & Console::getViewsFolder()
{
return Console::viewsFolder;
}

void Console::loadViews(const fs::path &viewsFolder)
{
if (!fs::exists(viewsFolder)
Expand Down
1 change: 1 addition & 0 deletions app/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Console {

vector<char> &getActions();
char getLastInput();
string &getViewsFolder();

void setLastInput(char);
bool takeActionIfAny();
Expand Down
30 changes: 21 additions & 9 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ void Controller::prepareView()

if (this->hasInput(copyChunk))
{
this->controllerAttributions.insert(this->controllerAttributions.begin(), "input");
this->controllerAttributions.push_back("input");
}
if (this->hasOutput(copyChunk))
{
this->controllerAttributions.insert(this->controllerAttributions.begin(), "output");
this->controllerAttributions.push_back("output");
}

// Determine the order of the input/output from the user.
Expand Down Expand Up @@ -55,17 +55,31 @@ void Controller::prepareView()
cout << copyChunk
<< endl;
}

// The input was validated and the model can safely operate now.
this->model.confirmInput();
}

void Controller::prepareViewInput(string &subChunk, string &inputAlias)
{
string userInput;

map<string, string> currentInput;

cout << subChunk << " ";
cin >> userInput;
cout << endl;

this->userInputs[inputAlias] = userInput;
currentInput[inputAlias] = userInput;

// Send the serialized input to the accessor model in order to determine if there are
// any validation errors and if we can proceed
//try
//{
this->model.sendSerializedInput(currentInput);
//}

// catch if there are any errors, either validation or argument error

}

Controller::Controller()
Expand All @@ -74,7 +88,6 @@ Controller::Controller()
strcpy(this->controllerName, "NO_CONTROLLER");
this->viewChunk = "";
this->controllerAttributions = {};
this->userInputs = {};
}

Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
Expand All @@ -86,7 +99,6 @@ Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
strcpy(this->controllerName, controllerName.c_str());
this->viewChunk = viewChunk;
this->controllerAttributions = {};
this->userInputs = {};

(this->hasInput(viewChunk) || this->hasOutput(viewChunk)) ? this->prepareView() : this->justShow();
}
Expand All @@ -104,15 +116,15 @@ 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(),
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()));

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

chunk.erase(0, chunk.find(inputAlias) + inputAlias.size() + 2);
}

// One Controller at a time, you expected more?
// Don't assign multiple Controllers, stupid.
void Controller::operator=(const Controller &controller)
{
if (controller.controllerName != NULL)
Expand All @@ -138,4 +150,4 @@ bool Controller::hasOutput(string &raw)
Controller::~Controller()
{
delete[] this->controllerName;
}
}
4 changes: 2 additions & 2 deletions app/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>

#include "Helpers.h"
#include "Model.h"

using namespace std;

Expand All @@ -14,9 +15,8 @@ class Controller {

char *controllerName;
vector<string> controllerAttributions;
map<string, string> userInputs;
string viewChunk;
// Model model;
Model model;

void justShow();
void prepareView();
Expand Down
27 changes: 27 additions & 0 deletions app/Entity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <string>
#include <map>

#include "Helpers.h"

using namespace std;

class EntityInterface {
private:
virtual void defineValidationRules() = 0;
protected:
map<string, string> ValidationRules;

// Might change, as we writing only string to the DB
map<string, string> payLoad;
public:
virtual void receiveCleanInput(map<string, string> &) = 0;
virtual void writeNewRecord() = 0;
virtual void validateItem() = 0;

virtual void retrieveItemForActive() = 0;
virtual void retrieveAll() = 0;

virtual void logPayload() = 0;
};
6 changes: 6 additions & 0 deletions app/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#include "Helpers.h"

void toLowerCase(string &input)
{
transform(input.begin(), input.end(), input.begin(),
[](unsigned char ch) { return tolower(ch); });
}

void sleepAndClearBuffer(unsigned delay)
{
Sleep(delay);
Expand Down
7 changes: 4 additions & 3 deletions app/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <conio.h>
#include <Windows.h>

// Define include guard as function templates will get a redefined error.
#pragma once

using namespace std;

template<typename T>
Expand Down Expand Up @@ -56,5 +56,6 @@ inline bool isInMap(map<FirstT, SecondT> &haystack, FirstT needle)
return haystack.find(needle) != haystack.end();
}

void toLowerCase(string &);
void sleepAndClearBuffer(unsigned delay);
void clearScreen();
60 changes: 60 additions & 0 deletions app/Model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "Model.h"

string Model::parseModelName(string inputAlias)
{
return inputAlias.erase(inputAlias.find("-"), string::npos);
}

void Model::attachModel(string &model)
{
toLowerCase(model);
if (model == "user")
{
this->currentModel = new User();
}
else if (model == "question")
{

}
else if (model == "category")
{

}
}

Model::Model()
{
this->accessHistory = {};
this->currentModel = NULL;
}

void Model::sendSerializedInput(map<string, string> &input)
{
string modelName = this->parseModelName(input.begin()->first);

if (this->currentModel == NULL)
{
this->attachModel(modelName);
}

string inputAlias = input.begin()->first;
inputAlias.erase(0, modelName.size() + 1);

// if (this->currentModel->validateItem(inputAlias, input.begin()->second)) {

// If the validation was successful push back into the ready map
this->ready[inputAlias] = input.begin()->second;

// }
}

void Model::confirmInput()
{
// Send the input to the attached model and determine if there are any errors and if the payload is complete
this->currentModel->receiveCleanInput(this->ready);
}

Model::~Model()
{
delete this->currentModel;
}
31 changes: 31 additions & 0 deletions app/Model.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <iostream>
#include <string>
#include <vector>
#include <map>

#include "Helpers.h"
#include "Entity.h"
#include "User.h"

using namespace std;

class Model {
private:
static string userModelAlias;
static string questionModelAlias;

vector<string> accessHistory;
map<string, string> ready;

EntityInterface *currentModel;

string parseModelName(string);
void attachModel(string &);
public:
Model();

void sendSerializedInput(map<string, string> &);
void confirmInput();

~Model();
};
46 changes: 33 additions & 13 deletions app/User.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
#include "User.h"

//--- User ---
//------------
unsigned User::count = 0;
string User::modelPath = "..\\db\\user.store";
string User::modelAlias = "user";

void User::setFromLastUuid()
void User::defineValidationRules()
{
// Get the last uuid from the db file
this->ValidationRules = {};
}

User::User()
{
this->defineValidationRules();
}

void User::receiveCleanInput(map<string, string>& cleanInput)
{
cout << "Got the input, doing something with it..."
<< endl;
}

User::count = 30;
void User::writeNewRecord()
{
//
}

void User::validateItem()
{
// Appli validation rules for the next item
}

User::User() : uuid(User::count++)
void User::retrieveItemForActive()
{
this->nick = new char[4];
// Search for the record having active set to true
}

User::~User()
void User::retrieveAll()
{
delete[] this->nick;
// Print everything
}

unsigned User::getUuid()
void User::logPayload()
{
return this->uuid;
}
//
}


27 changes: 20 additions & 7 deletions app/User.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
class User {
#include <iostream>
#include <string>
#include <map>

#include "Entity.h"

class User : public EntityInterface {
private:
static unsigned count;
static string modelPath;
static string modelAlias;

const unsigned uuid;
char *nick;
void defineValidationRules();
public:
static void setFromLastUuid();
User();
~User();
unsigned getUuid();

void receiveCleanInput(map<string, string> &);
void writeNewRecord();
void validateItem();

void retrieveItemForActive();
void retrieveAll();

void logPayload();
// Set the active to false when destroying the object.
};
Loading

0 comments on commit 2c5f5cf

Please sign in to comment.