-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepository.py
More file actions
35 lines (29 loc) · 1.44 KB
/
Copy pathrepository.py
File metadata and controls
35 lines (29 loc) · 1.44 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
import os
import json
import base64
from google.cloud import firestore
from google.oauth2 import service_account
from fastapi import HTTPException
class Repository():
def __init__(self):
cred_b64 = os.getenv("FIRESTORE_CREDENTIALS_B64")
cred_json = json.loads(base64.b64decode(cred_b64))
credentials = service_account.Credentials.from_service_account_info(cred_json)
self.db = firestore.Client(credentials=credentials)
def get_collection(self, collection_name: str) -> list:
docs = self.db.collection(collection_name).stream()
return [{"id": doc.id, **doc.to_dict()} for doc in docs]
def get_document(self, collection_name: str, doc_id: str) -> dict:
doc = self.db.collection(collection_name).document(doc_id).get()
if not doc.exists:
raise HTTPException(status_code=404, detail="Documento não encontrado")
return {"id": doc.id, **doc.to_dict()}
def set_document(self, collection_name: str, data: object):
doc_ref = self.db.collection(collection_name).document()
doc_ref.set(data)
def update_document(self, collection_name: str, data: object, doc_id: str):
doc_ref = self.db.collection(collection_name).document(doc_id)
doc_ref.update(data)
def delete_document(self, collection_name: str, doc_id: str):
doc_ref = self.db.collection(collection_name).document(doc_id)
doc_ref.delete()