-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Vlad Stan <[email protected]>
- Loading branch information
Showing
12 changed files
with
1,598 additions
and
1,485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,95 @@ | ||
from typing import Optional | ||
|
||
from lnbits.db import SQLITE, Database | ||
from lnbits.db import Database | ||
from lnbits.helpers import urlsafe_short_hash | ||
|
||
from .models import CreateTipJar, Tip, TipJar | ||
|
||
db = Database("ext_tipjar") | ||
|
||
|
||
async def create_tip( | ||
tip_id: str, wallet: str, message: str, name: str, sats: int, tipjar: str | ||
) -> Tip: | ||
async def create_tip(tip: Tip) -> Tip: | ||
"""Create a new Tip""" | ||
await db.execute( | ||
""" | ||
INSERT INTO tipjar.Tips ( | ||
id, | ||
wallet, | ||
name, | ||
message, | ||
sats, | ||
tipjar | ||
) | ||
VALUES (?, ?, ?, ?, ?, ?) | ||
""", | ||
(tip_id, wallet, name, message, sats, tipjar), | ||
) | ||
|
||
tip = await get_tip(tip_id) | ||
assert tip, "Newly created tip couldn't be retrieved" | ||
await db.insert("tipjar.tip", tip) | ||
return tip | ||
|
||
|
||
async def create_tipjar(data: CreateTipJar) -> TipJar: | ||
"""Create a new TipJar""" | ||
|
||
returning = "" if db.type == SQLITE else "RETURNING ID" | ||
method = db.execute if db.type == SQLITE else db.fetchone | ||
|
||
result = await method( | ||
f""" | ||
INSERT INTO tipjar.TipJars ( | ||
name, | ||
wallet, | ||
webhook, | ||
onchain, | ||
onchain_limit | ||
) | ||
VALUES (?, ?, ?, ?, ?) | ||
{returning} | ||
""", | ||
(data.name, data.wallet, data.webhook, data.onchain, data.onchain_limit), | ||
tipjar = TipJar( | ||
id=urlsafe_short_hash(), | ||
**data.dict(), | ||
) | ||
if db.type == SQLITE: | ||
tipjar_id = result._result_proxy.lastrowid | ||
else: | ||
tipjar_id = result[0] # type: ignore | ||
|
||
tipjar = await get_tipjar(tipjar_id) | ||
assert tipjar | ||
await db.insert("tipjar.tipjar", tipjar) | ||
return tipjar | ||
|
||
|
||
async def get_tipjar(tipjar_id: int) -> Optional[TipJar]: | ||
async def get_tipjar(tipjar_id: str) -> Optional[TipJar]: | ||
"""Return a tipjar by ID""" | ||
row = await db.fetchone("SELECT * FROM tipjar.TipJars WHERE id = ?", (tipjar_id,)) | ||
return TipJar(**row) if row else None | ||
return await db.fetchone( | ||
"SELECT * FROM tipjar.tipjar WHERE id = :id", | ||
{"id": tipjar_id}, | ||
TipJar, | ||
) | ||
|
||
|
||
async def get_tipjars(wallet_id: str) -> Optional[list]: | ||
"""Return all TipJars belonging assigned to the wallet_id""" | ||
rows = await db.fetchall( | ||
"SELECT * FROM tipjar.TipJars WHERE wallet = ?", (wallet_id,) | ||
return await db.fetchall( | ||
"SELECT * FROM tipjar.tipjar WHERE wallet = :wallet_id", | ||
{"wallet_id": wallet_id}, | ||
TipJar, | ||
) | ||
return [TipJar(**row) for row in rows] if rows else None | ||
|
||
|
||
async def delete_tipjar(tipjar_id: int) -> None: | ||
async def delete_tipjar(tipjar_id: str) -> None: | ||
"""Delete a TipJar and all corresponding Tips""" | ||
tips = await get_tipjar_tips(tipjar_id) | ||
for tip in tips: | ||
await delete_tip(tip.id) | ||
await db.execute("DELETE FROM tipjar.TipJars WHERE id = ?", (tipjar_id,)) | ||
await db.execute("DELETE FROM tipjar.tipjar WHERE id = :id", {"id": tipjar_id}) | ||
|
||
|
||
async def get_tip(tip_id: str) -> Optional[Tip]: | ||
"""Return a Tip""" | ||
row = await db.fetchone("SELECT * FROM tipjar.Tips WHERE id = ?", (tip_id,)) | ||
return Tip(**row) if row else None | ||
return await db.fetchone( | ||
"SELECT * FROM tipjar.tip WHERE id = :id", | ||
{"id": tip_id}, | ||
Tip, | ||
) | ||
|
||
|
||
async def get_tipjar_tips(tipjar_id: int) -> list[Tip]: | ||
async def get_tipjar_tips(tipjar_id: str) -> list[Tip]: | ||
"""Return all Tips for a tipjar""" | ||
rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE tipjar = ?", (tipjar_id,)) | ||
return [Tip(**row) for row in rows] | ||
return await db.fetchall( | ||
"SELECT * FROM tipjar.tip WHERE tipjar = :tipjar_id", | ||
{"tipjar_id": tipjar_id}, | ||
Tip, | ||
) | ||
|
||
|
||
async def get_tips(wallet_id: str) -> Optional[list]: | ||
async def get_tips(wallet_id: str) -> list[Tip]: | ||
"""Return all Tips assigned to wallet_id""" | ||
rows = await db.fetchall("SELECT * FROM tipjar.Tips WHERE wallet = ?", (wallet_id,)) | ||
return [Tip(**row) for row in rows] if rows else None | ||
return await db.fetchall( | ||
"SELECT * FROM tipjar.tip WHERE wallet = :wallet_id", | ||
{"wallet_id": wallet_id}, | ||
Tip, | ||
) | ||
|
||
|
||
async def delete_tip(tip_id: str) -> None: | ||
"""Delete a Tip and its corresponding statspay charge""" | ||
await db.execute("DELETE FROM tipjar.Tips WHERE id = ?", (tip_id,)) | ||
await db.execute("DELETE FROM tipjar.tip WHERE id = :id", {"id": tip_id}) | ||
|
||
|
||
async def update_tip(tip_id: str, **kwargs) -> Tip: | ||
async def update_tip(tip: Tip) -> Tip: | ||
"""Update a Tip""" | ||
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) | ||
await db.execute( | ||
f"UPDATE tipjar.Tips SET {q} WHERE id = ?", (*kwargs.values(), tip_id) | ||
) | ||
row = await db.fetchone("SELECT * FROM tipjar.Tips WHERE id = ?", (tip_id,)) | ||
assert row, "Newly updated tip couldn't be retrieved" | ||
return Tip(**row) | ||
await db.update("tipjar.tip", tip) | ||
return tip | ||
|
||
|
||
async def update_tipjar(tipjar_id: str, **kwargs) -> TipJar: | ||
async def update_tipjar(tipjar: TipJar) -> TipJar: | ||
"""Update a tipjar""" | ||
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) | ||
await db.execute( | ||
f"UPDATE tipjar.TipJars SET {q} WHERE id = ?", (*kwargs.values(), tipjar_id) | ||
) | ||
row = await db.fetchone("SELECT * FROM tipjar.TipJars WHERE id = ?", (tipjar_id,)) | ||
assert row, "Newly updated tipjar couldn't be retrieved" | ||
return TipJar(**row) | ||
await db.update("tipjar.tipjar", tipjar) | ||
return tipjar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.