-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathads.py
80 lines (72 loc) · 2.18 KB
/
ads.py
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
from database import DB
class Ad:
def __init__(self, id, title, description, price, date_created, is_active, owner_id, buyer_id):
self.id = id
self.title = title
self.description = description
self.price = price
self.date_created = date_created
self.is_active = is_active
self.owner_id = owner_id
self.buyer_id = buyer_id
def create(self):
with DB() as db:
db.execute(
'''
INSERT INTO ads (id, title, description, price, date_created, is_active, owner_id)
VALUES ({}, {}, {}, {}, {}, {}, {})
'''.format(
self.id,
self.title,
self.description,
self.price,
self.date_created,
self.is_active,
self.owner_id))
return self
@staticmethod
def all():
with DB() as db:
rows = db.execute('SELECT * FROM ads').fetchall()
return [Ad(*row) for row in rows]
@staticmethod
def to_dict(ad):
return {
'id': ad.id,
'title': ad.title,
'description': ad.description,
'price': ad.price,
'date_created': ad.date_created,
'is_active': ad.is_active,
'owner_id': ad.owner_id,
'buyer_id': ad.buyer_id
}
@staticmethod
def find_by(column, data):
if not data:
return None
with DB() as db:
row = db.execute(
'SELECT * FROM ads WHERE {} = {}'.format(column, data)
).fetchone()
if row:
return Ad(*row)
@staticmethod
def update(id, column, data):
with DB() as db:
db.execute(
'''
UPDATE ads
SET {} = "{}"
WHERE id = {}
'''.format(column, data, id))
return
@staticmethod
def delete(id):
with DB() as db:
db.execute(
'''
DELETE FROM ads
WHERE id = {}
'''.format(id))
return