-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAd.py
More file actions
121 lines (90 loc) · 3.47 KB
/
Ad.py
File metadata and controls
121 lines (90 loc) · 3.47 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
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
import json
import uuid
from flask import Flask
from flask import request
from flask import render_template
from werkzeug.security import generate_password_hash, check_password_hash
from flask_httpauth import HTTPBasicAuth
import basic_auth
from database import SQLite
from errors import ApplicationError
#import basic_auth
class Ad:
def __init__(self, user_id, title, description, price, date, isActive, buyer):
self.user_id = user_id
self.title = title
self.description = description
self.price = price
self.date = date
self.isActive = isActive
self.buyer = buyer
def to_dict(self):
return self.__dict__
@staticmethod
def create_ad (newAd):
result = None
with SQLite() as db:
result = db.execute("INSERT INTO ads (user_id, title, description, price, date, isActive, buyer) VALUES (?, ?, ?, ?, ?, ?, ?)",
(newAd.user_id, newAd.title, newAd.description, newAd.price, newAd.date, newAd.isActive, newAd.buyer,))
if result.rowcount == 0:
raise ApplicationError("No value present", 404)
@staticmethod
def find_by_id(id):
result = None
with SQLite() as db:
result = db.execute(
"SELECT user_id, title, description, price, date, isActive, buyer FROM ads WHERE id = ?",
(id,))
ad = result.fetchone()
if ad is None:
raise ApplicationError(
"Ad with id {} not found".format(ad), 404)
return Ad(*ad)
@staticmethod
def all():
with SQLite() as db:
result = db.execute(
"SELECT user_id, title, description, price, date, isActive, buyer FROM ads").fetchall()
return [Ad(*row) for row in result]
@staticmethod
def edit(ad_id, part_to_edit, value):
if part_to_edit=="title":
with SQLite() as db:
result = db.execute(
"UPDATE ads SET title = ? WHERE id = ?;",
(value, ad_id,))
elif part_to_edit=="description":
with SQLite() as db:
result = db.execute(
"UPDATE ads SET description = ? WHERE id = ?;",
(value, ad_id,))
elif part_to_edit=="price":
with SQLite() as db:
result = db.execute(
"UPDATE ads SET price = ? WHERE id = ?;",
(value, ad_id,))
elif part_to_edit=="date":
with SQLite() as db:
result = db.execute(
"UPDATE ads SET date = ? WHERE id = ?;",
(value, ad_id,))
elif part_to_edit=="isActive":
with SQLite() as db:
result = db.execute(
"UPDATE ads SET isActive = ? WHERE id = ?;",
(value, ad_id,))
elif part_to_edit=="buyer":
with SQLite() as db:
result = db.execute(
"UPDATE ads SET buyer = ? WHERE id = ?;",
(value, ad_id,))
else:
raise ApplicationError("{} doesnt exist".format(part_to_edit),404)
@staticmethod
def delete(id):
result = None
with SQLite() as db:
result = db.execute("DELETE FROM ads WHERE id = ?",
(id,))
if result.rowcount == 0:
raise ApplicationError("No value present", 404)