-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
123 lines (84 loc) · 3.81 KB
/
main.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
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
115
116
117
118
119
120
121
122
123
from http.server import HTTPServer, BaseHTTPRequestHandler
import sqlite3
from urllib.parse import urlparse, parse_qs
import json
def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
server_address = ('http://julienlabonte.ca/', 8000)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
req = urlparse(self.path)
query = parse_qs(req.query)
conn = sqlite3.connect('bieres.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
self.send_response(200)
self.send_header("content-type", "application/json")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
if 'id' in query:
c.execute("SELECT rowid, * FROM biere WHERE rowid=?", query['id'])
bieres = c.fetchall()
else:
c.execute("SELECT rowid, * FROM biere") # sqlite a un build in id. Le rajoute aussi pour des besoins futures d'affichage
bieres = c.fetchall() # Recupere les donnees de la base de donne
res = [dict(biere) for biere in bieres]
self.wfile.write(json.dumps(res).encode("UTF-8"))
conn.commit()
conn.close()
def do_POST(self):
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
conn = sqlite3.connect('bieres.db')
conn.row_factory = sqlite3.Row
c = conn.cursor()
req = urlparse(self.path)
if req.path == '/create':
reqDecode = post_body.decode("UTF-8")
reqParse = parse_qs(reqDecode)
#Le strip vient enlever les caracteres superflus et le float vient tranformer le pourcentage en chiffre.
#Besoin de faire des str parce que sinon il s'agit de listes.
nom = reqParse['nom'][0]
brasserie = reqParse['brasserie'][0]
pourcentage = reqParse['pourcentage'][0]
nBiere = (nom, brasserie, pourcentage)
print(nBiere)
c.execute("INSERT INTO biere VALUES (?,?,?)", nBiere)
res = "La biere a ete cree"
print(res)
self.send_response(200)
self.send_header("content-type", "application/json")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps(res).encode("UTF-8"))
if req.path == '/delete':
reqDecode = post_body.decode("UTF-8")
reqParse = parse_qs(reqDecode)
id = reqParse['id'][0]
c.execute("DELETE FROM biere WHERE rowid = (?)", (id,))
res = "La biere a ete supprime"
self.send_response(200)
self.send_header("content-type", "application/json")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps(res).encode("UTF-8"))
if req.path == '/update':
reqDecode = post_body.decode("UTF-8")
reqParse = parse_qs(reqDecode)
nom = reqParse['nom'][0]
brasserie = reqParse['brasserie'][0]
pourcentage = reqParse['pourcentage'][0]
id = reqParse['id'][0]
nBiere = (nom, brasserie, pourcentage, id)
c.execute("UPDATE biere SET nom = ?, brasserie = ?, pourcentage = ? WHERE rowid = ?", nBiere)
res = "La biere a ete modifie"
print(res)
self.send_response(200)
self.send_header("content-type", "application/json")
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps(res).encode("UTF-8"))
conn.commit()
conn.close()
run(handler_class=Handler)