-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreply.rb
More file actions
77 lines (61 loc) · 1.51 KB
/
reply.rb
File metadata and controls
77 lines (61 loc) · 1.51 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
74
75
76
77
require_relative 'questions_database'
require_relative 'table'
class Reply
def self.all
results = QuestionsDatabase.instance.execute('SELECT * FROM replies')
results.map { |result| Reply.new(result) }
end
def self.find_by_question_id(question_id)
results = QuestionsDatabase.instance.execute(<<-SQL, question_id)
SELECT
*
FROM
replies
WHERE
replies.question_id = ?
SQL
results.map { |result| Reply.new(result) }
end
def self.find_by_reply_id(id)
results = QuestionsDatabase.instance.execute(<<-SQL, id)
SELECT
*
FROM
replies
WHERE
replies.id = ?
SQL
Reply.new(results.first)
end
def self.find_by_replier_id(replier_id)
results = QuestionsDatabase.instance.execute(<<-SQL, replier_id)
SELECT
*
FROM
replies
WHERE
replies.replier_id = ?
SQL
Reply.new(results.first)
end
attr_accessor :id, :question_id, :parent_reply_id, :replier_id, :body
def initialize(options = {})
@id = options['id']
@question_id = options['question_id']
@parent_reply_id = options['parent_reply_id']
@replier_id = options['replier_id']
@body = options['body']
end
def author
User::find_by_user_id(@replier_id)
end
def child_replies
Reply.all.select{|reply| reply.parent_reply_id == @id}
end
def parent_reply
Reply::find_by_reply_id(@parent_reply_id)
end
def question
Question::find_by_question_id(@question_id)
end
end