-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.rb
114 lines (90 loc) · 2.09 KB
/
notes.rb
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copy/ Paste Your Customer, Owner, Restaurant, and Review Classes Here
class Customer
include Databaseable::InstanceMethods
extend Databaseable::ClassMethods
ATTRIBUTES = {
id: "INTEGER PRIMARY KEY",
name: "TEXT",
birth_year: "INTEGER",
hometown: "TEXT"
}
attr_accessor(*self.public_attributes)
attr_reader :id
def reviews
sql = <<-SQL
SELECT * FROM reviews
WHERE reviews.customer_id = ?
SQL
self.class.db.execute(sql, self.id)
end
def restaurants
sql = <<-SQL
SELECT restaurants.* FROM restaurants
INNER JOIN reviews ON reviews.restaurant_id = restaurants.id
WHERE reviews.customer_id = ?
SQL
self.class.db.execute(sql, self.id)
end
end
class Owner
include Databaseable::InstanceMethods
extend Databaseable::ClassMethods
ATTRIBUTES = {
id: "INTEGER PRIMARY KEY",
name: "TEXT",
}
attr_accessor(*self.public_attributes)
attr_reader :id
def restaurants
sql = <<-SQL
SELECT * FROM restaurants
WHERE restaurants.owner_id = ?
SQL
self.class.db.execute(sql, self.id)
end
end
class Restaurant
include Databaseable::InstanceMethods
extend Databaseable::ClassMethods
ATTRIBUTES = {
id: "INTEGER PRIMARY KEY",
name: "TEXT",
location: "TEXT",
owner_id: "INTEGER"
}
attr_accessor(*self.public_attributes)
attr_reader :id
def owner
sql = <<-SQL
SELECT * FROM owners
WHERE owners.id = ?
SQL
self.class.db.execute(sql, self.owner_id).first
end
end
class Review
include Databaseable::InstanceMethods
extend Databaseable::ClassMethods
ATTRIBUTES = {
id: "INTEGER PRIMARY KEY",
customer_id: "INTEGER",
restaurant_id: "INTEGER",
review: "TEXT"
}
attr_accessor(*self.public_attributes)
attr_reader :id
def customer
sql = <<-SQL
SELECT * FROM customers
WHERE customers.id = ?
SQL
self.class.db.execute(sql, self.customer_id).first
end
def restaurant
sql = <<-SQL
SELECT * FROM restaurants
WHERE restaurants.id = ?
SQL
self.class.db.execute(sql, self.restaurant_id).first
end
end