From f1d5a7b87e27aed3362ff89d59b4f83ea5337612 Mon Sep 17 00:00:00 2001 From: Cristian-Adrian Frasineanu Date: Sun, 4 Dec 2016 18:22:38 +0200 Subject: [PATCH] List own questions for a user. --- app/QuestionModel.cpp | 16 +++++++++ app/QuestionModel.h | 1 + app/UserRepository.cpp | 36 +++++++++++++++++++ app/View.cpp | 9 +++-- .../{questions.view => user-questions.view} | 0 5 files changed, 59 insertions(+), 3 deletions(-) rename views/user/{questions.view => user-questions.view} (100%) diff --git a/app/QuestionModel.cpp b/app/QuestionModel.cpp index 433f2a8..89c0934 100644 --- a/app/QuestionModel.cpp +++ b/app/QuestionModel.cpp @@ -57,6 +57,22 @@ vector QuestionModel::retrieveAll() return questions; } +vector QuestionModel::retrieveForUserId(int userId) +{ + vector userQuestions; + + this->io.seekg(0, this->io.beg); + while (this->io.read(reinterpret_cast(&this->question), sizeof(Question))) + { + if (this->question.user_id == userId) + { + userQuestions.push_back(this->question); + } + } + + return userQuestions; +} + int QuestionModel::getId() { return this->question.id; diff --git a/app/QuestionModel.h b/app/QuestionModel.h index b544423..314e76f 100644 --- a/app/QuestionModel.h +++ b/app/QuestionModel.h @@ -44,6 +44,7 @@ class QuestionModel : public ModelInterface { Question setAfterUserId(int); Question setAfterId(int); vector retrieveAll(); + vector retrieveForUserId(int); int getId(); char *getTitle(); diff --git a/app/UserRepository.cpp b/app/UserRepository.cpp index 9c60d70..4e96a3a 100644 --- a/app/UserRepository.cpp +++ b/app/UserRepository.cpp @@ -112,7 +112,43 @@ void UserRepository::echo(const string &alias) } else if (alias == "questions") { + this->model.setActiveIfAny(); + vector userQuestions = this->questions.retrieveForUserId(this->model.getId()); + + if (!userQuestions.empty()) + { + for (vector::iterator it = userQuestions.begin(); it != userQuestions.end(); it++) + { + printString((string("#") + string(to_string(it->id)) + string(" ")).c_str()); + printString(it->title); + printString("\n"); + printString("~~~~~~~~~~~~~"); + printString("\n"); + printString(it->body); + printString("\n"); + printString("~~~~~~~~~~~~~"); + printString("\n"); + if (it->votes >= 0) + { + printString((string("+") + string(to_string(it->votes))).c_str()); + } + else + { + printString(string(to_string(it->votes)).c_str()); + } + + // Make it look nice. + if (it != userQuestions.end() - 1) + { + printString("\n\n"); + } + } + } + else + { + printString("Nothing to see here. Move on."); + } } } diff --git a/app/View.cpp b/app/View.cpp index d0017cb..064f96a 100644 --- a/app/View.cpp +++ b/app/View.cpp @@ -57,7 +57,8 @@ void View::loadViewsOptions() findOrAskView = "find-or-ask.view", createView = "create.view", searchResultsView = "search-results.view", - showQuestionView = "show-question.view"; + showQuestionView = "show-question.view", + userQuestionsView = "user-questions.view"; // Create non-linkable views, e.g. search results @@ -71,7 +72,7 @@ void View::loadViewsOptions() { signupView, { { 'c', dashboardView } } }, - { dashboardView, { { '1', findOrAskView }, { '4', logoutView } } }, + { dashboardView, { { '1', findOrAskView }, { '2', userQuestionsView }, { '4', logoutView } } }, { logoutView, { { 'y', "home.view" }, { 'b', "" } } }, @@ -85,7 +86,9 @@ void View::loadViewsOptions() { createView, { } }, - { showQuestionView, { { '1', dashboardView } } } + { showQuestionView, { { '1', dashboardView } } }, + + { userQuestionsView, { { 'b', "" } } } }; } diff --git a/views/user/questions.view b/views/user/user-questions.view similarity index 100% rename from views/user/questions.view rename to views/user/user-questions.view