Skip to content

Commit

Permalink
fix: finish refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Nov 4, 2024
1 parent 62b4d47 commit a761a1c
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 599 deletions.
15 changes: 9 additions & 6 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from typing import Optional

from lnbits.db import Database
Expand All @@ -7,7 +8,6 @@
Bitcoinswitch,
BitcoinswitchPayment,
CreateBitcoinswitch,
Switches,
)

db = Database("ext_bitcoinswitch")
Expand All @@ -24,13 +24,14 @@ async def create_bitcoinswitch(
title=data.title,
wallet=data.wallet,
currency=data.currency,
switches=Switches(switches=data.switches),
switches=data.switches,
)
await db.insert("bitcoinswitch.switch", device)
return device


async def update_bitcoinswitch(device: Bitcoinswitch) -> Bitcoinswitch:
device.updated_at = datetime.now(timezone.utc)
await db.update("bitcoinswitch.switch", device)
return device

Expand Down Expand Up @@ -77,15 +78,16 @@ async def create_bitcoinswitch_payment(
payment_hash=payment_hash,
sats=amount_msat,
)
await db.update("bitcoinswitch.payment", payment)
await db.insert("bitcoinswitch.payment", payment)
return payment


async def update_bitcoinswitch_payment(
bitcoinswitchpayment: BitcoinswitchPayment,
bitcoinswitch_payment: BitcoinswitchPayment,
) -> BitcoinswitchPayment:
await db.update("bitcoinswitch.payment", bitcoinswitchpayment)
return bitcoinswitchpayment
bitcoinswitch_payment.updated_at = datetime.now(timezone.utc)
await db.update("bitcoinswitch.payment", bitcoinswitch_payment)
return bitcoinswitch_payment


async def delete_bitcoinswitch_payment(bitcoinswitch_payment_id: str) -> None:
Expand All @@ -101,6 +103,7 @@ async def get_bitcoinswitch_payment(
return await db.fetchone(
"SELECT * FROM bitcoinswitch.payment WHERE id = :id",
{"id": bitcoinswitchpayment_id},
BitcoinswitchPayment,
)


Expand Down
6 changes: 4 additions & 2 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async def m001_initial(db):
wallet TEXT NOT NULL,
currency TEXT NOT NULL,
switches TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
updated_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
);
"""
)
Expand All @@ -29,7 +30,8 @@ async def m001_initial(db):
payload TEXT NOT NULL,
pin INT,
sats {db.big_int},
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
created_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
updated_at TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
);
"""
)
7 changes: 2 additions & 5 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,13 @@ class CreateBitcoinswitch(BaseModel):
switches: list[Switch]


class Switches(BaseModel):
switches: list[Switch]


class Bitcoinswitch(BaseModel):
id: str
title: str
wallet: str
currency: str
key: str
switches: Switches
switches: list[Switch]
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))

Expand All @@ -64,3 +60,4 @@ class BitcoinswitchPayment(BaseModel):
pin: int
sats: int
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
Loading

0 comments on commit a761a1c

Please sign in to comment.