Events extension now works

This commit is contained in:
arcbtc 2020-08-10 15:27:30 +01:00
parent bc930ab606
commit 784a2dd876
7 changed files with 61 additions and 42 deletions

View File

@ -9,33 +9,46 @@ from .models import Tickets, Events
#######TICKETS########
def create_ticket(wallet: str, event: str, name: str, email: str) -> Tickets:
def create_ticket(checking_id: str, 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 (?, ?, ?, ?, ?, ?)
INSERT INTO tickets (id, paid, wallet, event, name, email, registered)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(ticket_id, wallet, event, name, email, False),
(checking_id, False, wallet, event, name, email, False),
)
return get_ticket(checking_id)
def update_ticket(paid: bool, checking_id: str) -> Tickets:
with open_ext_db("events") as db:
db.execute(
"""
UPDATE tickets
SET paid = ?
WHERE id = ?
""",
(paid, checking_id),
)
row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (checking_id,))
eventdata = get_event(row[3])
sold = eventdata.sold + 1
amount_tickets = eventdata.amount_tickets - 1
db.execute(
"""
UPDATE events
SET sold = ?, amount_tickets = ?
WHERE id = ?
""",
(sold, amount_tickets, event),
(sold, amount_tickets, row[3]),
)
return get_ticket(ticket_id)
return get_ticket(checking_id)
def get_ticket(ticket_id: str) -> Optional[Tickets]:
def get_ticket(checking_id: str) -> Optional[Tickets]:
with open_ext_db("events") as db:
row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (ticket_id,))
row = db.fetchone("SELECT * FROM tickets WHERE id = ?", (checking_id,))
return Tickets(**row) if row else None
@ -51,9 +64,9 @@ def get_tickets(wallet_ids: Union[str, List[str]]) -> List[Tickets]:
return [Tickets(**row) for row in rows]
def delete_ticket(ticket_id: str) -> None:
def delete_ticket(checking_id: str) -> None:
with open_ext_db("events") as db:
db.execute("DELETE FROM tickets WHERE id = ?", (ticket_id,))
db.execute("DELETE FROM tickets WHERE id = ?", (checking_id,))

View File

@ -24,6 +24,7 @@ def m001_initial(db):
"""
CREATE TABLE IF NOT EXISTS tickets (
id TEXT PRIMARY KEY,
paid BOOLEAN NOT NULL,
wallet TEXT NOT NULL,
event TEXT NOT NULL,
name TEXT NOT NULL,

View File

@ -16,6 +16,7 @@ class Events(NamedTuple):
class Tickets(NamedTuple):
id: str
paid: bool
wallet: str
event: str
name: str

View File

@ -133,8 +133,15 @@
var self = this
axios
.get('/events/api/v1/tickets/' + '{{ event_id }}/{{ event_price }}')
.post(
'/events/api/v1/tickets/' + '{{ event_id }}/{{ event_price }}',
{
event: '{{ event_id }}',
event_name: '{{ event_name }}',
name: self.formDialog.data.name,
email: self.formDialog.data.email
}
)
.then(function (response) {
self.paymentReq = response.data.payment_request
self.paymentCheck = response.data.checking_id
@ -152,12 +159,7 @@
paymentChecker = setInterval(function () {
axios
.post('/events/api/v1/tickets/' + self.paymentCheck, {
event: '{{ event_id }}',
event_name: '{{ event_name }}',
name: self.formDialog.data.name,
email: self.formDialog.data.email
})
.get('/events/api/v1/tickets/' + self.paymentCheck)
.then(function (res) {
if (res.data.paid) {
clearInterval(paymentChecker)

View File

@ -156,9 +156,9 @@
type: 'positive',
message: 'Registered!'
})
self.tickets = response.data.map(function (obj) {
return mapEvents(obj)
})
setTimeout(function () {
window.location.reload()
}, 2000)
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)

View File

@ -7,7 +7,7 @@ from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.settings import WALLET
from lnbits.extensions.events import events_ext
from .crud import create_ticket, get_ticket, get_tickets, delete_ticket, create_event, update_event, get_event, get_events, delete_event, get_event_tickets, reg_ticket
from .crud import create_ticket, update_ticket, get_ticket, get_tickets, delete_ticket, create_event, update_event, get_event, get_events, delete_event, get_event_tickets, reg_ticket
#########Events##########
@ -86,11 +86,18 @@ def api_tickets():
return jsonify([ticket._asdict() for ticket in get_tickets(wallet_ids)]), HTTPStatus.OK
@events_ext.route("/api/v1/tickets/<event_id>/<sats>", methods=["POST"])
@api_validate_post_request(
schema={
"name": {"type": "string", "empty": False, "required": True},
"email": {"type": "string", "empty": False, "required": True}
})
def api_ticket_make_ticket(event_id, sats):
@events_ext.route("/api/v1/tickets/<event_id>/<sats>", methods=["GET"])
def api_ticket_create(event_id, sats):
event = get_event(event_id)
if not event:
return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND
try:
checking_id, payment_request = create_invoice(
wallet_id=event.wallet, amount=int(sats), memo=f"#lnticket {event_id}"
@ -98,32 +105,27 @@ def api_ticket_create(event_id, sats):
except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
ticket = create_ticket(checking_id=checking_id, wallet=event.wallet, event=event_id, **g.data)
if not ticket:
return jsonify({"message": "LNTicket could not be fetched."}), HTTPStatus.NOT_FOUND
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), HTTPStatus.OK
@events_ext.route("/api/v1/tickets/<checking_id>", methods=["POST"])
@api_validate_post_request(
schema={
"event": {"type": "string", "empty": False, "required": True},
"name": {"type": "string", "empty": False, "required": True},
"email": {"type": "string", "empty": False, "required": True}
})
@events_ext.route("/api/v1/tickets/<checking_id>", methods=["GET"])
def api_ticket_send_ticket(checking_id):
event = get_event(g.data['event'])
if not event:
return jsonify({"message": "LNTicket does not exist."}), HTTPStatus.NOT_FOUND
theticket = get_ticket(checking_id)
try:
is_paid = not WALLET.get_invoice_status(checking_id).pending
except Exception:
return jsonify({"message": "Not paid."}), HTTPStatus.NOT_FOUND
if is_paid:
wallet = get_wallet(event.wallet)
wallet = get_wallet(theticket.wallet)
payment = wallet.get_payment(checking_id)
payment.set_pending(False)
ticket = create_ticket(wallet=event.wallet, **g.data)
ticket = update_ticket(paid=True, checking_id=checking_id)
return jsonify({"paid": True, "ticket_id": ticket.id}), HTTPStatus.OK