Commits

Miki Tebeka committed 4204d95

long -> original and getting short works

  • Participants
  • Parent commits 07b2229

Comments (0)

Files changed (1)

File src/biggieurl.clj

 
 (defentity Url ()
   ((short :key identity)
-   (long)))
+   (original)))
 
 (def *title* "BiggieURL: The not-so-small URL Shortener")
+(def *jquery-url* 
+  "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js")
+
 
 (defn header-link []
   (if (nil? (current-user))
       "Someone"
       (:nickname user))))
 
-(def *jquery-url* 
-  "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js")
+(defn original-url [short-url]
+  (let [u (find-entity (make-key "url" short-url))]
+    (when u
+      (redirect (:original u)))))
 
-(defn page [& content]
+(defn full-url [url]
+  (if (re-matches #"^[a-z]{3,5}://.*" url)
+    url
+    (str "http://" url)))
+
+(defn create-new-url [original-url]
+  (let [short-url (uuid)
+        u (url {:short short-url :original (full-url original-url)})]
+    (save-entity u)
+    short-url))
+
+(defn shorten-url [params]
+  (let [original-url (full-url (params "url"))
+        urls (find-urls-by-original original-url)]
+    (if (empty? urls) ; New URL
+      (create-new-url original-url)
+      (:short (first urls)))))
+
+(defn home-page []
   (html
     [:head
      (include-css "/static/site.css")
     [:body 
      [:div {:class "header"} "Hello " (user-name) " [" (header-link) "]"]
      [:h1 *title*]
-     content]))
-
-; FIXME
-(defn long-url [url]
-  (println (str "LONG: " url))
-  "OK")
-
-(defn create-new-url [long-url]
-  (let [short-url (uuid)
-        u (url {:short short-url :long long-url})]
-    (save-entity u)
-    short-url))
-
-(defn shorten-url [params]
-  (let [long-url (params "url")
-        urls (find-urls-by-long long-url)]
-    (if (empty? urls) ; New URL
-      (create-new-url long-url)
-      (:short (first urls)))))
-
-(defn home-page []
-  (page
-    (form-to [:post]
-      "URL: "
-      [:input {:type "text" :name "url" :size "100"}]
-      (submit-button "Shorten"))
-    (:div {:id "shortened" :class "shortened"})))
+     (form-to [:post]
+       "URL: "
+       [:input {:type "text" :name "url" :size "100"}]
+       (submit-button "Shorten"))
+     (:div {:id "shortened" :class "shortened"})]))
 
 (defroutes biggieurl-routes
   (GET "/" [] (home-page))
   (POST "/shorten" {params :params} (shorten-url params))
-;  (GET "/:url" [url] (or (long-url url) :next))
+  (GET "/:url" [url] (or (original-url url) :next))
   (route/not-found "Sorry, can't find it."))
 
 (defservice biggieurl-routes)