Simplifies database queries and updates auth

Removes the `castle.` prefix from database table names in queries, streamlining data access.

Updates authentication to use `WalletTypeInfo` dependency injection for retrieving wallet information. This improves security and aligns with LNBits' authentication patterns. Also modifies the main router's tag to uppercase.
This commit is contained in:
padreug 2025-10-22 12:52:52 +02:00
parent cdd0cda001
commit 5589d813f0
3 changed files with 39 additions and 35 deletions

30
crud.py
View file

@ -31,13 +31,13 @@ async def create_account(data: CreateAccount) -> Account:
user_id=data.user_id,
created_at=datetime.now(),
)
await db.insert("castle.accounts", account)
await db.insert("accounts", account)
return account
async def get_account(account_id: str) -> Optional[Account]:
return await db.fetchone(
"SELECT * FROM castle.accounts WHERE id = :id",
"SELECT * FROM accounts WHERE id = :id",
{"id": account_id},
Account,
)
@ -45,7 +45,7 @@ async def get_account(account_id: str) -> Optional[Account]:
async def get_account_by_name(name: str) -> Optional[Account]:
return await db.fetchone(
"SELECT * FROM castle.accounts WHERE name = :name",
"SELECT * FROM accounts WHERE name = :name",
{"name": name},
Account,
)
@ -53,14 +53,14 @@ async def get_account_by_name(name: str) -> Optional[Account]:
async def get_all_accounts() -> list[Account]:
return await db.fetchall(
"SELECT * FROM castle.accounts ORDER BY account_type, name",
"SELECT * FROM accounts ORDER BY account_type, name",
model=Account,
)
async def get_accounts_by_type(account_type: AccountType) -> list[Account]:
return await db.fetchall(
"SELECT * FROM castle.accounts WHERE account_type = :type ORDER BY name",
"SELECT * FROM accounts WHERE account_type = :type ORDER BY name",
{"type": account_type.value},
Account,
)
@ -74,7 +74,7 @@ async def get_or_create_user_account(
account = await db.fetchone(
"""
SELECT * FROM castle.accounts
SELECT * FROM accounts
WHERE user_id = :user_id AND account_type = :type AND name = :name
""",
{"user_id": user_id, "type": account_type.value, "name": account_name},
@ -123,7 +123,7 @@ async def create_journal_entry(
lines=[],
)
await db.insert("castle.journal_entries", journal_entry)
await db.insert("journal_entries", journal_entry)
# Create entry lines
lines = []
@ -137,7 +137,7 @@ async def create_journal_entry(
credit=line_data.credit,
description=line_data.description,
)
await db.insert("castle.entry_lines", line)
await db.insert("entry_lines", line)
lines.append(line)
journal_entry.lines = lines
@ -146,7 +146,7 @@ async def create_journal_entry(
async def get_journal_entry(entry_id: str) -> Optional[JournalEntry]:
entry = await db.fetchone(
"SELECT * FROM castle.journal_entries WHERE id = :id",
"SELECT * FROM journal_entries WHERE id = :id",
{"id": entry_id},
JournalEntry,
)
@ -159,7 +159,7 @@ async def get_journal_entry(entry_id: str) -> Optional[JournalEntry]:
async def get_entry_lines(journal_entry_id: str) -> list[EntryLine]:
return await db.fetchall(
"SELECT * FROM castle.entry_lines WHERE journal_entry_id = :id",
"SELECT * FROM entry_lines WHERE journal_entry_id = :id",
{"id": journal_entry_id},
EntryLine,
)
@ -168,7 +168,7 @@ async def get_entry_lines(journal_entry_id: str) -> list[EntryLine]:
async def get_all_journal_entries(limit: int = 100) -> list[JournalEntry]:
entries = await db.fetchall(
"""
SELECT * FROM castle.journal_entries
SELECT * FROM journal_entries
ORDER BY entry_date DESC, created_at DESC
LIMIT :limit
""",
@ -187,7 +187,7 @@ async def get_journal_entries_by_user(
) -> list[JournalEntry]:
entries = await db.fetchall(
"""
SELECT * FROM castle.journal_entries
SELECT * FROM journal_entries
WHERE created_by = :user_id
ORDER BY entry_date DESC, created_at DESC
LIMIT :limit
@ -212,7 +212,7 @@ async def get_account_balance(account_id: str) -> int:
SELECT
COALESCE(SUM(debit), 0) as total_debit,
COALESCE(SUM(credit), 0) as total_credit
FROM castle.entry_lines
FROM entry_lines
WHERE account_id = :id
""",
{"id": account_id},
@ -241,7 +241,7 @@ async def get_user_balance(user_id: str) -> UserBalance:
"""Get user's balance with the Castle (positive = castle owes user, negative = user owes castle)"""
# Get all user-specific accounts
user_accounts = await db.fetchall(
"SELECT * FROM castle.accounts WHERE user_id = :user_id",
"SELECT * FROM accounts WHERE user_id = :user_id",
{"user_id": user_id},
Account,
)
@ -272,7 +272,7 @@ async def get_account_transactions(
"""Get all transactions affecting a specific account"""
lines = await db.fetchall(
"""
SELECT * FROM castle.entry_lines
SELECT * FROM entry_lines
WHERE account_id = :id
ORDER BY id DESC
LIMIT :limit