Improved readability of the code

… by using the namespace "database" for functions that interact with the database.
This commit is contained in:
tastytea 2018-10-17 17:28:29 +02:00
parent 0f6de4d26e
commit 79bd6f7d01
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
4 changed files with 26 additions and 21 deletions

View File

@ -42,7 +42,7 @@ void MainWindow::populate_tableview()
tableview->horizontalHeader()->resizeSection(0, 250); tableview->horizontalHeader()->resizeSection(0, 250);
result_view result; result_view result;
if (view(result)) if (database::view(result))
{ {
for (const std::tuple<string, int, string> &line : result) for (const std::tuple<string, int, string> &line : result)
{ {
@ -85,7 +85,7 @@ void MainWindow::remove()
for (auto &row : selection->selectedRows()) for (auto &row : selection->selectedRows())
{ {
const string user = row.data().toString().toStdString(); const string user = row.data().toString().toStdString();
::remove(user); database::remove(user);
statusBar()->showMessage(tr("Removed %1 from database.") statusBar()->showMessage(tr("Removed %1 from database.")
.arg(QString::fromStdString(user))); .arg(QString::fromStdString(user)));
_model->removeRow(row.row()); _model->removeRow(row.row());
@ -119,7 +119,7 @@ void MainWindow::show_details(QModelIndex index)
result_details result; result_details result;
string text = ""; string text = "";
if (details(user, result)) if (database::details(user, result))
{ {
if (!std::get<2>(result).empty()) if (!std::get<2>(result).empty())
{ {
@ -188,13 +188,13 @@ void DialogAdd::accept()
{ {
return; return;
} }
::add_block(user, blocked, reason); database::add_block(user, blocked, reason);
_parent->add_row(QString::fromStdString(user), _parent->add_row(QString::fromStdString(user),
blocked, blocked,
QString::fromStdString(reason)); QString::fromStdString(reason));
for (const string &receipt : std::get<3>(data)) for (const string &receipt : std::get<3>(data))
{ {
::add_url(user, receipt); database::add_receipt(user, receipt);
} }
_parent->statusBar()->showMessage(tr("Added %1 to database.") _parent->statusBar()->showMessage(tr("Added %1 to database.")

View File

@ -65,7 +65,7 @@ int main(int argc, char *argv[])
cin.ignore(); cin.ignore();
std::getline(cin, reason, '\n'); std::getline(cin, reason, '\n');
if (add_block(user, blocked, reason)) if (database::add_block(user, blocked, reason))
{ {
cout << user << " added.\n"; cout << user << " added.\n";
} }
@ -80,7 +80,7 @@ int main(int argc, char *argv[])
cout << "URL: "; cout << "URL: ";
cin >> url; cin >> url;
if (add_url(user, url)) if (database::add_receipt(user, url))
{ {
cout << "Receipt added.\n"; cout << "Receipt added.\n";
} }
@ -103,7 +103,7 @@ int main(int argc, char *argv[])
cout << "User or instance: "; cout << "User or instance: ";
cin >> user; cin >> user;
if (remove(user)) if (database::remove(user))
{ {
cout << user << " removed.\n"; cout << user << " removed.\n";
} }
@ -113,7 +113,7 @@ int main(int argc, char *argv[])
case 'V': case 'V':
{ {
result_view result; result_view result;
if (view(result)) if (database::view(result))
{ {
for (const std::tuple<string, int, string> &line : result) for (const std::tuple<string, int, string> &line : result)
{ {
@ -138,7 +138,7 @@ int main(int argc, char *argv[])
cin >> answer; cin >> answer;
{ {
result_details result; result_details result;
if (details(answer, result)) if (database::details(answer, result))
{ {
cout << answer << " is "; cout << answer << " is ";
if (std::get<0>(result) == 1) if (std::get<0>(result) == 1)

View File

@ -54,7 +54,8 @@ const string get_filepath()
return filepath; return filepath;
} }
const bool add_block(const string &user, const int blocked, const string &reason) const bool database::add_block(const string &user, const int blocked,
const string &reason)
{ {
try try
{ {
@ -72,13 +73,13 @@ const bool add_block(const string &user, const int blocked, const string &reason
return true; return true;
} }
const bool add_url(const string &user, const string &url) const bool database::add_receipt(const string &user, const string &receipt)
{ {
try try
{ {
sqlite::connection con(get_filepath()); sqlite::connection con(get_filepath());
sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);"); sqlite::execute ins(con, "INSERT INTO urls VALUES(?, ?);");
ins % user % url; ins % user % receipt;
ins(); ins();
} }
catch (const std::exception &e) catch (const std::exception &e)
@ -90,7 +91,7 @@ const bool add_url(const string &user, const string &url)
return true; return true;
} }
const bool remove(const string &user) const bool database::remove(const string &user)
{ {
try try
{ {
@ -111,7 +112,7 @@ const bool remove(const string &user)
return true; return true;
} }
const bool view(result_view &result) const bool database::view(result_view &result)
{ {
try try
{ {
@ -137,7 +138,7 @@ const bool view(result_view &result)
return true; return true;
} }
const bool details(const string &user, result_details &result) const bool database::details(const string &user, result_details &result)
{ {
try try
{ {

View File

@ -26,10 +26,14 @@ using result_view = std::vector<std::tuple<string, int, string>>;
using result_details = std::tuple<int, string, std::vector<string>>; using result_details = std::tuple<int, string, std::vector<string>>;
const string get_filepath(); const string get_filepath();
const bool add_block(const string &user, const int blocked, const string &reason); namespace database
const bool add_url(const string &user, const string &url); {
const bool remove(const string &user); const bool add_block(const string &user, const int blocked,
const bool view(result_view &result); const string &reason);
const bool details(const string &user, result_details &result); const bool add_receipt(const string &user, const string &receipt);
const bool remove(const string &user);
const bool view(result_view &result);
const bool details(const string &user, result_details &result);
}
#endif // WHYBLOCKED_HPP #endif // WHYBLOCKED_HPP