Miki Tebeka avatar Miki Tebeka committed 07b2229

Creating short url

Comments (0)

Files changed (1)

src/biggieurl.clj

         compojure.core
         hiccup.core
         hiccup.form-helpers
-        hiccup.page-helpers
+        [hiccup.page-helpers :only (include-css include-js)]
         ring.util.response
         ring.util.servlet)
   (:require [compojure.route :as route]
   (:import java.util.UUID))
 
 (defn uuid []
-  (apply str (re-seq "#[0-9a-f]+" (str (UUID/randomUUID)))))
+  (apply str (re-seq #"[0-9a-f]+" (str (UUID/randomUUID)))))
 
-(defentity BURL ()
-  ((original)
-   (shortened)
-   (user-id) ; We call it user-id to match Google user object
-   (hits))) 
+(defentity Url ()
+  ((short :key identity)
+   (long)))
 
 (def *title* "BiggieURL: The not-so-small URL Shortener")
 
      content]))
 
 ; FIXME
-(defn long-url [url])
-(defn shorten-url [url])
+(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
 (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 (long-url url) :next))
   (route/not-found "Sorry, can't find it."))
 
 (defservice biggieurl-routes)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.