From 925e01a95bc954109a616fc0a3a6077f27e2bd16 Mon Sep 17 00:00:00 2001 From: Christian Mueller Date: Sat, 25 Jan 2014 12:24:00 +0100 Subject: [PATCH] note deletion completed --- src/NoteHub/api.clj | 4 ++-- src/NoteHub/storage.clj | 28 ++++++++++++++++------------ src/NoteHub/views/pages.clj | 2 +- test/NoteHub/test/api.clj | 2 +- test/NoteHub/test/storage.clj | 31 +++++++++++++++++++++---------- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/NoteHub/api.clj b/src/NoteHub/api.clj index 7311dfd..b76c76b 100644 --- a/src/NoteHub/api.clj +++ b/src/NoteHub/api.clj @@ -48,7 +48,7 @@ (defn- get-path [noteID & description] (let [[year month day title] (split noteID #" ")] (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)]))))) (let [md5Instance (java.security.MessageDigest/getInstance "MD5")] @@ -94,7 +94,7 @@ (cons proposed-title (map #(str proposed-title "-" (+ 2 %)) (range))))) 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 (storage/add-note noteID note pid password) {:noteID noteID diff --git a/src/NoteHub/storage.clj b/src/NoteHub/storage.clj index acb2c50..db3f431 100644 --- a/src/NoteHub/storage.clj +++ b/src/NoteHub/storage.clj @@ -78,11 +78,6 @@ (redis :hincrby :views noteID 1) (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] (= 1 (redis :hexists :short-url url))) @@ -94,16 +89,23 @@ (when value ; TODO: necessary? (read-string value)))) -(defn delete-short-url [noteID] - (let [value (redis :hget :short-url noteID)] - (redis :hdel :short-url noteID) - (redis :hdel :short-url value))) +(defn delete-short-url [url] + (when-let [params (redis :hget :short-url url)] + (redis :hdel :short-url params) + (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 "Creates a short url for the given request metadata or extracts one if it was already created" - [arg] - (let [key (str (into (sorted-map) arg))] + [noteID params] + (let [key (str (into (sorted-map) params))] (if (short-url-exists? key) (redis :hget :short-url key) (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 (redis :hset :short-url url key) (redis :hset :short-url key url) - url)))) \ No newline at end of file + ; we save all short urls of a note for removal later + (redis :sadd (str noteID :urls) url) + url)))) diff --git a/src/NoteHub/views/pages.clj b/src/NoteHub/views/pages.clj index c23de4b..39ae8d4 100644 --- a/src/NoteHub/views/pages.clj +++ b/src/NoteHub/views/pages.clj @@ -113,7 +113,7 @@ (md-node :article.bottom-space (:note note)) (let [links (map #(link-to (if (= :short-url %) - (url (storage/create-short-url params)) + (url (storage/create-short-url noteID params)) (str (:longURL note) "/" (name %))) (get-message %)) [:stats :edit :export :short-url]) diff --git a/test/NoteHub/test/api.clj b/test/NoteHub/test/api.clj index 3777910..987c3cc 100644 --- a/test/NoteHub/test/api.clj +++ b/test/NoteHub/test/api.clj @@ -47,7 +47,7 @@ (is (= (:shortURL post-response) (:shortURL get-response))) (is (storage/note-exists? (:noteID post-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 (clojure.string/replace (:shortURL get-response) domain "")) resp (send-request ((:headers resp) "Location"))] diff --git a/test/NoteHub/test/storage.clj b/test/NoteHub/test/storage.clj index dde7302..0f738bc 100644 --- a/test/NoteHub/test/storage.clj +++ b/test/NoteHub/test/storage.clj @@ -2,7 +2,8 @@ (:use [NoteHub.storage] [NoteHub.api :only [build-key]] [NoteHub.views.pages] - [clojure.test])) + [clojure.test]) + (:require [taoensso.carmine :as car :refer (wcar)])) (def date [2012 06 03]) (def test-title "Some title.") @@ -13,22 +14,29 @@ :title test-title, :theme "dark", :header-font "Anton"}) +(def test-short-url "") (deftest storage (testing "Storage" (testing "of short-url mechanism" - (let [url (create-short-url metadata) - url2 (create-short-url metadata)] + (let [fakeID (build-key date test-title) + 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 (= url url2)) (is (= metadata (resolve-url url))) - (is (not (do - (delete-short-url url) - (short-url-exists? url)))))) + (delete-short-url url) + (is (not (short-url-exists? url)))))) (testing "of correct note creation" (is (= (do (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))) test-note)) (is (= "1" (get-note-views (build-key date test-title)))) @@ -59,8 +67,11 @@ (is (invalidate-session s3)))) (testing "of note existence" (is (note-exists? (build-key date test-title))) - (is (not (do - (delete-note (build-key date test-title)) - (note-exists? (build-key date test-title))))) + (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)) + (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 date "some title"))))))) + (is (not (note-exists? (build-key date "some title"))))))