-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathartifact.py
55 lines (49 loc) · 1.56 KB
/
artifact.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
# This file contains what might be useful later for app.py
# app.py
'''
import os
from flask import Flask, request, jsonify
from firebase_admin import credentials, firestore, initialize_app
app = Flask(__name__)
#Initializing Firebase DB
cred = credentials.Certificate('key.json')
default_app = initialize_app(cred)
db = firestore.client()
todo_ref = db.collection('todos')
@app.route('/add', methods = ['POST'])
def create():
try:
id = request.json['id']
todo_ref.document(id).set(request.json)
return jsonify({"success": True}), 200
except Exception as e:
return f"An error occurred: {e}"
@app.route('/list', methods = ['GET'])
def read():
try:
todo_id = request.args.get('id')
if todo_id:
todo = todo_ref.document(todo_id).get()
return jsonify(todo.to_dict()), 200
else:
all_todos = [doc.to_dict() for doc in todo_ref.stream()]
return jsonify(all_todos), 200
except Exception as e:
return f"An error occurred: {e}"
@app.route('/update', methods = ['POST', 'PUT'])
def update():
try:
id = request.json['id']
todo_ref.document(id).update(request.json)
return jsonify({"success": True}), 200
except Exception as e:
return f"An error occurred: {e}"
@app.route('/delete', methods = ['GET', 'DELETE'])
def delete():
try:
todo_id = request.args.get('id')
todo_ref.document(todo_id).delete()
return jsonify({"success": True}), 200
except Exception as e:
return f"An error occurred: {e}"
'''