Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tero Parviainen committed Jun 15, 2010
0 parents commit 911ea9b
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pom.xml
*jar
lib
classes
build.xml
manifest.mf
nbproject
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2010 Tero Parviainen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
15 changes: 15 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# todos-server

FIXME: write description

## Usage

FIXME: write

## Installation

FIXME: write

## License

FIXME: write
8 changes: 8 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(defproject todos-server "1.0.0-SNAPSHOT"
:description "Backend for the SproutCore tutorial app"
:dependencies [[org.clojure/clojure "1.1.0"]
[org.clojure/clojure-contrib "1.1.0"]

[compojure "0.4.0-RC3"]
[congomongo "0.1.2-SNAPSHOT"]]
:dev-dependencies [[ring/ring-httpcore-adapter "0.2.0"]])
43 changes: 43 additions & 0 deletions src/todos_server/api.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns todos-server.api
(:use clojure.contrib.json.write
clojure.contrib.json.read
clojure.contrib.duck-streams
compojure.core
todos-server.db))

(defn- emit-json
"Turn the object to JSON, and emit it with the correct content type"
[x]
{:headers {"Content-Type" "application/json"}
:body (json-str {:content x})})

(defn- parse-json
"Parse the request body into a Clojure data structure"
[body]
(read-json (slurp* body)))

(defn guid [task]
(str "/tasks/" (:id task)))

(defn with-guid [task]
(assoc task :guid (guid task)))

(defroutes main-routes
(GET "/tasks" []
(emit-json
(map with-guid (find-all-tasks))))
(GET "/tasks/:id" [id]
(emit-json
(with-guid (find-task id))))
(POST "/tasks" {body :body}
(let [saved-task (add-task (parse-json body))]
{:status 201
:headers {"Location" (guid saved-task)}}))
(PUT "/tasks/:id" {body :body {id "id"} :route-params}
(update-task id (parse-json body)))
(DELETE "/tasks/:id" [id]
(destroy-task id)
{:status 200}))



38 changes: 38 additions & 0 deletions src/todos_server/db.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns todos-server.db
(:use todos-server.util)
(:use somnium.congomongo))

(mongo! :db "todos")

(defn- extern-id
"Returns a version of obj with a string :id based on its :_id"
[obj]
(-> obj
(assoc :id (str (:_id obj)))
(dissoc :_id)))

(defn- intern-id
"Returns a version of obj with an ObjectId based on its :id"
[obj]
(-> obj
(assoc :_id (object-id (:id obj)))
(dissoc :id)))

(defn find-all-tasks []
(map extern-id (fetch :tasks)))

(defn find-task [id]
(extern-id (fetch-by-id :tasks id)))

(defn add-task [task]
(extern-id (insert! :tasks task)))

(defn update-task [id task]
(let [task-in-db (fetch-by-id :tasks id)]
(update! :tasks
task-in-db
(merge-with-kw-keys task-in-db task))))

(defn destroy-task [id]
(destroy! :tasks
(fetch-by-id :tasks id)))
18 changes: 18 additions & 0 deletions src/todos_server/util.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns todos-server.util)

(defn keywordify-keys
"Returns a map otherwise same as the argument but
with all keys turned to keywords"
[m]
(zipmap
(map keyword (keys m))
(vals m)))

(defn merge-with-kw-keys
"Merges maps converting all keys to keywords"
[& maps]
(reduce
merge
(map keywordify-keys maps)))


6 changes: 6 additions & 0 deletions test/todos_server/core_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(ns todos-server.core-test
(:use [todos-server.core] :reload-all)
(:use [clojure.test]))

(deftest replace-me ;; FIXME: write
(is false))

0 comments on commit 911ea9b

Please sign in to comment.