-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappwrite_backend.py
104 lines (73 loc) · 2.57 KB
/
appwrite_backend.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
from secrets import PROJECT_API_KEY
from typing import Optional
from appwrite.client import Client
from appwrite.services.database import Database
from config import get_config
from models import (
CoffeeBag,
CoffeeBagDocument,
CoffeeBeanRoast,
CoffeeCup,
CoffeeCupDocument,
)
client = Client()
_config = get_config()
(
client.set_endpoint(_config.appwrite.api_endpoint)
.set_project(_config.appwrite.project_id)
.set_key(PROJECT_API_KEY)
)
def _get_database() -> Database:
db = Database(client)
return db
def _get_coffee_bag_collections_id() -> str:
return _config.appwrite.collections.coffee_bag_collection_id
def _get_coffee_cup_collections_id() -> str:
return _config.appwrite.collections.coffee_cup_collection_id
# ---- Coffee Bags ----
def get_coffee_bags(
brand: Optional[str], active: Optional[bool], roast: Optional[CoffeeBeanRoast]
) -> list[CoffeeBagDocument]:
db = _get_database()
filters = []
if brand is not None:
filters.append(f"brand={brand}")
if active is not None:
filters.append(f"active={int(active)}")
if roast is not None:
filters.append(f"roast={roast}")
print(filters)
res = db.list_documents(_get_coffee_bag_collections_id(), filters=filters)
return [CoffeeBagDocument(**info) for info in res["documents"]]
def get_coffee_bag(id: str) -> CoffeeBagDocument:
db = _get_database()
res = db.get_document(
collection_id=_get_coffee_bag_collections_id(), document_id=id
)
return CoffeeBagDocument(**res)
def add_coffee_bag(coffee_bag: CoffeeBag) -> CoffeeBagDocument:
db = _get_database()
res = db.create_document(
collection_id=_get_coffee_bag_collections_id(), data=coffee_bag.json()
)
return CoffeeBagDocument(**res)
# ---- Coffee Cups ----
def get_coffee_cups(bag_id: Optional[str]) -> list[CoffeeCupDocument]:
db = _get_database()
filters = []
if bag_id is not None:
filters.append(f"bag_id={bag_id}")
res = db.list_documents(_get_coffee_cup_collections_id(), filters=filters)
return [CoffeeCupDocument(**info) for info in res["documents"]]
def get_coffee_cup(id: str) -> CoffeeCupDocument:
db = _get_database()
res = db.get_document(
collection_id=_get_coffee_cup_collections_id(), document_id=id
)
return CoffeeCupDocument(**res)
def add_coffee_cup(coffee_cup: CoffeeCup) -> CoffeeCupDocument:
db = _get_database()
res = db.create_document(
collection_id=_get_coffee_cup_collections_id(), data=coffee_cup.json()
)
return CoffeeCupDocument(**res)