Added paginated fetching to withdraw link table (#41)

This commit is contained in:
Julian 2024-07-19 07:22:46 +02:00 committed by GitHub
commit 0c77e691df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 50 additions and 23 deletions

33
crud.py
View file

@ -1,5 +1,5 @@
from datetime import datetime
from typing import List, Optional, Union
from typing import List, Optional, Tuple
import shortuuid
from lnbits.db import Database
@ -87,19 +87,32 @@ async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[Withdra
return WithdrawLink.parse_obj(link)
async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
async def get_withdraw_links(
wallet_ids: List[str], limit: int, offset: int
) -> Tuple[List[WithdrawLink], int]:
rows = await db.fetchall(
f"""
"""
SELECT * FROM withdraw.withdraw_link
WHERE wallet IN ({q}) ORDER BY open_time DESC
""",
WHERE wallet IN ({})
ORDER BY open_time DESC
LIMIT ? OFFSET ?
""".format(
",".join("?" * len(wallet_ids))
),
(*wallet_ids, limit, offset),
)
total = await db.fetchone(
"""
SELECT COUNT(*) as total FROM withdraw.withdraw_link
WHERE wallet IN ({})
""".format(
",".join("?" * len(wallet_ids))
),
(*wallet_ids,),
)
return [WithdrawLink(**row) for row in rows]
return [WithdrawLink(**row) for row in rows], total["total"]
async def remove_unique_withdraw_link(link: WithdrawLink, unique_hash: str) -> None:

View file

@ -59,7 +59,9 @@ new Vue({
{name: 'max', align: 'right', label: 'Max (sat)', field: 'max_fsat'}
],
pagination: {
rowsPerPage: 10
page: 1,
rowsPerPage: 10,
rowsNumber: 0
}
},
nfcTagWriting: false,
@ -97,19 +99,30 @@ new Vue({
}
},
methods: {
getWithdrawLinks: function () {
getWithdrawLinks: function (props) {
if (props) {
this.withdrawLinksTable.pagination = props.pagination
}
let pagination = this.withdrawLinksTable.pagination
const query = {
limit: pagination.rowsPerPage,
offset: (pagination.page - 1) * pagination.rowsPerPage
}
var self = this
LNbits.api
.request(
'GET',
'/withdraw/api/v1/links?all_wallets=true',
`/withdraw/api/v1/links?all_wallets=true&limit=${query.limit}&offset=${query.offset}`,
this.g.user.wallets[0].inkey
)
.then(function (response) {
self.withdrawLinks = response.data.map(function (obj) {
self.withdrawLinks = response.data.data.map(function (obj) {
return mapWithdrawLink(obj)
})
self.withdrawLinksTable.pagination.rowsNumber = response.data.total
})
.catch(function (error) {
clearInterval(self.checker)
@ -309,11 +322,8 @@ new Vue({
},
created: function () {
if (this.g.user.wallets.length) {
var getWithdrawLinks = this.getWithdrawLinks
getWithdrawLinks()
this.checker = setInterval(function () {
getWithdrawLinks()
}, 300000)
this.getWithdrawLinks()
this.checker = setInterval(this.getWithdrawLinks, 300000)
}
}
})

View file

@ -32,6 +32,7 @@
row-key="id"
:columns="withdrawLinksTable.columns"
:pagination.sync="withdrawLinksTable.pagination"
@request="getWithdrawLinks"
>
{% raw %}
<template v-slot:header="props">

View file

@ -25,6 +25,8 @@ async def api_links(
req: Request,
wallet: WalletTypeInfo = Depends(get_key_type),
all_wallets: bool = Query(False),
offset: int = Query(0),
limit: int = Query(0),
):
wallet_ids = [wallet.wallet.id]
@ -33,10 +35,11 @@ async def api_links(
wallet_ids = user.wallet_ids if user else []
try:
return [
{**link.dict(), **{"lnurl": link.lnurl(req)}}
for link in await get_withdraw_links(wallet_ids)
]
links, total = await get_withdraw_links(wallet_ids, limit, offset)
return {
"data": [{**link.dict(), **{"lnurl": link.lnurl(req)}} for link in links],
"total": total,
}
except LnurlInvalidUrl as exc:
raise HTTPException(