feat: update product

This commit is contained in:
Vlad Stan 2023-03-02 18:38:06 +02:00
parent 1e6aaf8436
commit 62e7d439c7
4 changed files with 84 additions and 35 deletions

28
crud.py
View file

@ -218,6 +218,30 @@ async def create_product(user_id: str, data: PartialProduct) -> Product:
return 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]: async def get_product(user_id: str, product_id: str) -> Optional[Product]:
row = await db.fetchone( row = await db.fetchone(
"SELECT * FROM nostrmarket.products WHERE user_id =? AND id = ?", "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_id,
), ),
) )
product = Product.from_row(row) if row else None return Product.from_row(row) if row else None
return product
async def get_products(user_id: str, stall_id: str) -> List[Product]: async def get_products(user_id: str, stall_id: str) -> List[Product]:

View file

@ -143,7 +143,7 @@
size="sm" size="sm"
color="accent" color="accent"
dense dense
@click="props.row.expanded= !props.row.expanded" @click="editProduct(props.row)"
icon="edit" icon="edit"
/> />
</q-td> </q-td>

View file

@ -203,6 +203,7 @@ async function stallDetails(path) {
sendProductFormData: function () { sendProductFormData: function () {
var data = { var data = {
stall_id: this.stall.id, stall_id: this.stall.id,
id: this.productDialog.data.id,
name: this.productDialog.data.name, name: this.productDialog.data.name,
description: this.productDialog.data.description, description: this.productDialog.data.description,
categories: this.productDialog.data.categories, categories: this.productDialog.data.categories,
@ -218,39 +219,27 @@ async function stallDetails(path) {
this.createProduct(data) this.createProduct(data)
} }
}, },
updateProduct: function (data) { updateProduct: async function (product) {
var self = this try {
let wallet = _.findWhere(this.stalls, { const {data} = await LNbits.api.request(
id: self.productDialog.data.stall 'PATCH',
}).wallet '/nostrmarket/api/v1/product/' + product.id,
LNbits.api this.adminkey,
.request( product
'PUT',
'/nostrmarket/api/v1/products/' + data.id,
_.findWhere(self.g.user.wallets, {
id: wallet
}).inkey,
data
) )
.then(async function (response) { const index = this.products.findIndex(r => r.id === product.id)
self.products = _.reject(self.products, function (obj) { if (index !== -1) {
return obj.id == data.id this.products.splice(index, 1, data)
}) }
let productData = mapProducts(response.data) this.$q.notify({
self.products.push(productData) type: 'positive',
//SEND Nostr data message: 'Product Updated',
try { timeout: 5000
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)
}) })
} catch (error) {
console.warn(error)
LNbits.utils.notifyApiError(error)
}
}, },
createProduct: async function (payload) { createProduct: async function (payload) {
try { try {
@ -271,6 +260,10 @@ async function stallDetails(path) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} }
}, },
editProduct: async function (product) {
this.productDialog.data = {...product}
this.productDialog.showDialog = true
},
deleteProduct: async function (productId) { deleteProduct: async function (productId) {
LNbits.utils LNbits.utils
.confirmDialog('Are you sure you want to delete this product?') .confirmDialog('Are you sure you want to delete this product?')
@ -296,6 +289,15 @@ async function stallDetails(path) {
}) })
}, },
showNewProductDialog: async function () { showNewProductDialog: async function () {
this.productDialog.data = {
id: null,
name: '',
description: '',
categories: [],
image: null,
price: 0,
quantity: 0
}
this.productDialog.showDialog = true this.productDialog.showDialog = true
} }
}, },

View file

@ -29,6 +29,7 @@ from .crud import (
get_stalls, get_stalls,
get_zone, get_zone,
get_zones, get_zones,
update_product,
update_stall, update_stall,
update_zone, 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}") @nostrmarket_ext.get("/api/v1/product/{stall_id}")
async def api_get_product( async def api_get_product(
stall_id: str, stall_id: str,