Browse Source

note deletion completed

master
Christian Mueller 12 years ago
parent
commit
925e01a95b
  1. 4
      src/NoteHub/api.clj
  2. 26
      src/NoteHub/storage.clj
  3. 2
      src/NoteHub/views/pages.clj
  4. 2
      test/NoteHub/test/api.clj
  5. 27
      test/NoteHub/test/storage.clj

4
src/NoteHub/api.clj

@ -48,7 +48,7 @@
(defn- get-path [noteID & description] (defn- get-path [noteID & description]
(let [[year month day title] (split noteID #" ")] (let [[year month day title] (split noteID #" ")]
(if description (if description
(str domain "/" (storage/create-short-url {:year year :month month :day day :title title})) (str domain "/" (storage/create-short-url noteID {:year year :month month :day day :title title}))
(apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)]))))) (apply str (interpose "/" [domain year month day (ring.util.codec/url-encode title)])))))
(let [md5Instance (java.security.MessageDigest/getInstance "MD5")] (let [md5Instance (java.security.MessageDigest/getInstance "MD5")]
@ -94,7 +94,7 @@
(cons proposed-title (cons proposed-title
(map #(str proposed-title "-" (+ 2 %)) (range))))) (map #(str proposed-title "-" (+ 2 %)) (range)))))
noteID (build-key date title) noteID (build-key date title)
short-url (storage/create-short-url {:year year :month month :day day :title title})] short-url (storage/create-short-url noteID {:year year :month month :day day :title title})]
(do (do
(storage/add-note noteID note pid password) (storage/add-note noteID note pid password)
{:noteID noteID {:noteID noteID

26
src/NoteHub/storage.clj

@ -78,11 +78,6 @@
(redis :hincrby :views noteID 1) (redis :hincrby :views noteID 1)
(redis :hget :note noteID)))) (redis :hget :note noteID))))
(defn delete-note [noteID]
(doseq [kw [:password :views :note :published :edited :publisher]]
; TODO: delete short url by looking for the title
(redis :hdel kw noteID)))
(defn short-url-exists? [url] (defn short-url-exists? [url]
(= 1 (redis :hexists :short-url url))) (= 1 (redis :hexists :short-url url)))
@ -94,16 +89,23 @@
(when value ; TODO: necessary? (when value ; TODO: necessary?
(read-string value)))) (read-string value))))
(defn delete-short-url [noteID] (defn delete-short-url [url]
(let [value (redis :hget :short-url noteID)] (when-let [params (redis :hget :short-url url)]
(redis :hdel :short-url noteID) (redis :hdel :short-url params)
(redis :hdel :short-url value))) (redis :hdel :short-url url)))
(defn delete-note [noteID]
(doseq [kw [:password :views :note :published :edited :publisher]]
(redis :hdel kw noteID))
(doseq [url (redis :smembers (str noteID :urls))]
(delete-short-url url))
(redis :del (str noteID :urls)))
(defn create-short-url (defn create-short-url
"Creates a short url for the given request metadata or extracts "Creates a short url for the given request metadata or extracts
one if it was already created" one if it was already created"
[arg] [noteID params]
(let [key (str (into (sorted-map) arg))] (let [key (str (into (sorted-map) params))]
(if (short-url-exists? key) (if (short-url-exists? key)
(redis :hget :short-url key) (redis :hget :short-url key)
(let [hash-stream (partition 5 (repeatedly #(rand-int 36))) (let [hash-stream (partition 5 (repeatedly #(rand-int 36)))
@ -119,4 +121,6 @@
; s.t. we can later easily check whether a short url already exists ; s.t. we can later easily check whether a short url already exists
(redis :hset :short-url url key) (redis :hset :short-url url key)
(redis :hset :short-url key url) (redis :hset :short-url key url)
; we save all short urls of a note for removal later
(redis :sadd (str noteID :urls) url)
url)))) url))))

2
src/NoteHub/views/pages.clj

@ -113,7 +113,7 @@
(md-node :article.bottom-space (:note note)) (md-node :article.bottom-space (:note note))
(let [links (map #(link-to (let [links (map #(link-to
(if (= :short-url %) (if (= :short-url %)
(url (storage/create-short-url params)) (url (storage/create-short-url noteID params))
(str (:longURL note) "/" (name %))) (str (:longURL note) "/" (name %)))
(get-message %)) (get-message %))
[:stats :edit :export :short-url]) [:stats :edit :export :short-url])

2
test/NoteHub/test/api.clj

@ -47,7 +47,7 @@
(is (= (:shortURL post-response) (:shortURL get-response))) (is (= (:shortURL post-response) (:shortURL get-response)))
(is (storage/note-exists? (:noteID post-response))) (is (storage/note-exists? (:noteID post-response)))
(let [su (last (clojure.string/split (:shortURL get-response) #"/"))] (let [su (last (clojure.string/split (:shortURL get-response) #"/"))]
(is (= su (storage/create-short-url (storage/resolve-url su))))) (is (= su (storage/create-short-url (:noteID post-response) (storage/resolve-url su)))))
(let [resp (send-request (let [resp (send-request
(clojure.string/replace (:shortURL get-response) domain "")) (clojure.string/replace (:shortURL get-response) domain ""))
resp (send-request ((:headers resp) "Location"))] resp (send-request ((:headers resp) "Location"))]

27
test/NoteHub/test/storage.clj

@ -2,7 +2,8 @@
(:use [NoteHub.storage] (:use [NoteHub.storage]
[NoteHub.api :only [build-key]] [NoteHub.api :only [build-key]]
[NoteHub.views.pages] [NoteHub.views.pages]
[clojure.test])) [clojure.test])
(:require [taoensso.carmine :as car :refer (wcar)]))
(def date [2012 06 03]) (def date [2012 06 03])
(def test-title "Some title.") (def test-title "Some title.")
@ -13,22 +14,29 @@
:title test-title, :title test-title,
:theme "dark", :theme "dark",
:header-font "Anton"}) :header-font "Anton"})
(def test-short-url "")
(deftest storage (deftest storage
(testing "Storage" (testing "Storage"
(testing "of short-url mechanism" (testing "of short-url mechanism"
(let [url (create-short-url metadata) (let [fakeID (build-key date test-title)
url2 (create-short-url metadata)] url (create-short-url fakeID metadata)
url2 (create-short-url fakeID metadata)]
(is (= 1 (redis :scard (str fakeID :urls))))
(def test-short-url (create-short-url fakeID (assoc metadata :a :b)))
(is (= 2 (redis :scard (str fakeID :urls))))
(is (short-url-exists? url)) (is (short-url-exists? url))
(is (= url url2)) (is (= url url2))
(is (= metadata (resolve-url url))) (is (= metadata (resolve-url url)))
(is (not (do
(delete-short-url url) (delete-short-url url)
(short-url-exists? url)))))) (is (not (short-url-exists? url))))))
(testing "of correct note creation" (testing "of correct note creation"
(is (= (do (is (= (do
(add-note (build-key date test-title) test-note "testPID") (add-note (build-key date test-title) test-note "testPID")
(is (= 2 (redis :scard (str (build-key date test-title) :urls))))
(create-short-url (build-key date test-title) metadata)
(is (= 3 (redis :scard (str (build-key date test-title) :urls))))
(get-note (build-key date test-title))) (get-note (build-key date test-title)))
test-note)) test-note))
(is (= "1" (get-note-views (build-key date test-title)))) (is (= "1" (get-note-views (build-key date test-title))))
@ -59,8 +67,11 @@
(is (invalidate-session s3)))) (is (invalidate-session s3))))
(testing "of note existence" (testing "of note existence"
(is (note-exists? (build-key date test-title))) (is (note-exists? (build-key date test-title)))
(is (not (do (is (short-url-exists? test-short-url))
(is (= 3 (redis :scard (str (build-key date test-title) :urls))))
(delete-note (build-key date test-title)) (delete-note (build-key date test-title))
(note-exists? (build-key date test-title))))) (is (not (short-url-exists? test-short-url)))
(is (not (note-exists? (build-key date test-title))))
(is (= 0 (redis :scard (str (build-key date test-title) :urls))))
(is (not (note-exists? (build-key [2013 06 03] test-title)))) (is (not (note-exists? (build-key [2013 06 03] test-title))))
(is (not (note-exists? (build-key date "some title"))))))) (is (not (note-exists? (build-key date "some title"))))))

Loading…
Cancel
Save