diff --git a/lnbits/extensions/livestream/crud.py b/lnbits/extensions/livestream/crud.py index 0d2f98f95..1c1043db2 100644 --- a/lnbits/extensions/livestream/crud.py +++ b/lnbits/extensions/livestream/crud.py @@ -1,4 +1,3 @@ -import unicodedata from typing import List, Optional from lnbits.core.crud import create_account, create_wallet @@ -65,22 +64,36 @@ async def add_track( name: str, download_url: Optional[str], price_msat: int, - producer_name: Optional[str], - producer_id: Optional[int], + producer: Optional[int], ) -> int: - if producer_id: - p_id = producer_id - elif producer_name: - p_id = await add_producer(livestream, producer_name) - else: - raise TypeError("need either producer_id or producer_name arguments") - result = await db.execute( """ INSERT INTO tracks (livestream, name, download_url, price_msat, producer) VALUES (?, ?, ?, ?, ?) """, - (livestream, name, download_url, price_msat, p_id), + (livestream, name, download_url, price_msat, producer), + ) + return result._result_proxy.lastrowid + + +async def update_track( + livestream: int, + track_id: int, + name: str, + download_url: Optional[str], + price_msat: int, + producer: int, +) -> int: + result = await db.execute( + """ + UPDATE tracks SET + name = ?, + download_url = ?, + price_msat = ?, + producer = ? + WHERE livestream = ? AND id = ? + """, + (name, download_url, price_msat, producer, livestream, track_id), ) return result._result_proxy.lastrowid @@ -120,7 +133,7 @@ async def delete_track_from_livestream(livestream: int, track_id: int): async def add_producer(livestream: int, name: str) -> int: - name = "".join([unicodedata.normalize("NFD", l)[0] for l in name if l]).strip() + name = name.strip() existing = await db.fetchall( """ diff --git a/lnbits/extensions/livestream/static/js/index.js b/lnbits/extensions/livestream/static/js/index.js index ac00fa807..faa439cb9 100644 --- a/lnbits/extensions/livestream/static/js/index.js +++ b/lnbits/extensions/livestream/static/js/index.js @@ -93,24 +93,21 @@ new Vue({ ) }, addTrack() { - let {name, producer, price_sat, download_url} = this.trackDialog.data + let {id, name, producer, price_sat, download_url} = this.trackDialog.data + + const [method, path] = id + ? ['PUT', `/livestream/api/v1/livestream/tracks/${id}`] + : ['POST', '/livestream/api/v1/livestream/tracks'] LNbits.api - .request( - 'POST', - '/livestream/api/v1/livestream/tracks', - this.selectedWallet.inkey, - { - download_url: - download_url && download_url.length > 0 - ? download_url - : undefined, - name, - price_msat: price_sat * 1000 || 0, - producer_name: typeof producer === 'string' ? producer : undefined, - producer_id: typeof producer === 'object' ? producer.id : undefined - } - ) + .request(method, path, this.selectedWallet.inkey, { + download_url: + download_url && download_url.length > 0 ? download_url : undefined, + name, + price_msat: price_sat * 1000 || 0, + producer_name: typeof producer === 'string' ? producer : undefined, + producer_id: typeof producer === 'object' ? producer.id : undefined + }) .then(response => { this.$q.notify({ message: `Track '${this.trackDialog.data.name}' added.`, @@ -124,6 +121,21 @@ new Vue({ LNbits.utils.notifyApiError(err) }) }, + openAddTrackDialog() { + this.trackDialog.show = true + this.trackDialog.data = {} + }, + openUpdateDialog(itemId) { + this.trackDialog.show = true + let item = this.livestream.tracks.find(item => item.id === itemId) + this.trackDialog.data = { + ...item, + producer: this.livestream.producers.find( + prod => prod.id === item.producer + ), + price_sat: Math.round(item.price_msat / 1000) + } + }, deleteTrack(trackId) { LNbits.utils .confirmDialog('Are you sure you want to delete this track?') diff --git a/lnbits/extensions/livestream/templates/livestream/index.html b/lnbits/extensions/livestream/templates/livestream/index.html index 5cf1e3c8c..e73293d26 100644 --- a/lnbits/extensions/livestream/templates/livestream/index.html +++ b/lnbits/extensions/livestream/templates/livestream/index.html @@ -64,7 +64,7 @@ Add new track @@ -107,12 +107,20 @@ {{ producersMap[props.row.producer].name }} {{ props.row.price_msat }}{{ Math.round(props.row.price_msat / 1000) }} {{ props.row.download_url }} + - + Add track + Update track + Add track +
diff --git a/lnbits/extensions/livestream/views_api.py b/lnbits/extensions/livestream/views_api.py index 1150ddb91..cf9708172 100644 --- a/lnbits/extensions/livestream/views_api.py +++ b/lnbits/extensions/livestream/views_api.py @@ -9,9 +9,11 @@ from .crud import ( get_or_create_livestream_by_wallet, add_track, get_tracks, + update_track, + add_producer, get_producers, - update_livestream_fee, update_current_track, + update_livestream_fee, delete_track_from_livestream, ) @@ -72,6 +74,7 @@ async def api_update_fee(fee_pct): @livestream_ext.route("/api/v1/livestream/tracks", methods=["POST"]) +@livestream_ext.route("/api/v1/livestream/tracks/", methods=["PUT"]) @api_check_wallet_key("invoice") @api_validate_post_request( schema={ @@ -90,17 +93,35 @@ async def api_update_fee(fee_pct): }, } ) -async def api_add_track(): +async def api_add_track(id=None): ls = await get_or_create_livestream_by_wallet(g.wallet.id) - await add_track( - ls.id, - g.data["name"], - g.data.get("download_url"), - g.data.get("price_msat", 0), - g.data.get("producer_name"), - g.data.get("producer_id"), - ) - return "", HTTPStatus.CREATED + + if "producer_id" in g.data: + p_id = g.data["producer_id"] + elif "producer_name" in g.data: + p_id = await add_producer(ls.id, g.data["producer_name"]) + else: + raise TypeError("need either producer_id or producer_name arguments") + + if id: + await update_track( + ls.id, + id, + g.data["name"], + g.data.get("download_url"), + g.data.get("price_msat", 0), + p_id, + ) + return "", HTTPStatus.OK + else: + await add_track( + ls.id, + g.data["name"], + g.data.get("download_url"), + g.data.get("price_msat", 0), + p_id, + ) + return "", HTTPStatus.CREATED @livestream_ext.route("/api/v1/livestream/tracks/", methods=["DELETE"])