-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud.py
156 lines (122 loc) · 4.08 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from typing import Optional
from lnbits.core.crud import create_account, create_wallet
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import CreateTrack, Livestream, Producer, Track
db = Database("ext_livestream")
async def create_livestream(wallet_id: str) -> Livestream:
livestream = Livestream(
id=urlsafe_short_hash(),
wallet=wallet_id,
)
await db.insert("livestream.livestreams", livestream)
return livestream
async def get_livestream(ls_id: str) -> Optional[Livestream]:
return await db.fetchone(
"SELECT * FROM livestream.livestreams WHERE id = :id",
{"id": ls_id},
Livestream,
)
async def get_livestream_by_track(track_id: str) -> Optional[Livestream]:
return await db.fetchone(
"""
SELECT a.* FROM livestream.tracks as b
LEFT JOIN livestream.livestreams as a ON a.id = b.livestream
WHERE b.id = :id
""",
{"id": track_id},
Livestream,
)
async def get_or_create_livestream_by_wallet(wallet: str) -> Livestream:
livestream = await db.fetchone(
"SELECT * FROM livestream.livestreams WHERE wallet = :wallet",
{"wallet": wallet},
Livestream,
)
if livestream:
return livestream
# create on the fly
ls = await create_livestream(wallet)
return ls
async def update_current_track(ls_id: str, track_id: Optional[str]):
await db.execute(
"UPDATE livestream.livestreams SET current_track = :track_id WHERE id = :id",
{"track_id": track_id, "id": ls_id},
)
async def update_livestream_fee(ls_id: str, fee_pct: int):
await db.execute(
"UPDATE livestream.livestreams SET fee_pct = :fee_pct WHERE id = :id",
{"fee_pct": fee_pct, "id": ls_id},
)
async def create_track(
livestream: str,
producer: str,
data: CreateTrack,
) -> Track:
track = Track(
id=urlsafe_short_hash(),
livestream=livestream,
producer=producer,
name=data.name,
download_url=data.download_url,
price_msat=data.price_msat,
)
await db.insert("livestream.tracks", track)
return track
async def update_track(track: Track) -> Track:
await db.update("livestream.tracks", track)
return track
async def get_track(track_id: str) -> Optional[Track]:
return await db.fetchone(
"SELECT * FROM livestream.tracks WHERE id = :id",
{"id": track_id},
Track,
)
async def get_tracks(livestream: str) -> list[Track]:
return await db.fetchall(
"SELECT * FROM livestream.tracks WHERE livestream = :livestream",
{"livestream": livestream},
Track,
)
async def delete_track_from_livestream(livestream: str, track_id: str):
await db.execute(
"""
DELETE FROM livestream.tracks WHERE livestream = :livestream AND id = :id
""",
{"livestream": livestream, "id": track_id},
)
async def create_producer(livestream_id: str, name: str) -> Producer:
name = name.strip()
producer = await db.fetchone(
"""
SELECT * FROM livestream.producers
WHERE livestream = :livestream AND lower(name) = :name
""",
{"livestream": livestream_id, "name": name.lower()},
Producer,
)
if producer:
return producer
user = await create_account()
wallet = await create_wallet(user_id=user.id, wallet_name="livestream: " + name)
producer = Producer(
id=urlsafe_short_hash(),
livestream=livestream_id,
user=user.id,
wallet=wallet.id,
name=name,
)
await db.insert("livestream.producers", producer)
return producer
async def get_producer(producer_id: str) -> Optional[Producer]:
return await db.fetchone(
"SELECT * FROM livestream.producers WHERE id = :id",
{"id": producer_id},
Producer,
)
async def get_producers(livestream: str) -> list[Producer]:
return await db.fetchall(
"SELECT * FROM livestream.producers WHERE livestream = :livestream",
{"livestream": livestream},
Producer,
)