summaryrefslogtreecommitdiff
path: root/bin/fetch.fnl
diff options
context:
space:
mode:
authorunwox <me@unwox.com>2024-10-30 12:28:41 +0600
committerunwox <me@unwox.com>2024-10-30 12:28:41 +0600
commite0e990a248e3a18f88d7a12f3c353b8664bd6898 (patch)
tree040ca39f28d96b72aa2637a44e8602b73031fdc3 /bin/fetch.fnl
parent8daa0b56f8e53c34706870462b346749b824a9a7 (diff)
archive old products and do not display them
Diffstat (limited to 'bin/fetch.fnl')
-rw-r--r--bin/fetch.fnl18
1 files changed, 15 insertions, 3 deletions
diff --git a/bin/fetch.fnl b/bin/fetch.fnl
index 4558c50..a3155a4 100644
--- a/bin/fetch.fnl
+++ b/bin/fetch.fnl
@@ -98,11 +98,12 @@
(fn store-products [tx products]
(when (< 0 (# products))
(local sql
- (.. "INSERT OR IGNORE INTO products VALUES "
+ (.. "INSERT OR REPLACE INTO products VALUES "
(array.join
(map (fn [_ _] "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
products)
", ")))
+ (local creation-time (now))
(local vars
(reduce
(fn [_ product rest]
@@ -117,7 +118,7 @@
(or product.weight 0)
(or product.price-per 0)
false
- (now)]))
+ creation-time]))
products []))
(must (luna.db.exec-tx tx sql vars))
@@ -125,7 +126,18 @@
(store-tags tx (array.unique
(array.flatten
(map (fn [_ v] v.tags) products))))
- (store-product-tags tx products)))
+ (store-product-tags tx products)
+
+ ;; archive previous products
+ (local site (. products 1 :site))
+ (when site
+ (must (luna.db.exec-tx
+ tx
+ "UPDATE products
+ SET archived = true
+ WHERE site = ?
+ AND creation_time < ?"
+ [site creation-time])))))
(fn populate-search-table []
(local tx (must (luna.db.begin db)))