Commits

Miki Tebeka committed 058e9ac

Phone register works

Comments (0)

Files changed (1)

src/duderemember.clj

       (first users))))
 
 (defn user->phone [user]
-  (let [entities (select "users" where (= :user-id (:user-id user)))]
+  (let [entities (find-users-by-id (:user-id user))]
     (if (zero? (count entities))
       nil
       (:phone (first entities)))))
   (nil? (user->phone user)))
 
 (defn update-user [u phone]
-  (if (new-user? u)
-    (let [entity (user {:id (:user-id u) :phone phone})]
-      (save-entity entity))
-    (let [entity (first (find-users-by-id (:user-id u)))
-          phone (digits-only phone)]
-      (update-entity entity {:phone phone}))))
+  (let [phone (digits-only phone)]
+    (if (new-user? u)
+      (save-entity (user {:id (:user-id u) :phone phone}))
+      (let [entity (first (find-users-by-id (:user-id u)))]
+        (update-entity entity {:phone phone})
+        (save-entity entity)))))
 
 (defn add-message [user text]
   (let [now (System/currentTimeMillis)
 (defn user-memos [user]
   (select "message"
     where (= :user-id (:user-id user))
-    order-by (:time)))
+    order-by (:time :desc)))
 
 (def *title* "Dude! ... Remember!")
 
-(defn page
-  "HTML page skeleton"
-  [& content]
+(defn page [& content]
   (html
     [:head
      (include-css "/static/dude.css")
      [:h1 [:a {:href "/"} [:img {:src "/static/sms.png"}]] *title*]
      content]))
 
-(defn format-time 
-  "Format a time stamp (milliseconds since epoch)"
-  [t]
+(defn format-time [t]
   (let [date (new java.util.Date)]
     (.setTime date t)
     (str date)))
 (defn user-page [user]
   (let [memos (user-memos user)]
     (page
-      [:h2 "Memos for " (:nickname user) "(" (user->phone user) ")"]
+      [:h2 "Memos for " (:nickname user) " (" (user->phone user) ")"]
       (render-memos memos))))
 
+(defn eval-page [expr]
+  (page
+    (str (load-string (str "(in-ns 'duderemember)" expr)))
+    (form-to [:post]
+      [:br]
+      (text-field :expr)
+      (submit-button "Eval"))))
+
 (defn phone-page []
   (page
     "Please update your phone number" [:br]
        (submit-button "Update"))))
 
 (defn twilio [params]
-  "Handle Twilio SMS event"
   (let [phone (params "From")
         text (params "Body")
         user (phone->user phone)]
           (update-user (current-user) phone)
           (redirect "/")))
   (POST "/twilio/sms" {params :params} (twilio params))
-  (route/not-found "Dude! I can't find it."))
+  (route/not-found "Dude! I can't find it.")
+           
+  (GET "/_eval" [] (eval-page "0"))
+  (POST "/_eval" {params :params} (eval-page (params "expr"))))
 
 (defservice duderemeber-routes)