From 307a919d179a0b456afbcc0db9d0793eab6dfa76 Mon Sep 17 00:00:00 2001 From: Kristjan Date: Mon, 28 Dec 2020 22:32:04 +0100 Subject: [PATCH] added CRUD operations --- lnbits/extensions/subdomains/crud.py | 140 ++++++++++++++++++ lnbits/extensions/subdomains/migrations.py | 4 +- lnbits/extensions/subdomains/models.py | 10 +- .../templates/subdomains/index.html | 8 +- lnbits/extensions/subdomains/views_api.py | 1 + 5 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 lnbits/extensions/subdomains/crud.py diff --git a/lnbits/extensions/subdomains/crud.py b/lnbits/extensions/subdomains/crud.py new file mode 100644 index 000000000..217d6f025 --- /dev/null +++ b/lnbits/extensions/subdomains/crud.py @@ -0,0 +1,140 @@ +from typing import List, Optional, Union + +from lnbits.helpers import urlsafe_short_hash + +from . import db +from .models import Domains, Subdomains +import httpx + +from lnbits.extensions import subdomains + +async def create_subdomain( + payment_hash: str, + wallet: str, + domain: str, + subdomain: str, + email: str, + ip: str, + sats: int, +) -> Subdomains: + await db.execute( + """ + INSERT INTO subdomain (id, domain, email, subdomain, ip, wallet, sats, paid) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + """, + (payment_hash, domain, email, subdomain, ip, wallet, sats, False), + ) + + subdomain = await get_subdomain(payment_hash) + assert subdomain, "Newly created subdomain couldn't be retrieved" + return subdomain + + +async def set_subdomain_paid(payment_hash: str) -> Subdomains: + row = await db.fetchone("SELECT * FROM subdomain WHERE id = ?", (payment_hash,)) + if row[7] == False: + await db.execute( + """ + UPDATE subdomain + SET paid = true + WHERE id = ? + """, + (payment_hash,), + ) + + domaindata = await get_domain(row[1]) + assert domaindata, "Couldn't get domain from paid subdomain" + + amount = domaindata.amountmade + row[7] + await db.execute( + """ + UPDATE domain + SET amountmade = ? + WHERE id = ? + """, + (amount, row[1]), + ) + + subdomain = await get_subdomain(payment_hash) + if domaindata.webhook: + async with httpx.AsyncClient() as client: + try: + r = await client.post( + domaindata.webhook, + json={ + "domain": subdomain.domain, + "subdomain": subdomain.subdomain, + "email": subdomain.email, + "ip": subdomain.ip + }, + timeout=40, + ) + except AssertionError: + webhook = None + return subdomain + subdomain = await get_subdomain(payment_hash) + return + + +async def get_subdomain(subdomain_id: str) -> Optional[Subdomains]: + row = await db.fetchone("SELECT * FROM subdomain WHERE id = ?", (subdomain_id,)) + return Subdomains(**row) if row else None + + +async def get_subdomains(wallet_ids: Union[str, List[str]]) -> List[Subdomains]: + if isinstance(wallet_ids, str): + wallet_ids = [wallet_ids] + + q = ",".join(["?"] * len(wallet_ids)) + rows = await db.fetchall(f"SELECT * FROM subdomain WHERE wallet IN ({q})", (*wallet_ids,)) + + return [Subdomains(**row) for row in rows] + + +async def delete_subdomain(subdomain_id: str) -> None: + await db.execute("DELETE FROM subdomain WHERE id = ?", (subdomain_id,)) + + +# Domains + + +async def create_domain(*, wallet: str, domain: str, cfToken: str, cfZoneId: str, webhook: Optional[str] = None, description: str, cost: int) -> Domains: + domain_id = urlsafe_short_hash() + await db.execute( + """ + INSERT INTO domains (id, wallet, domain, webhook, cf_token, cf_zone_id, description, cost, amountmade) + VALUES (?, ?, ?, ?, ?, ?, ?. ?, ?) + """, + (domain_id, wallet, domain, webhook, cfToken, cfZoneId, description, cost, 0), + ) + + domain = await get_domain(domain_id) + assert domain, "Newly created domain couldn't be retrieved" + return domain + + +async def update_domain(domain_id: str, **kwargs) -> Domains: + q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) + await db.execute(f"UPDATE domain SET {q} WHERE id = ?", (*kwargs.values(), domain_id)) + row = await db.fetchone("SELECT * FROM domain WHERE id = ?", (domain_id,)) + assert row, "Newly updated domain couldn't be retrieved" + return Domains(**row) + + +async def get_domain(domain_id: str) -> Optional[Domains]: + row = await db.fetchone("SELECT * FROM domain WHERE id = ?", (domain_id,)) + return Domains(**row) if row else None + + +async def get_domains(wallet_ids: Union[str, List[str]]) -> List[Domains]: + if isinstance(wallet_ids, str): + wallet_ids = [wallet_ids] + + q = ",".join(["?"] * len(wallet_ids)) + rows = await db.fetchall(f"SELECT * FROM domain WHERE wallet IN ({q})", (*wallet_ids,)) + + return [Domains(**row) for row in rows] + + +async def delete_domain(domain_id: str) -> None: + await db.execute("DELETE FROM domain WHERE id = ?", (domain_id,)) diff --git a/lnbits/extensions/subdomains/migrations.py b/lnbits/extensions/subdomains/migrations.py index 00010f60f..7d4fa4d71 100644 --- a/lnbits/extensions/subdomains/migrations.py +++ b/lnbits/extensions/subdomains/migrations.py @@ -5,7 +5,7 @@ async def m001_initial(db): CREATE TABLE IF NOT EXISTS domain ( id TEXT PRIMARY KEY, wallet TEXT NOT NULL, - domain_name TEXT NOT NULL, + domain TEXT NOT NULL, webhook TEXT, cf_token TEXT NOT NULL, cf_zone_id TEXT NOT NULL, @@ -21,7 +21,7 @@ async def m001_initial(db): """ CREATE TABLE IF NOT EXISTS subdomain ( id TEXT PRIMARY KEY, - domain_name TEXT NOT NULL, + domain TEXT NOT NULL, email TEXT NOT NULL, subdomain TEXT NOT NULL, ip TEXT NOT NULL, diff --git a/lnbits/extensions/subdomains/models.py b/lnbits/extensions/subdomains/models.py index 9b330b858..0a71a1dd8 100644 --- a/lnbits/extensions/subdomains/models.py +++ b/lnbits/extensions/subdomains/models.py @@ -4,7 +4,7 @@ from typing import NamedTuple class Domains(NamedTuple): id: str wallet: str - domainName: str + domain: str cfToken: str cfZoneId: str webhook: str @@ -16,11 +16,11 @@ class Domains(NamedTuple): class Subdomains(NamedTuple): id: str - domainName: str - email: str - subdomain: str - ip: str wallet: str + domain: str + subdomain: str + email: str + ip: str sats: int paid: bool time: int diff --git a/lnbits/extensions/subdomains/templates/subdomains/index.html b/lnbits/extensions/subdomains/templates/subdomains/index.html index cc6a37362..183fc0221 100644 --- a/lnbits/extensions/subdomains/templates/subdomains/index.html +++ b/lnbits/extensions/subdomains/templates/subdomains/index.html @@ -207,18 +207,18 @@ createDomain: function (wallet, data) { var self = this - /* + LNbits.api - .request('POST', '/lnticket/api/v1/forms', wallet.inkey, data) + .request('POST', '/subdomains/api/v1/domains', wallet.inkey, data) .then(function (response) { - self.forms.push(mapLNTicket(response.data)) + self.forms.push(mapLNDomain(response.data)) self.domainDialog.show = false self.domainDialog.data = {} }) .catch(function (error) { LNbits.utils.notifyApiError(error) }) - */ + }, updateDomainDialog: function (formId) { var link = _.findWhere(this.forms, {id: formId}) diff --git a/lnbits/extensions/subdomains/views_api.py b/lnbits/extensions/subdomains/views_api.py index bfcac16c7..dc9263ca2 100644 --- a/lnbits/extensions/subdomains/views_api.py +++ b/lnbits/extensions/subdomains/views_api.py @@ -38,3 +38,4 @@ async def api_subdomains(): ] return jsonify(tools), HTTPStatus.OK +