Skip to content

Commit

Permalink
Merge pull request #6 from lnbits/refactor-singular
Browse files Browse the repository at this point in the history
refactor into singular
  • Loading branch information
dni authored Jun 27, 2023
2 parents cdcba00 + 1032bd9 commit fb428cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
32 changes: 16 additions & 16 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from lnbits.helpers import urlsafe_short_hash

from . import db
from .models import CreateEvent, Events, Tickets
from .models import CreateEvent, Event, Ticket

# TICKETS


async def create_ticket(
payment_hash: str, wallet: str, event: str, name: str, email: str
) -> Tickets:
) -> Ticket:
await db.execute(
"""
INSERT INTO events.ticket (id, wallet, event, name, email, registered, paid)
Expand Down Expand Up @@ -38,20 +38,20 @@ async def create_ticket(
return ticket


async def get_ticket(payment_hash: str) -> Optional[Tickets]:
async def get_ticket(payment_hash: str) -> Optional[Ticket]:
row = await db.fetchone("SELECT * FROM events.ticket WHERE id = ?", (payment_hash,))
return Tickets(**row) if row else None
return Ticket(**row) if row else None


async def get_tickets(wallet_ids: Union[str, List[str]]) -> List[Tickets]:
async def get_tickets(wallet_ids: Union[str, List[str]]) -> List[Ticket]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM events.ticket WHERE wallet IN ({q})", (*wallet_ids,)
)
return [Tickets(**row) for row in rows]
return [Ticket(**row) for row in rows]


async def delete_ticket(payment_hash: str) -> None:
Expand All @@ -65,7 +65,7 @@ async def delete_event_tickets(event_id: str) -> None:
# EVENTS


async def create_event(data: CreateEvent) -> Events:
async def create_event(data: CreateEvent) -> Event:
event_id = urlsafe_short_hash()
await db.execute(
"""
Expand All @@ -91,7 +91,7 @@ async def create_event(data: CreateEvent) -> Events:
return event


async def update_event(event_id: str, **kwargs) -> Events:
async def update_event(event_id: str, **kwargs) -> Event:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE events.events SET {q} WHERE id = ?", (*kwargs.values(), event_id)
Expand All @@ -101,12 +101,12 @@ async def update_event(event_id: str, **kwargs) -> Events:
return event


async def get_event(event_id: str) -> Optional[Events]:
async def get_event(event_id: str) -> Optional[Event]:
row = await db.fetchone("SELECT * FROM events.events WHERE id = ?", (event_id,))
return Events(**row) if row else None
return Event(**row) if row else None


async def get_events(wallet_ids: Union[str, List[str]]) -> List[Events]:
async def get_events(wallet_ids: Union[str, List[str]]) -> List[Event]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]

Expand All @@ -115,7 +115,7 @@ async def get_events(wallet_ids: Union[str, List[str]]) -> List[Events]:
f"SELECT * FROM events.events WHERE wallet IN ({q})", (*wallet_ids,)
)

return [Events(**row) for row in rows]
return [Event(**row) for row in rows]


async def delete_event(event_id: str) -> None:
Expand All @@ -125,20 +125,20 @@ async def delete_event(event_id: str) -> None:
# EVENTTICKETS


async def get_event_tickets(event_id: str, wallet_id: str) -> List[Tickets]:
async def get_event_tickets(event_id: str, wallet_id: str) -> List[Ticket]:
rows = await db.fetchall(
"SELECT * FROM events.ticket WHERE wallet = ? AND event = ?",
(wallet_id, event_id),
)
return [Tickets(**row) for row in rows]
return [Ticket(**row) for row in rows]


async def reg_ticket(ticket_id: str) -> List[Tickets]:
async def reg_ticket(ticket_id: str) -> List[Ticket]:
await db.execute(
"UPDATE events.ticket SET registered = ? WHERE id = ?", (True, ticket_id)
)
ticket = await db.fetchone("SELECT * FROM events.ticket WHERE id = ?", (ticket_id,))
rows = await db.fetchall(
"SELECT * FROM events.ticket WHERE event = ?", (ticket[1],)
)
return [Tickets(**row) for row in rows]
return [Ticket(**row) for row in rows]
6 changes: 3 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from fastapi.param_functions import Query
from fastapi import Query
from pydantic import BaseModel


Expand All @@ -18,7 +18,7 @@ class CreateTicket(BaseModel):
email: str


class Events(BaseModel):
class Event(BaseModel):
id: str
wallet: str
name: str
Expand All @@ -32,7 +32,7 @@ class Events(BaseModel):
time: int


class Tickets(BaseModel):
class Ticket(BaseModel):
id: str
wallet: str
event: str
Expand Down

0 comments on commit fb428cc

Please sign in to comment.