Jelajahi Sumber

flattened menu model, added IDs and queries.

Harlan Iverson 5 tahun lalu
induk
melakukan
837e66e9bf
1 mengubah file dengan 66 tambahan dan 42 penghapusan
  1. 66 42
      src/pos_demo/core.cljs

+ 66 - 42
src/pos_demo/core.cljs

@@ -10,6 +10,7 @@
 
 (defonce app-state (atom {:shop-name "Hola world!"
                           :active-order nil
+                          :customize-menu-item nil
                           :payment-amount "0.00"}))
 
 
@@ -160,64 +161,87 @@
 
 
 
-(def menu (atom (map->Menu {:sections [
-  (map->MenuSection {:title "Salads" :items [
-    (map->MenuItem {:title "Caesar" :description "" :price "11.95" :ingredients [
-      (map->MenuItemIngredient {:ingredient "Kale" :amount 1.2 :description "The leafy green ingredient."})
-      (map->MenuItemIngredient {:ingredient "Caesar Dressing" :amount 1 :description "Creamy dressing."})
-      (map->MenuItemIngredient {:ingredient "Bacon" :amount 0.4 :description "Adds crunch and salty flavor."})
-      ]})
-    ]})
-  (map->MenuSection {:title "Sandos" :items [
-    (map->MenuItem {:title "Grilled Cheese" :description ""  :price "8.95" :ingredients [
-    	(map->MenuItemIngredient {:ingredient "Bread" :amount 2 :description "White Bread for the sando."})
-    	(map->MenuItemIngredient {:ingredient "Cheese" :amount 1 :description "American Singles, baby."})
-    	(map->MenuItemIngredient {:ingredient "Butter" :amount 0.25 :description "Bread is buttered before grilling."})
-    	
-      ]})
-    (map->MenuItem {:title "PB & J" :description "" :price "6.95" :ingredients [
-    	(map->MenuItemIngredient {:ingredient "Bread" :amount 2 :description "White Bread for the sando."})
-    	(map->MenuItemIngredient {:ingredient "Peanut Butter" :amount 0.25 :description "Crunchy roasted peanut butter."})
-    	(map->MenuItemIngredient {:ingredient "Grape Preserves" :amount 0.25 :description "Concord grape preserves."})
-    	
-      ]})
-    ]})
-  ]})))
+#_ (defrecord Sale [description conditions price-adjustment])
 
