From ea3b858695440c845143571972740e0d08092db1 Mon Sep 17 00:00:00 2001
From: benarc Events is an easy way to create/sell tickets for an event.
+ LNEVENTS
-Create/sell tickets for an event
-Example Extension
+*tagline*
+This is an example extension to help you organise and build you own.
-It is advised to setup a specific wallet in lnbits for the event.
curl -H "Content-type: application/json" -X POST https://YOUR-LNBITS/YOUR-EXTENSION/api/v1/EXAMPLE -d '{"amount":"100","memo":"example"}' -H "X-Api-Key: YOUR_WALLET-ADMIN/INVOICE-KEY"
diff --git a/lnbits/extensions/events/config.json.example b/lnbits/extensions/events/config.json.example
index 738e264f..3c9055d3 100644
--- a/lnbits/extensions/events/config.json.example
+++ b/lnbits/extensions/events/config.json.example
@@ -1,6 +1,6 @@
{
- "name": "Events",
- "short_description": "LN tickets for events.",
- "icon": "local_activity",
- "contributors": ["arcbtc"]
+ "name": "Events",
+ "short_description": "Sell/register event tickets",
+ "icon": "local_activity",
+ "contributors": ["benarc"]
}
diff --git a/lnbits/extensions/events/config.json.wip b/lnbits/extensions/events/config.json.wip
deleted file mode 100644
index 738e264f..00000000
--- a/lnbits/extensions/events/config.json.wip
+++ /dev/null
@@ -1,6 +0,0 @@
-{
- "name": "Events",
- "short_description": "LN tickets for events.",
- "icon": "local_activity",
- "contributors": ["arcbtc"]
-}
diff --git a/lnbits/extensions/events/crud.py b/lnbits/extensions/events/crud.py
new file mode 100644
index 00000000..d0a23d89
--- /dev/null
+++ b/lnbits/extensions/events/crud.py
@@ -0,0 +1,116 @@
+from typing import List, Optional, Union
+
+from lnbits.db import open_ext_db
+from lnbits.helpers import urlsafe_short_hash
+
+from .models import Tickets, Events
+
+
+#######TICKETS########
+
+
+def create_ticket(wallet: str, event: str, name: str, email: str) -> Tickets:
+ with open_ext_db("events") as db:
+ eventdata = get_event(event)
+ sold = eventdata.sold + 1
+ amount_tickets = eventdata.amount_tickets - 1
+ ticket_id = urlsafe_short_hash()
+ db.execute(
+ """
+ INSERT INTO tickets (id, wallet, event, name, email, registered)
+ VALUES (?, ?, ?, ?, ?, ?)
+ """,
+ (ticket_id, wallet, event, name, email, False),
+ )
+ db.execute(
+ """
+ UPDATE events
+ SET sold = ?, amount_tickets = ?
+ WHERE id = ?
+ """,
+ (sold, amount_tickets, event),
+ )
+ return get_ticket(ticket_id)
+
+
+def get_ticket(ticket_id: str) -> Optional[Tickets]:
+ with open_ext_db("events") as db:
+ row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (ticket_id,))
+
+ return Tickets(**row) if row else None
+
+
+def get_tickets(wallet_ids: Union[str, List[str]]) -> List[Tickets]:
+ if isinstance(wallet_ids, str):
+ wallet_ids = [wallet_ids]
+
+ with open_ext_db("events") as db:
+ q = ",".join(["?"] * len(wallet_ids))
+ rows = db.fetchall(f"SELECT * FROM tickets WHERE wallet IN ({q})", (*wallet_ids,))
+ print("scrum")
+ return [Tickets(**row) for row in rows]
+
+
+def delete_ticket(ticket_id: str) -> None:
+ with open_ext_db("events") as db:
+ db.execute("DELETE FROM tickets WHERE id = ?", (ticket_id,))
+
+
+
+########EVENTS#########
+
+
+def create_event(*, wallet: str, name: str, info: str, closing_date: str, event_start_date: str, event_end_date: str, amount_tickets: int, price_per_ticket: int) -> Events:
+ with open_ext_db("events") as db:
+ event_id = urlsafe_short_hash()
+ db.execute(
+ """
+ INSERT INTO events (id, wallet, name, info, closing_date, event_start_date, event_end_date, amount_tickets, price_per_ticket, sold)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
+ """,
+ (event_id, wallet, name, info, closing_date, event_start_date, event_end_date, amount_tickets, price_per_ticket, 0),
+ )
+ print(event_id)
+
+ return get_event(event_id)
+
+def update_event(event_id: str, **kwargs) -> Events:
+ q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
+ with open_ext_db("events") as db:
+ db.execute(f"UPDATE events SET {q} WHERE id = ?", (*kwargs.values(), event_id))
+ row = db.fetchone("SELECT * FROM events WHERE id = ?", (event_id,))
+
+ return Events(**row) if row else None
+
+
+def get_event(event_id: str) -> Optional[Events]:
+ with open_ext_db("events") as db:
+ row = db.fetchone("SELECT * FROM events WHERE id = ?", (event_id,))
+
+ return Events(**row) if row else None
+
+
+def get_events(wallet_ids: Union[str, List[str]]) -> List[Events]:
+ if isinstance(wallet_ids, str):
+ wallet_ids = [wallet_ids]
+
+ with open_ext_db("events") as db:
+ q = ",".join(["?"] * len(wallet_ids))
+ rows = db.fetchall(f"SELECT * FROM events WHERE wallet IN ({q})", (*wallet_ids,))
+
+ return [Events(**row) for row in rows]
+
+
+def delete_event(event_id: str) -> None:
+ with open_ext_db("events") as db:
+ db.execute("DELETE FROM events WHERE id = ?", (event_id,))
+
+########EVENTTICKETS#########
+
+def get_event_tickets(event_id: str, wallet_id: str) -> Tickets:
+
+ with open_ext_db("events") as db:
+ rows = db.fetchall("SELECT * FROM tickets WHERE wallet = ? AND event = ?", (wallet_id, event_id))
+ print(rows)
+
+ return [Tickets(**row) for row in rows]
\ No newline at end of file
diff --git a/lnbits/extensions/events/migrations.py b/lnbits/extensions/events/migrations.py
index 04435aa5..523071e5 100644
--- a/lnbits/extensions/events/migrations.py
+++ b/lnbits/extensions/events/migrations.py
@@ -1,5 +1,40 @@
from lnbits.db import open_ext_db
+def m001_initial(db):
+
+ db.execute(
+ """
+ CREATE TABLE IF NOT EXISTS events (
+ id TEXT PRIMARY KEY,
+ wallet TEXT NOT NULL,
+ name TEXT NOT NULL,
+ info TEXT NOT NULL,
+ closing_date TEXT NOT NULL,
+ event_start_date TEXT NOT NULL,
+ event_end_date TEXT NOT NULL,
+ amount_tickets INTEGER NOT NULL,
+ price_per_ticket INTEGER NOT NULL,
+ sold INTEGER NOT NULL,
+ time TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now'))
+ );
+ """
+ )
+
+ db.execute(
+ """
+ CREATE TABLE IF NOT EXISTS tickets (
+ id TEXT PRIMARY KEY,
+ wallet TEXT NOT NULL,
+ event TEXT NOT NULL,
+ name TEXT NOT NULL,
+ email TEXT NOT NULL,
+ registered BOOLEAN NOT NULL,
+ time TIMESTAMP NOT NULL DEFAULT (strftime('%s', 'now'))
+ );
+ """
+ )
def migrate():
- print("pending")
+ with open_ext_db("events") as db:
+ m001_initial(db)
+
diff --git a/lnbits/extensions/events/models.py b/lnbits/extensions/events/models.py
new file mode 100644
index 00000000..d2d60663
--- /dev/null
+++ b/lnbits/extensions/events/models.py
@@ -0,0 +1,24 @@
+from typing import NamedTuple
+
+
+class Events(NamedTuple):
+ id: str
+ wallet: str
+ name: str
+ info: str
+ closing_date: str
+ event_start_date: str
+ event_end_date: str
+ amount_tickets: int
+ price_per_ticket: int
+ sold: int
+ time: int
+
+class Tickets(NamedTuple):
+ id: str
+ wallet: str
+ event: str
+ name: str
+ email: str
+ registered: bool
+ time: int
\ No newline at end of file
diff --git a/lnbits/extensions/events/schema.sql b/lnbits/extensions/events/schema.sql
deleted file mode 100644
index bc1dabca..00000000
--- a/lnbits/extensions/events/schema.sql
+++ /dev/null
@@ -1,26 +0,0 @@
-CREATE TABLE IF NOT EXISTS events (
- key INTEGER PRIMARY KEY AUTOINCREMENT,
- usr TEXT,
- wal TEXT,
- walnme TEXT,
- walinvkey INTEGER,
- uni TEXT,
- tit TEXT,
- cldate TEXT,
- notickets INTEGER,
- sold INTEGER DEFAULT 0,
- prtick INTEGER,
- descr TEXT,
- unireg TEXT
-);
-
-CREATE TABLE IF NOT EXISTS eventssold (
- key INTEGER PRIMARY KEY AUTOINCREMENT,
- uni TEXT,
- email TEXT,
- name TEXT,
- hash TEXT,
- paid INTEGER DEFAULT 0,
- reg INTEGER DEFAULT 0
-);
-
diff --git a/lnbits/extensions/events/templates/events/_api_docs.html b/lnbits/extensions/events/templates/events/_api_docs.html
new file mode 100644
index 00000000..c2a71b00
--- /dev/null
+++ b/lnbits/extensions/events/templates/events/_api_docs.html
@@ -0,0 +1,27 @@
+
+ Events alows you to make a wave of tickets for an event. Once an attendee has paid for a ticket they get a unqiue code. Events comes with a shareable scanning frontend, so you can register the attendees
+ Created by, Ben Arc