Skip to content

Commit

Permalink
Changes to deactivate/reactivate products (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
rleed authored Nov 4, 2024
1 parent 7a3b144 commit 2020bd9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
10 changes: 6 additions & 4 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ async def create_product(merchant_id: str, data: PartialProduct) -> Product:
await db.execute(
f"""
INSERT INTO nostrmarket.products
(merchant_id, id, stall_id, name, price, quantity, pending, event_id, event_created_at, image_urls, category_list, meta)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
(merchant_id, id, stall_id, name, price, quantity, active, pending, event_id, event_created_at, image_urls, category_list, meta)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO NOTHING
""",
(
Expand All @@ -303,6 +303,7 @@ async def create_product(merchant_id: str, data: PartialProduct) -> Product:
data.name,
data.price,
data.quantity,
data.active,
data.pending,
data.event_id,
data.event_created_at,
Expand All @@ -321,13 +322,14 @@ async def update_product(merchant_id: str, product: Product) -> Product:

await db.execute(
f"""
UPDATE nostrmarket.products set name = ?, price = ?, quantity = ?, pending = ?, event_id =?, event_created_at = ?, image_urls = ?, category_list = ?, meta = ?
UPDATE nostrmarket.products set name = ?, price = ?, quantity = ?, active = ?, pending = ?, event_id =?, event_created_at = ?, image_urls = ?, category_list = ?, meta = ?
WHERE merchant_id = ? AND id = ?
""",
(
product.name,
product.price,
product.quantity,
product.active,
product.pending,
product.event_id,
product.event_created_at,
Expand Down Expand Up @@ -385,7 +387,7 @@ async def get_products_by_ids(
q = ",".join(["?"] * len(product_ids))
rows = await db.fetchall(
f"""
SELECT id, stall_id, name, price, quantity, category_list, meta
SELECT id, stall_id, name, price, quantity, active, category_list, meta
FROM nostrmarket.products
WHERE merchant_id = ? AND pending = false AND id IN ({q})
""",
Expand Down
5 changes: 5 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,9 @@ async def m003_update_direct_message_type(db):
async def m004_add_merchant_timestamp(db):
await db.execute(
f"ALTER TABLE nostrmarket.merchants ADD COLUMN time TIMESTAMP;"
)

async def m005_update_product_activation(db):
await db.execute(
"ALTER TABLE nostrmarket.products ADD COLUMN active BOOLEAN NOT NULL DEFAULT true;"
)
23 changes: 14 additions & 9 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class PartialProduct(BaseModel):
images: List[str] = []
price: float
quantity: int
active: bool = True
pending: bool = False
config: ProductConfig = ProductConfig()

Expand All @@ -257,20 +258,24 @@ def to_nostr_event(self, pubkey: str) -> NostrEvent:
"currency": self.config.currency,
"price": self.price,
"quantity": self.quantity,
"active": self.active,
"shipping": [dict(s) for s in self.config.shipping or []]
}
categories = [["t", tag] for tag in self.categories]

event = NostrEvent(
pubkey=pubkey,
created_at=round(time.time()),
kind=30018,
tags=[["d", self.id]] + categories,
content=json.dumps(content, separators=(",", ":"), ensure_ascii=False),
)
event.id = event.event_id
if self.active:
event = NostrEvent(
pubkey=pubkey,
created_at=round(time.time()),
kind=30018,
tags=[["d", self.id]] + categories,
content=json.dumps(content, separators=(",", ":"), ensure_ascii=False),
)
event.id = event.event_id

return event
return event
else:
return self.to_nostr_delete_event(pubkey)

def to_nostr_delete_event(self, pubkey: str) -> NostrEvent:
delete_event = NostrEvent(
Expand Down
10 changes: 10 additions & 0 deletions static/components/stall-details/stall-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@
<q-td auto-width>
<q-btn size="sm" color="primary" dense @click="editProduct(props.row)" icon="edit" />
</q-td>
<q-td auto-width>
<q-toggle
@input="updateProduct({ ...props.row, active: props.row.active })"
size="xs"
checked-icon="check"
v-model="props.row.active"
color="green"
unchecked-icon="clear"
/>
</q-td>

<q-td key="id" :props="props"> {{props.row.id}} </q-td>
<q-td key="name" :props="props"> {{shortLabel(props.row.name)}} </q-td>
Expand Down
6 changes: 6 additions & 0 deletions static/components/stall-details/stall-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ async function stallDetails(path) {
label: '',
field: ''
},
{
name: 'activate',
align: 'left',
label: '',
field: ''
},

{
name: 'id',
Expand Down

1 comment on commit 2020bd9

@PatMulligan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this requires an update to the frontend, see lnbits/nostr-market-app#18

Please sign in to comment.