]> us-delaporte.pagekite.me Git - matrix-shop-bot.git/blobdiff - matrix_shop_bot/db.py
No case
[matrix-shop-bot.git] / matrix_shop_bot / db.py
index cacac97b5c281c707ec84aed25476de873e32dc5..ce0d46ec2681d31ca64dda6c68bc7ea356d1f3f5 100644 (file)
@@ -67,7 +67,7 @@ class PurchaseRecord(SQLModel, table=True):
     purchase_date: dt | None = Field(default=None)
     def __str__(self):
         date = self.purchase_date.strftime(DATE_FORMAT)
-        return f'{self.name} at {self.store} on {date}'
+        return f'Bought {self.name} at {self.store} on {date}'
 
 def _get_with_id(cls, item_id):
     return select(cls).where(cls.id == item_id)
@@ -150,7 +150,7 @@ class Data():
 
     def list_purchases(self, store_name):
         with Session(self.engine) as session:
-            cmd = select(PurchaseRecord).where(PurchaseRecord.store == store_name)
+            cmd = select(PurchaseRecord).where(func.lower(PurchaseRecord.store) == func.lower(store_name))
             records = session.exec(cmd).all()
             return records
 
@@ -183,13 +183,20 @@ class Data():
         return results
 
 
-    def clear_list(self, store_id, clear_all=False):
+    def clear_list(self, store_name, clear_all=False):
+        results = []
         with Session(self.engine) as session:
-            store = session.exec(_get_with_id(Store, store_id)).first()
+            store = session.exec(_get_with_name(Store, store_name)).first()
+            if not store:
+                return [f'Store {store_name} not found.']
+
             for item in store.shopping_list:
                 if clear_all or item.purchase_date:
                     session.delete(item)
+
             session.commit()
+            results = store.get_pretty_shopping_list()
+        return results
 
     def get_stores(self):
         with Session(self.engine) as session: