-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathumarell_gcp.py
49 lines (38 loc) · 1.39 KB
/
umarell_gcp.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
from google.cloud import firestore
from google.cloud.firestore_v1.base_query import FieldFilter
class Umarell(object):
def __init__(self):
self.db = firestore.Client()
def addUmarell(self, id, body):
try:
int(id) # check id is integer
nome = body['nome']
cognome = body['cognome']
cap = int(body['cap'])
id = str(id)
if cap < 0 or cap > 99999:
raise Exception('Cap errror!')
except:
return None, 400
# conflict
if self.db.collection('umarell').document(id).get().exists:
return None, 409
res = {'nome': nome, 'cognome': cognome, 'cap': cap}
self.db.collection('umarell').document(id).set(res)
return res, 201
def getUmarell(self, id):
doc = self.db.collection('umarell').document(str(id)).get()
if doc.exists:
return doc.to_dict(), 200
return None, 404
def search(self, cap):
res = []
for doc in self.db.collection("umarell").where(filter=FieldFilter("cap", "==", cap)).get():
um = doc.to_dict()
um['id'] = doc.id
res.append(um)
return res
def clean(self):
for doc in self.db.collection('umarell').stream():
doc_ref = doc.reference
doc_ref.delete()