-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion.rb
More file actions
73 lines (57 loc) · 1.35 KB
/
question.rb
File metadata and controls
73 lines (57 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
require_relative 'questions_database'
class Question
def self.all
results = QuestionsDatabase.instance.execute('SELECT * FROM questions')
results.map { |result| Question.new(result) }
end
def self.find_by_author_id(author_id)
results = QuestionsDatabase.instance.execute(<<-SQL, author_id)
SELECT
*
FROM
questions
WHERE
questions.author_id = ?
SQL
Question.new(results.first)
end
def self.find_by_question_id(id)
results = QuestionsDatabase.instance.execute(<<-SQL, id)
SELECT
*
FROM
questions
WHERE
questions.id = ?
SQL
Question.new(results.first)
end
def self.most_followed(n)
QuestionFollow::most_followed_questions(n)
end
def self.most_liked(n)
QuestionLike.most_liked_questions(n)
end
attr_accessor :id, :title, :body, :author_id
def initialize(options = {})
@id = options['id']
@title = options['title']
@body = options['body']
@author_id = options['author_id']
end
def author
User::find_by_user_id(@author_id)
end
def followers
QuestionFollow::followers_for_question_id(@id)
end
def likers
QuestionLike::likers_for_question_id(@id)
end
def num_likes
QuestionLike.num_likes_for_question_id(@id)
end
def replies
Reply::find_by_question_id(@id)
end
end