Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First stab at a JSON document storage service example.

  • Loading branch information...
commit 9c1c51218ccb767738ac371b236ebaf1b1bae992 1 parent 3358465
@aeden authored
View
6 docstore/clojure/docstore/.gitignore
@@ -0,0 +1,6 @@
+pom.xml
+*jar
+/lib/
+/classes/
+.lein-failures
+.lein-deps-sum
View
13 docstore/clojure/docstore/README
@@ -0,0 +1,13 @@
+# docstore
+
+Web service for storing a JSON document
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2012 Anthony Eden
+
+Distributed under the MIT License.
View
8 docstore/clojure/docstore/project.clj
@@ -0,0 +1,8 @@
+(defproject docstore "1.0.0-SNAPSHOT"
+ :description "Web service that stores a document"
+ :dependencies [[org.clojure/clojure "1.2.1"]
+ [ring/ring "1.1.0"]
+ [org.clojure/java.jdbc "0.0.6"]
+ [postgresql/postgresql "8.4-702.jdbc4"]]
+ :dev-dependencies [[lein-exec "0.1"]
+ [ring-mock "0.1.2"]])
View
5 docstore/clojure/docstore/schema.sql
@@ -0,0 +1,5 @@
+CREATE TABLE documents (
+ id SERIAL PRIMARY KEY,
+ name varchar(255) not null,
+ body text
+);
View
26 docstore/clojure/docstore/src/docstore/core.clj
@@ -0,0 +1,26 @@
+(ns docstore.core
+ (:require [clojure.java.jdbc :as sql]))
+
+(def db
+ {:classname "org.postgresql.Driver"
+ :subprotocol "postgresql"
+ :subname "//localhost:5432/service-examples"
+ :user "aeden"
+ :password ""})
+
+(defn create-document [name body]
+ (sql/with-connection db
+ (sql/insert-record :documents {:name name :body body})))
+
+(defn find-document [name]
+ (peek (sql/with-connection db
+ (sql/with-query-results results
+ ["select * from documents where name = ? limit 1" name]
+ (into [] results)))))
+
+(defn app [request]
+ (let [body (slurp (request :body))]
+ (create-document (request :uri) body)
+ {:status 201
+ :headers {"Content-Type" "application/json"}
+ :body body}))
View
12 docstore/clojure/docstore/test/docstore/test/core.clj
@@ -0,0 +1,12 @@
+(ns docstore.test.core
+ (:use [docstore.core])
+ (:use [clojure.test])
+ (:use [ring.mock.request]))
+
+(deftest app-test
+ (let [document "{\"title\": \"Test Document\", \"author\":\"Bob\"}"]
+ (is (= (app (body (request :post "/test-document"), document))
+ {:status 201
+ :headers {"Content-Type" "application/json"}
+ :body document}))
+ (is (= "{\"title\": \"Test Document\", \"author\":\"Bob\"}" (get (find-document "/test-document") :body)))))
Please sign in to comment.
Something went wrong with that request. Please try again.