def get_all( self ): """return all objects in an array""" """if no user objects are found, returned array should be empty""" all_rows = [] for answer in list(self.db.answers.find()): answer_obj = qanda.Answer(answer["_id"], answer["body"], self.VotesCount(answer["_id"], 1), self.VotesCount(answer["_id"], -1)) all_rows.append(answer_obj) return all_rows
def get_answers( self, question_id ): """find all answers to a question""" """answers are returned as an array of Answer objects""" """KeyError exception should be thrown if question_id not found""" answer_arr = [] for answer in list(self.db.question.find({"answers.qid": question_id })): answer_obj = qanda.Answer(answer["_id"], answer["body"], self.VotesCount(answer["_id"], 1), self.VotesCount(answer["_id"], -1)) answer_arr.append(obj) return answer_arr
def dbRow_To_AnswerObj(self, results): """convert each row from the answer table to an Answer Object""" answers = [] for row in results: id = row[0] body = row[1] vote = self.vote_tally(id) up_vote = vote[0] down_vote = vote[1] answer = qanda.Answer(id, body, up_vote, down_vote) answers.append(answer) return answers
def doc_to_ansObj(self, cursor): answers = [] for entry in cursor: for i in range(len(entry["answer"])): id = entry["answer"][i]["_id"] body = entry["answer"][i]["text"] votes = entry["answer"][i]["vote"] vote_tally = self.vote_tally(votes) up_vote = vote_tally[0] down_vote = vote_tally[1] answer = qanda.Answer(id,body, up_vote, down_vote) answers.append(answer) return answers