lnbits-legend/lnbits/extensions/diagonalley/crud.py

308 lines
8.5 KiB
Python
Raw Normal View History

2022-01-27 15:26:55 +00:00
import re
2022-01-27 12:24:38 +00:00
from base64 import urlsafe_b64encode
from typing import List, Optional, Union
2022-01-27 15:26:55 +00:00
from uuid import uuid4
2022-01-27 12:24:38 +00:00
2022-01-27 15:26:55 +00:00
import httpx
2022-01-27 12:24:38 +00:00
# from lnbits.db import open_ext_db
from lnbits.db import SQLITE
from lnbits.helpers import urlsafe_short_hash
2022-01-27 15:26:55 +00:00
from lnbits.settings import WALLET
from . import db
2022-01-27 16:18:12 +00:00
from .models import (
2022-07-27 15:28:58 +01:00
Market,
2022-08-16 12:08:27 +01:00
OrderDetail,
2022-01-27 16:18:12 +00:00
Orders,
Products,
Stalls,
Zones,
2022-01-28 15:11:31 +00:00
createOrder,
2022-08-16 12:08:27 +01:00
createOrderDetails,
2022-01-27 16:18:12 +00:00
createProduct,
createStalls,
createZones,
)
2022-01-27 12:24:38 +00:00
###Products
2022-07-12 15:25:25 +01:00
async def create_diagonalley_product(data: createProduct) -> Products:
2022-01-27 12:24:38 +00:00
product_id = urlsafe_short_hash()
2022-01-27 15:26:55 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
2022-01-27 15:26:55 +00:00
INSERT INTO diagonalley.products (id, stall, product, categories, description, image, price, quantity)
2022-01-27 12:24:38 +00:00
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""",
(
product_id,
2022-01-27 15:26:55 +00:00
data.stall,
data.product,
data.categories,
data.description,
data.image,
data.price,
data.quantity,
2022-01-27 12:24:38 +00:00
),
)
product = await get_diagonalley_product(product_id)
assert product, "Newly created product couldn't be retrieved"
return product
async def update_diagonalley_product(product_id: str, **kwargs) -> Optional[Stalls]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE diagonalley.products SET {q} WHERE id = ?",
(*kwargs.values(), product_id),
)
row = await db.fetchone(
"SELECT * FROM diagonalley.products WHERE id = ?", (product_id,)
)
2022-07-15 10:22:13 +01:00
return Products(**row) if row else None
2022-01-27 12:24:38 +00:00
async def get_diagonalley_product(product_id: str) -> Optional[Products]:
row = await db.fetchone(
"SELECT * FROM diagonalley.products WHERE id = ?", (product_id,)
)
2022-01-28 15:11:31 +00:00
return Products(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-07-12 17:22:03 +01:00
async def get_diagonalley_products(stall_ids: Union[str, List[str]]) -> List[Products]:
if isinstance(stall_ids, str):
stall_ids = [stall_ids]
2022-01-27 12:24:38 +00:00
# with open_ext_db("diagonalley") as db:
2022-07-12 17:22:03 +01:00
q = ",".join(["?"] * len(stall_ids))
2022-01-27 12:24:38 +00:00
rows = await db.fetchall(
f"""
SELECT * FROM diagonalley.products WHERE stall IN ({q})
""",
2022-07-12 17:22:03 +01:00
(*stall_ids,),
2022-01-27 12:24:38 +00:00
)
2022-01-28 15:11:31 +00:00
return [Products(**row) for row in rows]
2022-01-27 12:24:38 +00:00
async def delete_diagonalley_product(product_id: str) -> None:
await db.execute("DELETE FROM diagonalley.products WHERE id = ?", (product_id,))
###zones
2022-07-12 15:25:25 +01:00
async def create_diagonalley_zone(user, data: createZones) -> Zones:
2022-01-27 12:24:38 +00:00
zone_id = urlsafe_short_hash()
2022-01-27 15:26:55 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
INSERT INTO diagonalley.zones (
id,
"user",
2022-01-27 12:24:38 +00:00
cost,
countries
)
VALUES (?, ?, ?, ?)
""",
2022-02-04 13:05:48 +00:00
(zone_id, user, data.cost, data.countries.lower()),
2022-01-27 12:24:38 +00:00
)
zone = await get_diagonalley_zone(zone_id)
assert zone, "Newly created zone couldn't be retrieved"
return zone
async def update_diagonalley_zone(zone_id: str, **kwargs) -> Optional[Zones]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE diagonalley.zones SET {q} WHERE id = ?",
(*kwargs.values(), zone_id),
)
row = await db.fetchone("SELECT * FROM diagonalley.zones WHERE id = ?", (zone_id,))
2022-01-28 15:11:31 +00:00
return Zones(**row) if row else None
2022-01-27 12:24:38 +00:00
async def get_diagonalley_zone(zone_id: str) -> Optional[Zones]:
row = await db.fetchone("SELECT * FROM diagonalley.zones WHERE id = ?", (zone_id,))
2022-01-28 15:11:31 +00:00
return Zones(**row) if row else None
2022-01-27 12:24:38 +00:00
2022-02-04 13:05:48 +00:00
async def get_diagonalley_zones(user: str) -> List[Zones]:
2022-07-12 15:25:25 +01:00
rows = await db.fetchall(
'SELECT * FROM diagonalley.zones WHERE "user" = ?', (user,)
)
2022-01-28 15:11:31 +00:00
return [Zones(**row) for row in rows]
2022-01-27 12:24:38 +00:00
async def delete_diagonalley_zone(zone_id: str) -> None:
await db.execute("DELETE FROM diagonalley.zones WHERE id = ?", (zone_id,))
###Stalls
2022-07-12 15:25:25 +01:00
async def create_diagonalley_stall(data: createStalls) -> Stalls:
2022-01-27 12:24:38 +00:00
stall_id = urlsafe_short_hash()
2022-01-27 16:18:12 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
INSERT INTO diagonalley.stalls (
id,
wallet,
name,
publickey,
privatekey,
relays,
shippingzones
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
2022-01-27 16:18:12 +00:00
(
stall_id,
data.wallet,
data.name,
data.publickey,
data.privatekey,
data.relays,
2022-07-12 15:25:25 +01:00
data.shippingzones,
),
2022-01-27 12:24:38 +00:00
)
stall = await get_diagonalley_stall(stall_id)
assert stall, "Newly created stall couldn't be retrieved"
return stall
async def update_diagonalley_stall(stall_id: str, **kwargs) -> Optional[Stalls]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE diagonalley.stalls SET {q} WHERE id = ?",
(*kwargs.values(), stall_id),
)
row = await db.fetchone(
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
)
2022-01-28 15:11:31 +00:00
return Stalls(**row) if row else None
2022-01-27 12:24:38 +00:00
async def get_diagonalley_stall(stall_id: str) -> Optional[Stalls]:
row = await db.fetchone(
"SELECT * FROM diagonalley.stalls WHERE id = ?", (stall_id,)
)
2022-07-15 10:22:13 +01:00
print("ROW", row)
2022-01-28 15:11:31 +00:00
return Stalls(**row) if row else None
2022-01-27 12:24:38 +00:00
async def get_diagonalley_stalls(wallet_ids: Union[str, List[str]]) -> List[Stalls]:
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM diagonalley.stalls WHERE wallet IN ({q})", (*wallet_ids,)
)
2022-01-28 15:11:31 +00:00
return [Stalls(**row) for row in rows]
2022-01-27 12:24:38 +00:00
async def delete_diagonalley_stall(stall_id: str) -> None:
await db.execute("DELETE FROM diagonalley.stalls WHERE id = ?", (stall_id,))
###Orders
2022-08-16 12:08:27 +01:00
async def create_diagonalley_order(wallet_id: str, data: createOrder) -> Orders:
2022-01-27 12:24:38 +00:00
order_id = urlsafe_short_hash()
2022-01-28 15:11:31 +00:00
await db.execute(
2022-01-27 12:24:38 +00:00
f"""
2022-08-16 12:08:27 +01:00
INSERT INTO diagonalley.orders (id, wallet, shippingzone, address, email, total, invoiceid, paid, shipped)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
2022-01-27 12:24:38 +00:00
""",
(
order_id,
2022-08-16 12:08:27 +01:00
wallet_id,
2022-01-28 15:11:31 +00:00
data.shippingzone,
data.address,
data.email,
2022-08-16 12:08:27 +01:00
data.total,
2022-01-28 15:11:31 +00:00
data.invoiceid,
2022-01-27 12:24:38 +00:00
False,
False,
),
)
2022-08-16 12:08:27 +01:00
2022-01-27 12:24:38 +00:00
link = await get_diagonalley_order(order_id)
assert link, "Newly created link couldn't be retrieved"
return link
2022-08-16 12:08:27 +01:00
async def create_diagonalley_order_details(order_id: str, data: List[createOrderDetails]):
for item in data:
item_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO diagonalley.order_details (id, orderid, productid, quantity)
VALUES (?, ?, ?, ?)
""",
(
item_id,
order_id,
item.product_id,
item.quantity,
),
)
order_details = await get_diagonalley_order_details(order_id)
return order_details
async def get_diagonalley_order_details(order_id: str) -> List[OrderDetail]:
rows = await db.fetchall(
f"SELECT * FROM diagonalley.order_details WHERE order_id = ?", (order_id,)
)
return [OrderDetail(**row) for row in rows]
2022-01-27 12:24:38 +00:00
async def get_diagonalley_order(order_id: str) -> Optional[Orders]:
row = await db.fetchone(
"SELECT * FROM diagonalley.orders WHERE id = ?", (order_id,)
)
2022-01-28 15:11:31 +00:00
return Orders(**row) if row else None
2022-01-27 12:24:38 +00:00
async def get_diagonalley_orders(wallet_ids: Union[str, List[str]]) -> List[Orders]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM diagonalley.orders WHERE wallet IN ({q})", (*wallet_ids,)
)
#
2022-01-28 15:11:31 +00:00
return [Orders(**row) for row in rows]
2022-01-27 12:24:38 +00:00
async def delete_diagonalley_order(order_id: str) -> None:
await db.execute("DELETE FROM diagonalley.orders WHERE id = ?", (order_id,))
2022-07-27 15:28:58 +01:00
### Market/Marketplace
async def get_diagonalley_markets(user: str) -> List[Market]:
rows = await db.fetchall(
'SELECT * FROM diagonalley.markets WHERE usr = ?', (user,)
)
return [Market(**row) for row in rows]
async def get_diagonalley_market(market_id: str) -> Optional[Market]:
row = await db.fetchone(
'SELECT * FROM diagonalley.markets WHERE id = ?', (market_id,)
)
Market(**row) if row else None
async def get_diagonalley_market_stalls(market_id: str):
rows = await db.fetchall(
"SELECT * FROM diagonalley.market_stalls WHERE marketid = ?", (market_id,)
)
return [Stalls(**row) for row in rows]