v1 in the middle (#32)
This commit is contained in:
parent
a8eb139360
commit
db20915756
14 changed files with 1259 additions and 1127 deletions
75
crud.py
75
crud.py
|
|
@ -1,59 +1,54 @@
|
|||
import json
|
||||
from typing import Optional
|
||||
|
||||
from lnbits.db import Database
|
||||
|
||||
from .models import Config, Relay
|
||||
from .models import Config, Relay, UserConfig
|
||||
|
||||
db = Database("ext_nostrclient")
|
||||
|
||||
|
||||
async def get_relays() -> list[Relay]:
|
||||
rows = await db.fetchall("SELECT * FROM nostrclient.relays")
|
||||
return [Relay.from_row(r) for r in rows]
|
||||
|
||||
|
||||
async def add_relay(relay: Relay) -> None:
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO nostrclient.relays (
|
||||
id,
|
||||
url,
|
||||
active
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(relay.id, relay.url, relay.active),
|
||||
return await db.fetchall(
|
||||
"SELECT * FROM nostrclient.relays",
|
||||
model=Relay,
|
||||
)
|
||||
|
||||
|
||||
async def add_relay(relay: Relay) -> Relay:
|
||||
await db.insert("nostrclient.relays", relay)
|
||||
return relay
|
||||
|
||||
|
||||
async def delete_relay(relay: Relay) -> None:
|
||||
await db.execute("DELETE FROM nostrclient.relays WHERE url = ?", (relay.url,))
|
||||
if not relay.url:
|
||||
return
|
||||
await db.execute(
|
||||
"DELETE FROM nostrclient.relays WHERE url = :url", {"url": relay.url}
|
||||
)
|
||||
|
||||
|
||||
######################CONFIG#######################
|
||||
async def create_config() -> Config:
|
||||
config = Config()
|
||||
await db.execute(
|
||||
async def create_config(owner_id: str) -> Config:
|
||||
admin_config = UserConfig(owner_id=owner_id)
|
||||
await db.insert("nostrclient.config", admin_config)
|
||||
return admin_config.extra
|
||||
|
||||
|
||||
async def update_config(owner_id: str, config: Config) -> Config:
|
||||
user_config = UserConfig(owner_id=owner_id, extra=config)
|
||||
await db.update("nostrclient.config", user_config, "WHERE owner_id = :owner_id")
|
||||
return user_config.extra
|
||||
|
||||
|
||||
async def get_config(owner_id: str) -> Optional[Config]:
|
||||
user_config: UserConfig = await db.fetchone(
|
||||
"""
|
||||
INSERT INTO nostrclient.config (json_data)
|
||||
VALUES (?)
|
||||
SELECT * FROM nostrclient.config
|
||||
WHERE owner_id = :owner_id
|
||||
""",
|
||||
(json.dumps(config.dict()),),
|
||||
{"owner_id": owner_id},
|
||||
model=UserConfig,
|
||||
)
|
||||
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
|
||||
return json.loads(row[0], object_hook=lambda d: Config(**d))
|
||||
|
||||
|
||||
async def update_config(config: Config) -> Optional[Config]:
|
||||
await db.execute(
|
||||
"""UPDATE nostrclient.config SET json_data = ?""",
|
||||
(json.dumps(config.dict()),),
|
||||
)
|
||||
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
|
||||
return json.loads(row[0], object_hook=lambda d: Config(**d))
|
||||
|
||||
|
||||
async def get_config() -> Optional[Config]:
|
||||
row = await db.fetchone("SELECT json_data FROM nostrclient.config", ())
|
||||
return json.loads(row[0], object_hook=lambda d: Config(**d)) if row else None
|
||||
if user_config:
|
||||
return user_config.extra
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue