From f0405c574a278d4f85b47ac45eb54bab9758a9a8 Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 6 Apr 2014 14:37:44 -0400 Subject: [PATCH] implement basic operation for adding new ascii art --- src/toascii/models/art.clj | 17 +++++++++++++++-- src/toascii/models/db.clj | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/toascii/models/art.clj b/src/toascii/models/art.clj index 8803757..be77ccd 100644 --- a/src/toascii/models/art.clj +++ b/src/toascii/models/art.clj @@ -1,5 +1,15 @@ (ns toascii.models.art - (:require [toascii.models.db :as db])) + (:require [toascii.models.db :as db] + [toascii.util :refer [convert-line-endings remove-leading-and-trailing-blank-lines]])) + +(defn- prep-ascii-art [s] + (-> s + (convert-line-endings) + (remove-leading-and-trailing-blank-lines))) + +(defn valid-name? [name] + (if-not (nil? name) + (re-matches #"[A-Za-z0-9\-]+" name))) (defn search [query] ) @@ -14,4 +24,7 @@ ) (defn add [name s ip] - ) + (if-not (valid-name? name) + (throw (new Exception "Invalid name"))) + (let [prepped-ascii (prep-ascii-art s)] + (db/add-ascii-art name prepped-ascii ip))) diff --git a/src/toascii/models/db.clj b/src/toascii/models/db.clj index bc31fcb..b7d3c53 100644 --- a/src/toascii/models/db.clj +++ b/src/toascii/models/db.clj @@ -1,7 +1,8 @@ (ns toascii.models.db (:require [com.ashafa.clutch :as couch] [cemerick.url :as url] - [toascii.config :refer [config-val]])) + [toascii.config :refer [config-val]] + [toascii.util :refer [now sha256]])) (defn db-url [db-name] (let [db-config (config-val :database) @@ -24,3 +25,20 @@ (map first x) (seq x))) +(defn existing-ascii-art? [name hash] + (->> (couch/get-view-with-db (db-library) "search" "hashes" {:key [name hash]}) + (seq))) + +(defn add-ascii-art [name ascii ip] + (let [hash (sha256 ascii)] + (println name hash) + (if (existing-ascii-art? name hash) + (throw (new Exception (str "Existing ASCII art with same name and art found"))) + (couch/put-document-with-db + (db-library) + {:name name + :date (now) + :ip ip + :format "ascii" + :hash (sha256 ascii) + :content ascii})))) \ No newline at end of file