Skip to content

Commit

Permalink
use shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Sep 29, 2024
1 parent 6027412 commit 347bbe3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
40 changes: 21 additions & 19 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from lnbits.db import Database
from lnbits.helpers import insert_query, update_query, urlsafe_short_hash
from lnbits.helpers import urlsafe_short_hash

from .models import CreateTipJar, Tip, TipJar

Expand All @@ -10,7 +10,7 @@

async def create_tip(tip: Tip) -> Tip:
"""Create a new Tip"""
await db.execute(insert_query("tipjar.tip", tip), tip.dict())
await db.insert("tipjar.tip", tip) # type: ignore
return tip


Expand All @@ -21,25 +21,25 @@ async def create_tipjar(data: CreateTipJar) -> TipJar:
id=urlsafe_short_hash(),
**data.dict(),
)
await db.execute(insert_query("tipjar.tipjar", tipjar), tipjar.dict())
await db.insert("tipjar.tipjar", tipjar) # type: ignore
return tipjar


async def get_tipjar(tipjar_id: str) -> Optional[TipJar]:
"""Return a tipjar by ID"""
row = await db.fetchone(
"SELECT * FROM tipjar.tipjar WHERE id = :id", {"id": tipjar_id}
return await db.fetchone(
"SELECT * FROM tipjar.tipjar WHERE id = :id", {"id": tipjar_id},
TipJar, # type: ignore
)
return TipJar(**row) if row else None


async def get_tipjars(wallet_id: str) -> Optional[list]:
"""Return all TipJars belonging assigned to the wallet_id"""
rows = await db.fetchall(
return await db.fetchall(
"SELECT * FROM tipjar.tipjar WHERE wallet = :wallet_id",
{"wallet_id": wallet_id},
TipJar, # type: ignore
)
return [TipJar(**row) for row in rows] if rows else None


async def delete_tipjar(tipjar_id: str) -> None:
Expand All @@ -52,24 +52,26 @@ async def delete_tipjar(tipjar_id: str) -> None:

async def get_tip(tip_id: str) -> Optional[Tip]:
"""Return a Tip"""
row = await db.fetchone("SELECT * FROM tipjar.tip WHERE id = :id", {"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, # type: ignore
)


async def get_tipjar_tips(tipjar_id: str) -> list[Tip]:
"""Return all Tips for a tipjar"""
rows = await db.fetchall(
"SELECT * FROM tipjar.tip WHERE tipjar = :tipjar_id", {"tipjar_id": tipjar_id}
return await db.fetchall(
"SELECT * FROM tipjar.tip WHERE tipjar = :tipjar_id", {"tipjar_id": tipjar_id},
Tip, # type: ignore
)
return [Tip(**row) for row in rows]


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.tip WHERE wallet = :wallet_id", {"wallet_id": wallet_id}
return await db.fetchall(
"SELECT * FROM tipjar.tip WHERE wallet = :wallet_id", {"wallet_id": wallet_id},
Tip, # type: ignore
)
return [Tip(**row) for row in rows] if rows else None


async def delete_tip(tip_id: str) -> None:
Expand All @@ -79,11 +81,11 @@ async def delete_tip(tip_id: str) -> None:

async def update_tip(tip: Tip) -> Tip:
"""Update a Tip"""
await db.execute(update_query("tipjar.tip", tip), tip.dict())
await db.update("tipjar.tip", tip) # type: ignore
return tip


async def update_tipjar(tipjar: TipJar) -> TipJar:
"""Update a tipjar"""
await db.execute(update_query("tipjar.tipjar", tipjar), tipjar.dict())
await db.update("tipjar.tipjar", tipjar) # type: ignore
return tipjar
2 changes: 1 addition & 1 deletion views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def tipjar_renderer():
@tipjar_generic_router.get("/")
async def index(request: Request, user: User = Depends(check_user_exists)):
return tipjar_renderer().TemplateResponse(
"tipjar/index.html", {"request": request, "user": user.dict()}
"tipjar/index.html", {"request": request, "user": user.json()}
)


Expand Down

0 comments on commit 347bbe3

Please sign in to comment.