From 62e7d439c7489939f20bb8680a5f3004f168c282 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Thu, 2 Mar 2023 18:38:06 +0200 Subject: [PATCH] feat: update product --- crud.py | 28 +++++++- .../stall-details/stall-details.html | 2 +- .../components/stall-details/stall-details.js | 64 ++++++++++--------- views_api.py | 25 ++++++++ 4 files changed, 84 insertions(+), 35 deletions(-) diff --git a/crud.py b/crud.py index a89455e..996bfc3 100644 --- a/crud.py +++ b/crud.py @@ -218,6 +218,30 @@ async def create_product(user_id: str, data: PartialProduct) -> Product: return product +async def update_product(user_id: str, product: Product) -> Product: + + await db.execute( + f""" + UPDATE nostrmarket.products set name = ?, category_list = ?, description = ?, images = ?, price = ?, quantity = ? + WHERE user_id = ? AND id = ? + """, + ( + product.name, + json.dumps(product.categories), + product.description, + product.image, + product.price, + product.quantity, + user_id, + product.id, + ), + ) + updated_product = await get_product(user_id, product.id) + assert updated_product, "Updated product couldn't be retrieved" + + return updated_product + + async def get_product(user_id: str, product_id: str) -> Optional[Product]: row = await db.fetchone( "SELECT * FROM nostrmarket.products WHERE user_id =? AND id = ?", @@ -226,9 +250,7 @@ async def get_product(user_id: str, product_id: str) -> Optional[Product]: product_id, ), ) - product = Product.from_row(row) if row else None - - return product + return Product.from_row(row) if row else None async def get_products(user_id: str, stall_id: str) -> List[Product]: diff --git a/static/components/stall-details/stall-details.html b/static/components/stall-details/stall-details.html index b2c736a..82610ca 100644 --- a/static/components/stall-details/stall-details.html +++ b/static/components/stall-details/stall-details.html @@ -143,7 +143,7 @@ size="sm" color="accent" dense - @click="props.row.expanded= !props.row.expanded" + @click="editProduct(props.row)" icon="edit" /> diff --git a/static/components/stall-details/stall-details.js b/static/components/stall-details/stall-details.js index ac86980..e62caaf 100644 --- a/static/components/stall-details/stall-details.js +++ b/static/components/stall-details/stall-details.js @@ -203,6 +203,7 @@ async function stallDetails(path) { sendProductFormData: function () { var data = { stall_id: this.stall.id, + id: this.productDialog.data.id, name: this.productDialog.data.name, description: this.productDialog.data.description, categories: this.productDialog.data.categories, @@ -218,39 +219,27 @@ async function stallDetails(path) { this.createProduct(data) } }, - updateProduct: function (data) { - var self = this - let wallet = _.findWhere(this.stalls, { - id: self.productDialog.data.stall - }).wallet - LNbits.api - .request( - 'PUT', - '/nostrmarket/api/v1/products/' + data.id, - _.findWhere(self.g.user.wallets, { - id: wallet - }).inkey, - data + updateProduct: async function (product) { + try { + const {data} = await LNbits.api.request( + 'PATCH', + '/nostrmarket/api/v1/product/' + product.id, + this.adminkey, + product ) - .then(async function (response) { - self.products = _.reject(self.products, function (obj) { - return obj.id == data.id - }) - let productData = mapProducts(response.data) - self.products.push(productData) - //SEND Nostr data - try { - await self.sendToRelays(productData, 'product', 'update') - } catch (e) { - console.error(e) - } - self.resetDialog('productDialog') - //self.productDialog.show = false - //self.productDialog.data = {} - }) - .catch(function (error) { - LNbits.utils.notifyApiError(error) + const index = this.products.findIndex(r => r.id === product.id) + if (index !== -1) { + this.products.splice(index, 1, data) + } + this.$q.notify({ + type: 'positive', + message: 'Product Updated', + timeout: 5000 }) + } catch (error) { + console.warn(error) + LNbits.utils.notifyApiError(error) + } }, createProduct: async function (payload) { try { @@ -271,6 +260,10 @@ async function stallDetails(path) { LNbits.utils.notifyApiError(error) } }, + editProduct: async function (product) { + this.productDialog.data = {...product} + this.productDialog.showDialog = true + }, deleteProduct: async function (productId) { LNbits.utils .confirmDialog('Are you sure you want to delete this product?') @@ -296,6 +289,15 @@ async function stallDetails(path) { }) }, showNewProductDialog: async function () { + this.productDialog.data = { + id: null, + name: '', + description: '', + categories: [], + image: null, + price: 0, + quantity: 0 + } this.productDialog.showDialog = true } }, diff --git a/views_api.py b/views_api.py index 9260c9a..3de3f97 100644 --- a/views_api.py +++ b/views_api.py @@ -29,6 +29,7 @@ from .crud import ( get_stalls, get_zone, get_zones, + update_product, update_stall, update_zone, ) @@ -323,6 +324,30 @@ async def api_create_product( ) +@nostrmarket_ext.patch("/api/v1/product/{product_id}") +async def api_update_product( + product_id: str, + product: Product, + wallet: WalletTypeInfo = Depends(require_admin_key), +) -> Product: + try: + product.validate_product() + product = await update_product(wallet.wallet.user, product) + + return product + except ValueError as ex: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail=str(ex), + ) + except Exception as ex: + logger.warning(ex) + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail="Cannot update product", + ) + + @nostrmarket_ext.get("/api/v1/product/{stall_id}") async def api_get_product( stall_id: str,