Skip to content

Commit

Permalink
List own questions for a user.
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianfrasineanu committed Dec 4, 2016
1 parent e1accb0 commit f1d5a7b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
16 changes: 16 additions & 0 deletions app/QuestionModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ vector<Question> QuestionModel::retrieveAll()
return questions;
}

vector<Question> QuestionModel::retrieveForUserId(int userId)
{
vector<Question> userQuestions;

this->io.seekg(0, this->io.beg);
while (this->io.read(reinterpret_cast<char *>(&this->question), sizeof(Question)))
{
if (this->question.user_id == userId)
{
userQuestions.push_back(this->question);
}
}

return userQuestions;
}

int QuestionModel::getId()
{
return this->question.id;
Expand Down
1 change: 1 addition & 0 deletions app/QuestionModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class QuestionModel : public ModelInterface {
Question setAfterUserId(int);
Question setAfterId(int);
vector<Question> retrieveAll();
vector<Question> retrieveForUserId(int);

int getId();
char *getTitle();
Expand Down
36 changes: 36 additions & 0 deletions app/UserRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,43 @@ void UserRepository::echo(const string &alias)
}
else if (alias == "questions")
{
this->model.setActiveIfAny();

vector<Question> userQuestions = this->questions.retrieveForUserId(this->model.getId());

if (!userQuestions.empty())
{
for (vector<Question>::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.");
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions app/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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', "" } } },

Expand All @@ -85,7 +86,9 @@ void View::loadViewsOptions()

{ createView, { } },

{ showQuestionView, { { '1', dashboardView } } }
{ showQuestionView, { { '1', dashboardView } } },

{ userQuestionsView, { { 'b', "" } } }
};
}

Expand Down
File renamed without changes.

0 comments on commit f1d5a7b

Please sign in to comment.