create product

This commit is contained in:
Tiago vasconcelos 2022-07-12 17:22:03 +01:00
parent 5618de2cd3
commit 83fe977a85
4 changed files with 46 additions and 43 deletions

View file

@ -70,18 +70,19 @@ async def get_diagonalley_product(product_id: str) -> Optional[Products]:
return Products(**row) if row else None return Products(**row) if row else None
async def get_diagonalley_products(wallet_ids: Union[str, List[str]]) -> List[Products]: async def get_diagonalley_products(stall_ids: Union[str, List[str]]) -> List[Products]:
if isinstance(wallet_ids, str): if isinstance(stall_ids, str):
wallet_ids = [wallet_ids] stall_ids = [stall_ids]
# with open_ext_db("diagonalley") as db: # with open_ext_db("diagonalley") as db:
q = ",".join(["?"] * len(wallet_ids)) q = ",".join(["?"] * len(stall_ids))
rows = await db.fetchall( rows = await db.fetchall(
f""" f"""
SELECT * FROM diagonalley.products WHERE stall IN ({q}) SELECT * FROM diagonalley.products WHERE stall IN ({q})
""", """,
(*wallet_ids,), (*stall_ids,),
) )
print("PRODS", rows)
return [Products(**row) for row in rows] return [Products(**row) for row in rows]

View file

@ -24,7 +24,7 @@ class createStalls(BaseModel):
class createProduct(BaseModel): class createProduct(BaseModel):
stall: str = Query(None) stall: str = Query(...)
product: str = Query(None) product: str = Query(None)
categories: str = Query(None) categories: str = Query(None)
description: str = Query(None) description: str = Query(None)

View file