+#_ (when [?cart :cart/item ?item1]
+         [?cart :cart/item ?item2]
+         
+         [?item1 :product/category ?cat1]
+         [?item2 :product/category ?cat2]
+         
+         [?item1 :product/price ?p1]
+         [?item2 :product/price ?p2]
+         
+         (= "Sandos" ?cat1 ?cat2)
+         
+         (then
+           (insert! (->Sale "50% off Summer" "Buy 3 or more." (* -1 0.50 (+ ?p1 ?p2 ?p3))))))
 
 
-(def menu2 (atom (let [m (map->Menu {})
-                       salads  (map->MenuSection {:menu m :title "Salads"})
-                       caesar (map->MenuItem {:menu-section salads :title "Caesar" :description "" :price "11.95"})
-                       sandos (map->MenuSection {:title "Sandos"})
-                       grilled-cheese (map->MenuItem {:menu-section sandos :title "Grilled Cheese" :description "" :price "8.95"})
-                       pb-and-j (map->MenuItem {:menu-section sandos :title "PB & J" :description "" :price "6.95"})
+(def menu (atom (let [m (map->Menu {:id 1})
+                       salads  (map->MenuSection {:id 10 :menu (:id m) :title "Salads"})
+                       caesar (map->MenuItem {:id 100 :menu-section (:id salads) :title "Caesar" :description "" :price "11.95"})
+                       sandos (map->MenuSection {:id 11 :menu (:id m) :title "Sandos"})
+                       grilled-cheese (map->MenuItem {:id 101 :menu-section (:id sandos) :title "Grilled Cheese" :description "" :price "8.95"})
+                       pb-and-j (map->MenuItem {:id 102 :menu-section (:id sandos) :title "PB & J" :description "" :price "6.95"})
                        ]
              [m salads caesar 
     
-      (map->MenuItemIngredient {:menu-item caesar :ingredient "Kale" :amount 1.2 :description "The leafy green ingredient."})
-      (map->MenuItemIngredient {:menu-item caesar :ingredient "Caesar Dressing" :amount 1 :description "Creamy dressing."})
-      (map->MenuItemIngredient {:menu-item caesar :ingredient "Bacon" :amount 0.4 :description "Adds crunch and salty flavor."})
+      (map->MenuItemIngredient {:id 1000 :menu-item (:id caesar) :ingredient "Kale" :amount 1.2 :description "The leafy green ingredient."})
+      (map->MenuItemIngredient {:id 1001 :menu-item (:id caesar) :ingredient "Caesar Dressing" :amount 1 :description "Creamy dressing."})
+      (map->MenuItemIngredient {:id 1002 :menu-item (:id caesar) :ingredient "Bacon" :amount 0.4 :description "Adds crunch and salty flavor."})
       
     
   sandos
     grilled-cheese
     
-    	(map->MenuItemIngredient {:menu-item grilled-cheese :ingredient "Bread" :amount 2 :description "White Bread for the sando."})
-    	(map->MenuItemIngredient {:menu-item grilled-cheese :ingredient "Cheese" :amount 1 :description "American Singles, baby."})
-    	(map->MenuItemIngredient {:menu-item grilled-cheese :ingredient "Butter" :amount 0.25 :description "Bread is buttered before grilling."})
+    	(map->MenuItemIngredient {:id 1003 :menu-item (:id grilled-cheese) :ingredient "Bread" :amount 2 :description "White Bread for the sando."})
+    	(map->MenuItemIngredient {:id 1004 :menu-item (:id grilled-cheese) :ingredient "Cheese" :amount 1 :description "American Singles, baby."})
+    	(map->MenuItemIngredient {:id 1005 :menu-item (:id grilled-cheese) :ingredient "Butter" :amount 0.25 :description "Bread is buttered before grilling."})
     	
     pb-and-j
     
-    	(map->MenuItemIngredient {:menu-item pb-and-j :ingredient "Bread" :amount 2 :description "White Bread for the sando."})
-    	(map->MenuItemIngredient {:menu-item pb-and-j :ingredient "Peanut Butter" :amount 0.25 :description "Crunchy roasted peanut butter."})
-    	(map->MenuItemIngredient {:menu-item pb-and-j :ingredient "Grape Preserves" :amount 0.25 :description "Concord grape preserves."})
+    	(map->MenuItemIngredient {:id 1006 :menu-item (:id pb-and-j) :ingredient "Bread" :amount 2 :description "White Bread for the sando."})
+    	(map->MenuItemIngredient {:id 1007 :menu-item (:id pb-and-j) :ingredient "Peanut Butter" :amount 0.25 :description "Crunchy roasted peanut butter."})
+    	(map->MenuItemIngredient {:id 1008 :menu-item (:id pb-and-j) :ingredient "Grape Preserves" :amount 0.25 :description "Concord grape preserves."})
     	
   ])))
 
+(defn find-menu
+  [id]
+  (first (filter #(and (instance? Menu %)
+                (= (:id %) id)) @menu)))
+
+
+(defn find-menu-section
+  [id]
+  (first (filter #(and (instance? MenuSection %)
+                (= (:id %) id)) @menu)))
+
+(defn find-menu-item
+  [id]
+  (first (filter #(and (instance? MenuItem %)
+                (= (:id %) id)) @menu)))
+
+(defn find-menu-item-ingredient
+  [id]
+  (first (filter #(and (instance? MenuItemIngredient %)
+                (= (:id %) id)) @menu)))
 
+(defn find-menu-sections
+  [menu-id]
+  (filter #(and (instance? MenuSection %)
+                (= (:menu %) menu-id)) @menu))
 
+(defn find-menu-items
+  [section-id]
+  (filter #(and (instance? MenuItem %)
+                (= (:menu-section %) section-id)) @menu))
 
+(defn find-menu-item-ingredients
+  [menu-item-id]
+  (filter #(and (instance? MenuItemIngredient %)
+                (= (:menu-item %) menu-item-id)) @menu))
 
 ; UI
 
@@ -271,9 +295,9 @@
   ; each section has the menu items
   ; the item can have ingredients removed, added, put on the side. ingredients from this or any item.
   ; there can be types of ingredients like sauces and patties.
-  (for [section (:sections menu)]
+  (for [section (find-menu-sections (:id menu))]
     [:div (:title section)
-      (for [item (:items section)]  
+      (for [item (find-menu-items (:id section))]  
         [button (:title item) #(menu-item-clicked menu item)]
           )]))
 
@@ -299,7 +323,7 @@
     
     [:p "Ingredients"]
     [:ul
-      (for [k (:ingredients menu-item)]
+      (for [k (find-menu-item-ingredients (:id menu-item))]
         [:li (:ingredient k) " -- " (:description k)])]
     
     [:div.custom-price [:strong "Price:"] (:price menu-item)]    
@@ -316,7 +340,7 @@
        (button "Resume Order" load-order-clicked)])
    (when-not (nil? (:active-order @app-state))
      [:div
-       (menu-ui @menu)
+       (menu-ui (find-menu 1))
        (when-let [menu-item (:customize-menu-item @app-state)]
          [:div
            [:hr]