diff --git a/crud.py b/crud.py index d19a958..a89455e 100644 --- a/crud.py +++ b/crud.py @@ -106,6 +106,7 @@ async def get_zones(user_id: str) -> List[Zone]: async def delete_zone(zone_id: str) -> None: + # todo: add user_id await db.execute("DELETE FROM nostrmarket.zones WHERE id = ?", (zone_id,)) @@ -236,3 +237,13 @@ async def get_products(user_id: str, stall_id: str) -> List[Product]: (user_id, stall_id), ) return [Product.from_row(row) for row in rows] + + +async def delete_product(user_id: str, product_id: str) -> None: + await db.execute( + "DELETE FROM nostrmarket.products WHERE user_id =? AND id = ?", + ( + user_id, + product_id, + ), + ) diff --git a/static/components/stall-details/stall-details.html b/static/components/stall-details/stall-details.html index a4260b2..b2c736a 100644 --- a/static/components/stall-details/stall-details.html +++ b/static/components/stall-details/stall-details.html @@ -134,7 +134,7 @@ size="sm" color="pink" dense - @click="props.row.expanded= !props.row.expanded" + @click="deleteProduct(props.row.id)" icon="delete" /> diff --git a/static/components/stall-details/stall-details.js b/static/components/stall-details/stall-details.js index 9fead9e..ac86980 100644 --- a/static/components/stall-details/stall-details.js +++ b/static/components/stall-details/stall-details.js @@ -17,7 +17,7 @@ async function stallDetails(path) { ], data: function () { return { - tab: 'info', + tab: 'products', stall: null, products: [], productDialog: { @@ -271,6 +271,30 @@ async function stallDetails(path) { LNbits.utils.notifyApiError(error) } }, + deleteProduct: async function (productId) { + LNbits.utils + .confirmDialog('Are you sure you want to delete this product?') + .onOk(async () => { + try { + await LNbits.api.request( + 'DELETE', + '/nostrmarket/api/v1/product/' + productId, + this.adminkey + ) + this.products = _.reject(this.products, function (obj) { + return obj.id === productId + }) + this.$q.notify({ + type: 'positive', + message: 'Product deleted', + timeout: 5000 + }) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } + }) + }, showNewProductDialog: async function () { this.productDialog.showDialog = true } diff --git a/views_api.py b/views_api.py index 76f4f11..9260c9a 100644 --- a/views_api.py +++ b/views_api.py @@ -20,6 +20,7 @@ from .crud import ( create_product, create_stall, create_zone, + delete_product, delete_stall, delete_zone, get_merchant_for_user, @@ -338,23 +339,19 @@ async def api_get_product( ) -# @market_ext.delete("/api/v1/products/{product_id}") -# async def api_market_products_delete( -# product_id, wallet: WalletTypeInfo = Depends(require_admin_key) -# ): -# product = await get_market_product(product_id) - -# if not product: -# return {"message": "Product does not exist."} - -# stall = await get_market_stall(product.stall) -# assert stall - -# if stall.wallet != wallet.wallet.id: -# return {"message": "Not your Market."} - -# await delete_market_product(product_id) -# raise HTTPException(status_code=HTTPStatus.NO_CONTENT) +@nostrmarket_ext.delete("/api/v1/product/{product_id}") +async def api_delete_product( + product_id: str, + wallet: WalletTypeInfo = Depends(require_admin_key), +): + try: + await delete_product(wallet.wallet.user, product_id) + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot delete product", + ) ######################################## OTHER ########################################