@ -9,7 +9,7 @@
dense dense
emit-value emit-value
v-model="productDialog.data.stall" v-model="productDialog.data.stall"
:options="stalls" :options="stalls.map(s => ({label: s.name, value: s.id}))"
label="Stall" label="Stall"
> >
</q-select> </q-select>
@ -100,7 +100,7 @@
v-else v-else
unelevated unelevated
color="primary" color="primary"
:disable="productDialog.data.image == null :disable="productDialog.data.price == null
|| productDialog.data.product == null || productDialog.data.product == null
|| productDialog.data.description == null || productDialog.data.description == null
|| productDialog.data.quantity == null" || productDialog.data.quantity == null"
@ -645,34 +645,38 @@
</div> </div>
</div> </div>
{% endblock %} {% block scripts %} {{ window_vars(user) }} {% endblock %} {% block scripts %} {{ window_vars(user) }}
<script src="https://cdn.jsdelivr.net/npm/pica@6.1.1/dist/pica.min.js"></script>
<script> <script>
Vue.component(VueQrcode.name, VueQrcode) Vue.component(VueQrcode.name, VueQrcode)
//const pica = window.pica() const pica = window.pica()
var mapStalls = obj => { const mapStalls = obj => {
obj._data = _.clone(obj) obj._data = _.clone(obj)
return obj return obj
} }
var mapProducts = obj => { const mapProducts = obj => {
obj._data = _.clone(obj) obj._data = _.clone(obj)
return obj return obj
} }
var mapZone = obj => { const mapZone = obj => {
obj._data = _.clone(obj) obj._data = _.clone(obj)
return obj return obj
} }
var mapOrders = obj => { const mapOrders = obj => {
obj._data = _.clone(obj) obj._data = _.clone(obj)
return obj return obj
} }
var mapKeys = obj => { const mapKeys = obj => {
obj._data = _.clone(obj) obj._data = _.clone(obj)
return obj return obj
} }
const humanReadableZones = (zones) => {
return zones.map(z => `${z.id} - ${z.countries}`)
}
new Vue({ new Vue({
el: '#vue', el: '#vue',
mixins: [windowMixin], mixins: [windowMixin],
@ -973,6 +977,7 @@ new Vue({
.then(function (response) { .then(function (response) {
if (response.data) { if (response.data) {
self.stalls = response.data.map(mapStalls) self.stalls = response.data.map(mapStalls)
console.log(self.stalls)
} }
}) })
.catch(function (error) { .catch(function (error) {
@ -980,20 +985,13 @@ new Vue({
}) })
}, },
openStallDialog: function () { openStallDialog: function () {
this.zoneOptions = this.zones.map(z => `${z.id} - ${z.countries}`) this.zoneOptions = humanReadableZones(this.zones)
//console.log(this.zones[0]['id'])
//for(let i = 0; i < this.zones.length; i ++){
// this.zoneOptions.push(this.zones[i]['id'])
//}
this.stallDialog.show = true this.stallDialog.show = true
}, },
openStallUpdateDialog: function (linkId) { openStallUpdateDialog: function (linkId) {
var self = this var self = this
var link = _.findWhere(self.stalls, {id: linkId}) var link = _.findWhere(self.stalls, {id: linkId})
this.zoneOptions = this.zones.map(z => `${z.id} - ${z.countries}`) this.zoneOptions = humanReadableZones(this.zones)
//for(let i = 0; i < this.zones.length; i ++){
// this.zoneOptions.push(this.stalls[i][0])
//}
this.stallDialog.data = _.clone(link._data) this.stallDialog.data = _.clone(link._data)
let shippingzones = this.zoneOptions.filter(z => this.stallDialog.data.shippingzones.split(",").includes(z.split("-")[0].trim())) let shippingzones = this.zoneOptions.filter(z => this.stallDialog.data.shippingzones.split(",").includes(z.split("-")[0].trim()))
@ -1103,10 +1101,11 @@ new Vue({
LNbits.api LNbits.api
.request( .request(
'GET', 'GET',
'/diagonalley/api/v1/products?all_stalls', '/diagonalley/api/v1/products?all_stalls=true',
self.g.user.wallets[0].inkey self.g.user.wallets[0].inkey
) )
.then(function (response) { .then(function (response) {
console.log(response.data)
if (response.data) { if (response.data) {
self.products = response.data.map(mapProducts) self.products = response.data.map(mapProducts)
} }
@ -1123,9 +1122,8 @@ new Vue({
self.productDialog.show = true self.productDialog.show = true
}, },
sendProductFormData: function () { sendProductFormData: function () {
if (this.productDialog.data.id) {
} else {
var data = { var data = {
stall: this.productDialog.data.stall,
product: this.productDialog.data.product, product: this.productDialog.data.product,
categories: categories:
this.productDialog.data.categories + this.productDialog.data.categories +
@ -1135,9 +1133,8 @@ new Vue({
price: this.productDialog.data.price, price: this.productDialog.data.price,
quantity: this.productDialog.data.quantity quantity: this.productDialog.data.quantity
} }
}
if (this.productDialog.data.id) { if (this.productDialog.data.id) {
this.updateProduct(this.productDialog.data) this.updateProduct({...this.productDialog.data, ...data})
} else { } else {
this.createProduct(data) this.createProduct(data)
} }
@ -1199,12 +1196,16 @@ new Vue({
}, },
createProduct: function (data) { createProduct: function (data) {
var self = this var self = this
let wallet = _.findWhere(this.stalls, {
id: self.productDialog.data.stall
}).wallet
console.log(wallet)
LNbits.api LNbits.api
.request( .request(
'POST', 'POST',
'/diagonalley/api/v1/products', '/diagonalley/api/v1/products',
_.findWhere(self.g.user.wallets, { _.findWhere(self.g.user.wallets, {
id: self.productDialog.data.wallet id: wallet
}).inkey, }).inkey,
data data
) )
@ -1570,7 +1571,7 @@ new Vue({
'cb4c0164fe03fcdadcbfb4f76611c71620790944c24f21a1cd119395cdedfe1b', 'cb4c0164fe03fcdadcbfb4f76611c71620790944c24f21a1cd119395cdedfe1b',
'a9c17358a6dc4ceb3bb4d883eb87967a66b3453a0f3199f0b1c8eef8070c6a07' 'a9c17358a6dc4ceb3bb4d883eb87967a66b3453a0f3199f0b1c8eef8070c6a07'
] ]
console.log(this.products.length) console.log(_.pick(this.g.user, "id"))
} }
} }
}) })

View file

@ -73,7 +73,6 @@ async def api_copilot_retrieve(
@diagonalley_ext.get("/api/v1/products") @diagonalley_ext.get("/api/v1/products")
async def api_diagonalley_products( async def api_diagonalley_products(
req: Request,
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(get_key_type),
all_stalls: bool = Query(False), all_stalls: bool = Query(False),
): ):
@ -82,13 +81,15 @@ async def api_diagonalley_products(
if all_stalls: if all_stalls:
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
return [product.dict() for product in await get_diagonalley_products(wallet_ids)] stalls = [stall.id for stall in await get_diagonalley_stalls(wallet_ids)]
return [product.dict() for product in await get_diagonalley_products(stalls)]
@diagonalley_ext.post("/api/v1/products") @diagonalley_ext.post("/api/v1/products")
@diagonalley_ext.put("/api/v1/products/{product_id}") @diagonalley_ext.put("/api/v1/products/{product_id}")
async def api_diagonalley_product_create( async def api_diagonalley_product_create(
product_id, data: createProduct, wallet: WalletTypeInfo = Depends(get_key_type) data: createProduct, product_id = None, wallet: WalletTypeInfo = Depends(get_key_type)
): ):
if product_id: if product_id: