feat: update product
This commit is contained in:
parent
1e6aaf8436
commit
62e7d439c7
4 changed files with 84 additions and 35 deletions
28
crud.py
28
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]:
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@
|
|||
size="sm"
|
||||
color="accent"
|
||||
dense
|
||||
@click="props.row.expanded= !props.row.expanded"
|
||||
@click="editProduct(props.row)"
|
||||
icon="edit"
|
||||
/>
|
||||
</q-td>
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
.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
|
||||
updateProduct: async function (product) {
|
||||
try {
|
||||
await self.sendToRelays(productData, 'product', 'update')
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
const {data} = await LNbits.api.request(
|
||||
'PATCH',
|
||||
'/nostrmarket/api/v1/product/' + product.id,
|
||||
this.adminkey,
|
||||
product
|
||||
)
|
||||
const index = this.products.findIndex(r => r.id === product.id)
|
||||
if (index !== -1) {
|
||||
this.products.splice(index, 1, data)
|
||||
}
|
||||
self.resetDialog('productDialog')
|
||||
//self.productDialog.show = false
|
||||
//self.productDialog.data = {}
|
||||
this.$q.notify({
|
||||
type: 'positive',
|
||||
message: 'Product Updated',
|
||||
timeout: 5000
|
||||
})
|
||||
.catch(function (error) {
|
||||
} 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
|
||||
}
|
||||
},
|
||||
|
|
|
|||
25
views_api.py
25
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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue