diff --git a/crud.py b/crud.py index ce190bf..d19a958 100644 --- a/crud.py +++ b/crud.py @@ -228,3 +228,11 @@ async def get_product(user_id: str, product_id: str) -> Optional[Product]: product = Product.from_row(row) if row else None return product + + +async def get_products(user_id: str, stall_id: str) -> List[Product]: + rows = await db.fetchall( + "SELECT * FROM nostrmarket.products WHERE user_id = ? AND stall_id = ?", + (user_id, stall_id), + ) + return [Product.from_row(row) for row in rows] diff --git a/static/components/stall-details/stall-details.html b/static/components/stall-details/stall-details.html index 73615c9..a4260b2 100644 --- a/static/components/stall-details/stall-details.html +++ b/static/components/stall-details/stall-details.html @@ -115,6 +115,59 @@
+ +
+
+ + + +
+
diff --git a/static/components/stall-details/stall-details.js b/static/components/stall-details/stall-details.js index dafd42a..9fead9e 100644 --- a/static/components/stall-details/stall-details.js +++ b/static/components/stall-details/stall-details.js @@ -32,6 +32,63 @@ async function stallDetails(path) { price: 0, quantity: 0 } + }, + productsFilter: '', + productsTable: { + columns: [ + { + name: 'delete', + align: 'left', + label: '', + field: '' + }, + { + name: 'edit', + align: 'left', + label: '', + field: '' + }, + + { + name: 'id', + align: 'left', + label: 'ID', + field: 'id' + }, + { + name: 'name', + align: 'left', + label: 'Name', + field: 'name' + }, + { + name: 'price', + align: 'left', + label: 'Price', + field: 'price' + }, + { + name: 'quantity', + align: 'left', + label: 'Quantity', + field: 'quantity' + }, + { + name: 'categories', + align: 'left', + label: 'Categories', + field: 'categories' + }, + { + name: 'description', + align: 'left', + label: 'Description', + field: 'description' + } + ], + pagination: { + rowsPerPage: 10 + } } } }, @@ -129,6 +186,20 @@ async function stallDetails(path) { this.productDialog.data.image = null this.productDialog = {...this.productDialog} }, + getProducts: async function () { + try { + const {data} = await LNbits.api.request( + 'GET', + '/nostrmarket/api/v1/product/' + this.stall.id, + this.inkey + ) + this.products = data + + console.log('### this.products', this.products) + } catch (error) { + LNbits.utils.notifyApiError(error) + } + }, sendProductFormData: function () { var data = { stall_id: this.stall.id, @@ -206,6 +277,7 @@ async function stallDetails(path) { }, created: async function () { await this.getStall() + await this.getProducts() } }) } diff --git a/views_api.py b/views_api.py index 472e17a..76f4f11 100644 --- a/views_api.py +++ b/views_api.py @@ -23,6 +23,7 @@ from .crud import ( delete_stall, delete_zone, get_merchant_for_user, + get_products, get_stall, get_stalls, get_zone, @@ -299,9 +300,9 @@ async def api_delete_stall( @nostrmarket_ext.post("/api/v1/product") -async def api_market_product_create( +async def api_create_product( data: PartialProduct, - wallet: WalletTypeInfo = Depends(require_invoice_key), + wallet: WalletTypeInfo = Depends(require_admin_key), ) -> Product: try: data.validate_product() @@ -321,14 +322,20 @@ async def api_market_product_create( ) -# @nostrmarket_ext.get("/api/v1/product/{stall_id}") -# async def api_market_products( -# stall_id: str, wallet: WalletTypeInfo = Depends(require_invoice_key), -# ): -# wallet_ids = [wallet.wallet.id] - - -# return [product.dict() for product in await get_products(stalls)] +@nostrmarket_ext.get("/api/v1/product/{stall_id}") +async def api_get_product( + stall_id: str, + wallet: WalletTypeInfo = Depends(require_invoice_key), +): + try: + products = await get_products(wallet.wallet.user, stall_id) + return products + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot get product", + ) # @market_ext.delete("/api/v1/products/{product_id}")