Skip to content

Commit

Permalink
Check if persisting data to file was successful.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Nov 28, 2016
1 parent 4fc87f8 commit 4ab2691
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
17 changes: 7 additions & 10 deletions app/UserModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ User UserModel::getAfterUser(string &username)
}
}

// Otherwise if we reach the end of the file, the eof flag will be set and write will fail.
this->io.clear();

throw invalid_argument("Username not found!");
Expand All @@ -23,8 +22,6 @@ 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));

this->io.clear();

return this->user;
}

Expand All @@ -40,8 +37,6 @@ User UserModel::getActive()
}
}

this->io.clear();

throw exception("No active user!");
}

Expand All @@ -59,8 +54,6 @@ bool UserModel::userExists(string &username)
}
}

this->io.clear();

return false;
}

Expand All @@ -78,7 +71,13 @@ 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));
// Otherwise if we reach the end of the file, the eof flag will be set and write will fail.
this->io.clear();

if (!this->io.write(reinterpret_cast<char *>(&this->user), sizeof(User)))
{
throw system_error(error_code(3, generic_category()), "Failed persisting data to file!");
}
}

void UserModel::setAttributes(map<string, string> &cleanInputs)
Expand Down Expand Up @@ -176,8 +175,6 @@ void UserModel::setLastId()
this->io.seekg((this->fileSize / sizeof(User) - 1) * sizeof(User), this->io.beg);
this->io.read(reinterpret_cast<char *>(&this->user), sizeof(User));

this->io.clear();

this->lastId = this->user.id;
}
}
Expand Down
35 changes: 26 additions & 9 deletions app/UserRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
try
{
User user = this->model.getAfterUser(cleanInput.find("username")->second);
if (!strcmp(user.password, cleanInput.find("password")->second.c_str())
if (!strcmp(user.password, cleanInput.find("password")->second.c_str())
&& !strcmp(user.deleted_at, "")
&& user.banned != true)
{
this->model.markAs(string("active"), user.id);
toast(string("Login successful! Please press -c- confirm your access to the dashboard."), string("success"));
printString("\n");
try
{
this->model.markAs(string("active"), user.id);

toast(string("Login successful! Please press -c- confirm your access to the dashboard."), string("success"));
printString("\n");
}
catch (const system_error &e)
{
Controller::pushError(string(e.what()));
}
}
else
{
Expand All @@ -63,14 +71,23 @@ void UserRepository::receiveCleanInput(map<string, string> &cleanInput)
else if (action == "signup")
{
// Try finding the user and validate the request.
if (this->model.userExists(cleanInput.find("username")->second)) {
if (this->model.userExists(cleanInput.find("username")->second))
{
Controller::pushError(string("The username already exists!"));
}
else {
toast(string("Account created successfully! Please press -c- confirm your access to the dashboard."), string("success"));
printString("\n");
else
{
try
{
this->model.save();

this->model.save();
toast(string("Account created successfully! Please press -c- confirm your access to the dashboard."), string("success"));
printString("\n");
}
catch (const system_error &e)
{
Controller::pushError(string(e.what()));
}
}
}
else
Expand Down
Binary file modified database/users.store
Binary file not shown.
11 changes: 11 additions & 0 deletions database/users.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ user
0
0

5
Frasiii Fras
frac@yahoo.com
fraaac1234
Adr5$32
Mon Nov 28 15:25:36 2016

user
0
0

2 changes: 1 addition & 1 deletion views/logout.view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
_\ \ || (_| | (__| </ ___/| | |_| \__ \/ ___/| | |_| \__ \
\__/\__\__,_|\___|_|\_\/ |_|\__,_|___/\/ |_|\__,_|___/

~~~ Do you want to log out? (y/b)
~~~ Do you want to log out? (Y/b)

===========================================================
# (c) 2016 Cristian-Adrian Frasineanu | GNU GPLv3 licence #
Expand Down

0 comments on commit 4ab2691

Please sign in to comment.