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 (
|
|
|
|
Orders,
|
|
|
|
Products,
|
|
|
|
Stalls,
|
|
|
|
Zones,
|
2022-01-28 15:11:31 +00:00
|
|
|
createOrder,
|
2022-01-27 16:18:12 +00:00
|
|
|
createProduct,
|
|
|
|
createStalls,
|
|
|
|
createZones,
|
|
|
|
)
|
2022-01-27 12:24:38 +00:00
|
|
|
|
|
|
|
###Products
|
|
|
|
|
|
|
|
async def create_diagonalley_product(
|
2022-01-27 15:26:55 +00:00
|
|
|
data: createProduct
|
2022-01-27 12:24:38 +00:00
|
|
|
) -> Products:
|
|
|
|
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()])
|
|
|
|
|
|
|
|
# with open_ext_db("diagonalley") as db:
|
|
|
|
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,)
|
|
|
|
)
|
|
|
|
|
|
|
|
return get_diagonalley_stall(product_id)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
async def get_diagonalley_products(wallet_ids: Union[str, List[str]]) -> List[Products]:
|
|
|
|
if isinstance(wallet_ids, str):
|
|
|
|
wallet_ids = [wallet_ids]
|
|
|
|
|
|
|
|
# with open_ext_db("diagonalley") as db:
|
|
|
|
q = ",".join(["?"] * len(wallet_ids))
|
|
|
|
rows = await db.fetchall(
|
|
|
|
f"""
|
|
|
|
SELECT * FROM diagonalley.products WHERE stall IN ({q})
|
|
|
|
""",
|
|
|
|
(*wallet_ids,),
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
|
async def create_diagonalley_zone(
|
2022-02-04 13:05:48 +00:00
|
|
|
user,
|
2022-01-27 15:26:55 +00:00
|
|
|
data: createZones
|
2022-01-27 12:24:38 +00:00
|
|
|
) -> Zones:
|
|
|
|
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,
|
2022-02-04 13:05:48 +00:00
|
|
|
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]:
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
async def create_diagonalley_stall(
|
2022-01-27 16:18:12 +00:00
|
|
|
data: createStalls
|
2022-01-27 12:24:38 +00:00
|
|
|
) -> Stalls:
|
|
|
|
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-02-04 13:05:48 +00:00
|
|
|
repr(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-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
|
|
|
|
|
|
|
|
|
|
|
|
async def create_diagonalley_order(
|
2022-01-28 15:11:31 +00:00
|
|
|
data: createOrder
|
2022-01-27 12:24:38 +00:00
|
|
|
) -> Orders:
|
|
|
|
|
|
|
|
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"""
|
|
|
|
INSERT INTO diagonalley.orders (id, productid, wallet, product,
|
|
|
|
quantity, shippingzone, address, email, invoiceid, paid, shipped)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
""",
|
|
|
|
(
|
|
|
|
order_id,
|
2022-01-28 15:11:31 +00:00
|
|
|
data.productid,
|
|
|
|
data.wallet,
|
|
|
|
data.product,
|
|
|
|
data.quantity,
|
|
|
|
data.shippingzone,
|
|
|
|
data.address,
|
|
|
|
data.email,
|
|
|
|
data.invoiceid,
|
2022-01-27 12:24:38 +00:00
|
|
|
False,
|
|
|
|
False,
|
|
|
|
),
|
|
|
|
)
|
2022-01-28 15:11:31 +00:00
|
|
|
# if db.type == SQLITE:
|
|
|
|
# order_id = result._result_proxy.lastrowid
|
|
|
|
# else:
|
|
|
|
# order_id = result[0]
|
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
|
|
|
|
|
|
|
|
|
|
|
|
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,))
|