Merge pull request #1354 from joelklabo/make-nip5-editable

Enabled Editing the Price for NIP-5 Domains
This commit is contained in:
calle 2023-01-12 15:45:27 +01:00 committed by GitHub
commit 3e9bb25b76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 121 additions and 2 deletions

View File

@ -3,7 +3,7 @@ from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import Address, CreateAddressData, CreateDomainData, Domain
from .models import Address, CreateAddressData, CreateDomainData, Domain, EditDomainData
async def get_domain(domain_id: str) -> Optional[Domain]:
@ -170,6 +170,26 @@ async def create_address_internal(domain_id: str, data: CreateAddressData) -> Ad
return address
async def update_domain_internal(wallet_id: str, data: EditDomainData) -> Domain:
if data.currency != "Satoshis":
amount = data.amount * 100
else:
amount = data.amount
print(data)
await db.execute(
"""
UPDATE nostrnip5.domains
SET amount = ?, currency = ?
WHERE id = ?
""",
(int(amount), data.currency, data.id),
)
domain = await get_domain(data.id)
assert domain, "Domain couldn't be updated"
return domain
async def create_domain_internal(wallet_id: str, data: CreateDomainData) -> Domain:
domain_id = urlsafe_short_hash()

View File

@ -24,6 +24,16 @@ class CreateDomainData(BaseModel):
domain: str
class EditDomainData(BaseModel):
id: str
currency: str
amount: float = Query(..., ge=0.01)
@classmethod
def from_row(cls, row: Row) -> "EditDomainData":
return cls(**dict(row))
class Domain(BaseModel):
id: str
wallet: str

View File

@ -73,6 +73,14 @@
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
@click="deleteDomain(props.row.id)"
></q-btn>
<q-btn
unelevated
dense
size="xs"
icon="edit"
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
@click="editDomain(props.row.id)"
></q-btn>
</q-td>
<q-td v-for="col in props.cols" :key="col.name" :props="props">
{{ col.value }}
@ -226,6 +234,39 @@
</q-form>
</q-card>
</q-dialog>
<q-dialog
v-model="editFormDialog.show"
position="top"
@hide="closeFormDialog"
>
<q-card class="q-pa-lg q-pt-xl" style="width: 500px">
<q-form @submit="saveEditedDomain" class="q-gutter-md">
<q-select
filled
dense
emit-value
v-model="editFormDialog.data.currency"
:options="currencyOptions"
label="Currency *"
></q-select>
<q-input
filled
dense
v-model.trim="editFormDialog.data.amount"
label="Amount"
placeholder="How much do you want to charge?"
></q-input>
<div class="row q-mt-lg">
<q-btn unelevated color="primary" type="submit">Update Amount</q-btn>
<q-btn v-close-popup flat color="grey" class="q-ml-auto"
>Cancel</q-btn
>
</div>
</q-form>
</q-card>
</q-dialog>
<q-dialog
v-model="addressFormDialog.show"
position="top"
@ -513,6 +554,10 @@
show: false,
data: {}
},
editFormDialog: {
show: false,
data: {}
},
addressFormDialog: {
show: false,
data: {}
@ -578,6 +623,34 @@
LNbits.utils.notifyApiError(error)
})
},
saveEditedDomain: function () {
var data = this.editFormDialog.data
var self = this
LNbits.api
.request(
'PUT',
'/nostrnip5/api/v1/domain',
_.findWhere(this.g.user.wallets, {
id: this.editFormDialog.data.wallet
}).inkey,
data
)
.then(function (response) {
self.editFormDialog.show = false
self.editFormDialog.data = {}
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
})
},
editDomain: function (domain_id) {
var self = this
var data = _.findWhere(this.domains, {id: domain_id})
self.editFormDialog.show = true
self.editFormDialog.data = data
},
deleteDomain: function (domain_id) {
var self = this
var domain = _.findWhere(this.domains, {id: domain_id})

View File

@ -26,8 +26,14 @@ from .crud import (
get_domain_by_name,
get_domains,
rotate_address,
update_domain_internal,
)
from .models import (
CreateAddressData,
CreateDomainData,
EditDomainData,
RotateAddressData,
)
from .models import CreateAddressData, CreateDomainData, RotateAddressData
@nostrnip5_ext.get("/api/v1/domains", status_code=HTTPStatus.OK)
@ -89,6 +95,16 @@ async def api_domain_create(
return domain
@nostrnip5_ext.put("/api/v1/domain", status_code=HTTPStatus.OK)
async def api_domain_update(
data: EditDomainData, wallet: WalletTypeInfo = Depends(get_key_type)
):
domain = await update_domain_internal(wallet_id=wallet.wallet.id, data=data)
return domain
@nostrnip5_ext.delete("/api/v1/domain/{domain_id}", status_code=HTTPStatus.CREATED)
async def api_domain_delete(
domain_id: str,