Add donation getter endpoint

This commit is contained in:
Fitti
2021-06-28 09:06:13 +02:00
parent f70eac2a48
commit 3f353a6eae
2 changed files with 34 additions and 1 deletions

View File

@@ -161,6 +161,14 @@ async def get_service(service_id: int) -> Optional[Service]:
return Service.from_row(row) if row else None
async def get_services(wallet_id: str) -> Optional[list]:
rows = await db.fetchall(
"SELECT * FROM Services WHERE wallet = ?",
(wallet_id,)
)
return [Service.from_row(row) for row in rows] if rows else None
async def authenticate_service(service_id, code, redirect_uri):
# The API token is passed in the querystring as 'code'
service = await get_service(service_id)
@@ -214,6 +222,18 @@ async def get_donation(donation_id: str) -> Optional[Donation]:
return Donation.from_row(row) if row else None
async def get_donations(wallet_id: str) -> Optional[list]:
services = await get_services(wallet_id)
service_ids = [service.id for service in services]
rows = []
for service_id in service_ids:
rows.append(await db.fetchall(
"SELECT * FROM Donations WHERE service = ?",
(service_id,)
))
return [Donation.from_row(row) for row in rows] if rows else None
async def delete_donation(donation_id: str) -> None:
await db.execute(
"DELETE FROM Donations WHERE id = ?",

View File

@@ -2,13 +2,14 @@ from quart import g, redirect, request, jsonify
from http import HTTPStatus
from lnbits.decorators import api_validate_post_request, api_check_wallet_key
from lnbits.core.crud import get_wallet
from lnbits.core.crud import get_wallet, get_user
from . import twitchalerts_ext
from .crud import (
get_charge_details,
create_donation,
post_donation,
get_donations,
create_service,
get_service,
authenticate_service
@@ -142,3 +143,15 @@ async def api_post_donation():
jsonify({"message": "Not a paid charge!"}),
HTTPStatus.BAD_REQUEST
)
@twitchalerts_ext.route("/api/v1/donations", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_get_donations():
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
return (
jsonify([
donation._asdict() for donation in await get_donations(wallet_ids)
]),
HTTPStatus.OK,
)