-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcrud.py
77 lines (56 loc) · 1.9 KB
/
crud.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
from typing import Optional
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import CreateItem, CreateShop, Item, Shop
from .wordlists import animals
db = Database("ext_offlineshop")
async def create_shop(data: CreateShop) -> Shop:
data.wordlist = data.wordlist or "\n".join(animals)
shop = Shop(id=urlsafe_short_hash(), **data.dict())
await db.insert("offlineshop.shops", shop)
return shop
async def get_shop(shop_id: str) -> Optional[Shop]:
return await db.fetchone(
"SELECT * FROM offlineshop.shops WHERE id = :id",
{"id": shop_id},
Shop,
)
async def get_or_create_shop_by_wallet(wallet: str) -> Optional[Shop]:
shop = await db.fetchone(
"SELECT * FROM offlineshop.shops WHERE wallet = :wallet",
{"wallet": wallet},
Shop,
)
return shop or await create_shop(CreateShop(wallet=wallet))
async def update_shop(shop: Shop) -> Shop:
await db.update("offlineshop.shops", shop)
return shop
async def create_item(
shop: str,
data: CreateItem,
) -> Item:
item = Item(id=urlsafe_short_hash(), shop=shop, **data.dict())
await db.insert("offlineshop.items", item)
return item
async def update_item(item: Item) -> Item:
await db.update("offlineshop.items", item)
return item
async def get_item(item_id: str) -> Optional[Item]:
return await db.fetchone(
"SELECT * FROM offlineshop.items WHERE id = :id LIMIT 1",
{"id": item_id},
Item,
)
async def get_items(shop: str) -> list[Item]:
return await db.fetchall(
"SELECT * FROM offlineshop.items WHERE shop = :shop",
{"shop": shop},
Item,
)
async def delete_item_from_shop(shop: str, item_id: str):
await db.execute(
"""
DELETE FROM offlineshop.items WHERE shop = :shop AND id = :id
""",
{"shop": shop, "id": item_id},
)