Skip to content

Commit

Permalink
Validation concept and repositories.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 23, 2016
1 parent 2c5f5cf commit f21c487
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 147 deletions.
16 changes: 15 additions & 1 deletion app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,21 @@ void Console::handleView()
{
buffer << viewFile.rdbuf();
this->currentView.setRawFormat(buffer.str());
this->theController = Controller(this->currentView.getViewName(), buffer.str(), View::getViewExtension());

// If there's any validation error inside the model
try
{
this->theController = Controller(this->currentView.getViewName(), buffer.str(), View::getViewExtension());
}
catch (const invalid_argument &e)
{
cout << endl
<< e.what()
<< endl;

sleepAndClearBuffer(3 * this->delay);
this->reloadView();
}

buffer.clear();
viewFile.close();
Expand Down
27 changes: 11 additions & 16 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ void Controller::prepareView()
<< endl;
}

// The input was validated and the model can safely operate now.
this->model.confirmInput();
// Send the payload to be validated and handled by the main model
if (isInVector(this->controllerAttributions, "input"))
{
this->model.confirmInput(this->userInputs);
}
}

void Controller::prepareViewInput(string &subChunk, string &inputAlias)
void Controller::prepareViewInput(const string &subChunk, const string &inputAlias)
{
string userInput;
map<string, string> currentInput;
Expand All @@ -69,17 +72,7 @@ void Controller::prepareViewInput(string &subChunk, string &inputAlias)
cin >> userInput;
cout << endl;

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

this->userInputs[inputAlias] = userInput;
}

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

Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
Expand All @@ -99,6 +93,7 @@ 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 Down Expand Up @@ -137,12 +132,12 @@ void Controller::operator=(const Controller &controller)
this->controllerAttributions = controller.controllerAttributions;
}

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

bool Controller::hasOutput(string &raw)
bool Controller::hasOutput(const string &raw)
{
return raw.find(Controller::viewOutputFormat) != string::npos;
}
Expand Down
7 changes: 4 additions & 3 deletions app/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ class Controller {

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

void justShow();
void prepareView();
void prepareViewInput(string &, string &);
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:
Controller();
Controller(char *, string &, string &);

bool hasInput(string &);
bool hasOutput(string &);
bool hasInput(const string &);
bool hasOutput(const string &);

vector<string> &getControllerAttributions();
char *getControllerName();
Expand Down
27 changes: 0 additions & 27 deletions app/Entity.h

This file was deleted.

6 changes: 3 additions & 3 deletions app/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using namespace std;

template<typename T>
inline void log(T &data, char *identifiedBy, char *when)
inline void log(const T &data, char *identifiedBy, char *when)
{
cout << endl << "---*****LOGGER*****---" << endl
<< identifiedBy << " was " << data << " when " << when << endl
Expand Down Expand Up @@ -44,8 +44,8 @@ inline void log(map<FirstT, SecondT> &data, char *identifiedBy, char *when)
<< "---*****LOGGER*****---" << endl << endl;
}

template<typename V>
inline bool isInVector(vector<V> &haystack, V needle)
template<typename V, typename N>
inline bool isInVector(vector<V> &haystack, N needle)
{
return find(haystack.begin(), haystack.end(), needle) != haystack.end();
}
Expand Down
44 changes: 24 additions & 20 deletions app/Model.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "Model.h"

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

void Model::attachModel(string &model)
void Model::attachEntity(string &model)
{
toLowerCase(model);
if (model == "user")
{
this->currentModel = new User();
this->repository = new UserRepository();
}
else if (model == "question")
{
Expand All @@ -24,37 +24,41 @@ void Model::attachModel(string &model)

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

void Model::sendSerializedInput(map<string, string> &input)
void Model::sendSerializedInput()
{
string modelName = this->parseModelName(input.begin()->first);
// There's interaction with only one model on the view.
string modelName = this->parseEntityName(this->rawInput.begin()->first),
inputAlias;

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

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

// if (this->currentModel->validateItem(inputAlias, input.begin()->second)) {
for (map<string, string>::iterator it = this->rawInput.begin(); it != this->rawInput.end(); it++)
{
inputAlias = it->first;
inputAlias.erase(0, modelName.size() + 1);

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

// }
// Sanitize the input, if there are any errors, display them and reload the view.
this->repository->validateItems(this->serializedInput);
}

void Model::confirmInput()
void Model::confirmInput(const map<string, string> &payLoad)
{
// 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);
this->rawInput = payLoad;
this->sendSerializedInput();
}

Model::~Model()
{
delete this->currentModel;
delete this->repository;
}
18 changes: 9 additions & 9 deletions app/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <map>

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

using namespace std;

Expand All @@ -14,18 +14,18 @@ class Model {
static string userModelAlias;
static string questionModelAlias;

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

EntityInterface *currentModel;
RepositoryInterface *repository;

string parseModelName(string);
void attachModel(string &);
string parseEntityName(string);
void attachEntity(string &);
void sendSerializedInput();
public:
Model();

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

~Model();
};
29 changes: 29 additions & 0 deletions app/RepositoryInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include <string>
#include <map>

#include "Helpers.h"

using namespace std;

//---Entity---
//------------
// This entity acts like a repository interface 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.
class RepositoryInterface {
private:
virtual void defineValidation() = 0;
protected:
map<string, string> ValidationRules;
map<string, string> ValidationErrors;
virtual void receiveCleanInput(map<string, string> &) = 0;
public:
virtual void writeNewRecord() = 0;
virtual void validateItems(map<string, string> &) = 0;

virtual void retrieveItemForActive() = 0;
virtual void retrieveAll() = 0;
};
47 changes: 0 additions & 47 deletions app/User.cpp

This file was deleted.

Loading

0 comments on commit f21c487

Please sign in to comment.