Last active
July 31, 2016 20:22
-
-
Save jacoelho/fd6658ffd6b66884b028a4e4b7df437e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns todo-rest.core | |
| (:require [ring.adapter.jetty :as jetty] | |
| [ring.middleware.reload :refer [wrap-reload]] | |
| [ring.middleware.json :refer [wrap-json-response wrap-json-params]] | |
| [compojure.core :refer [defroutes context GET POST PUT DELETE]] | |
| [compojure.route :refer [not-found]] | |
| [clojure.java.jdbc :as jdbc]) | |
| (:import com.mchange.v2.c3p0.ComboPooledDataSource) | |
| (:gen-class)) | |
| (def db-config | |
| {:classname "org.h2.Driver" | |
| :subprotocol "h2" | |
| :subname "mem:documents" | |
| :user "" | |
| :password ""}) | |
| (defn pool | |
| [config] | |
| (let [cpds | |
| (doto | |
| (ComboPooledDataSource.) | |
| (.setDriverClass (:classname config)) | |
| (.setJdbcUrl (str "jdbc:" (:subprotocol config) ":" (:subname config))) | |
| (.setUser (:user config)) | |
| (.setPassword (:password config)) | |
| (.setMaxPoolSize 6) | |
| (.setMinPoolSize 1) | |
| (.setInitialPoolSize 1))] | |
| {:datasource cpds})) | |
| (def pooled-db (delay (pool db-config))) | |
| (defn db-connection [] @pooled-db) | |
| (defn create-table | |
| [] | |
| (jdbc/db-do-commands | |
| (db-connection) | |
| (jdbc/create-table-ddl :todo_list | |
| [[:id :int "PRIMARY KEY AUTO_INCREMENT"] | |
| [:title "varchar(30)"] | |
| [:text "varchar(1024)"]]))) | |
| (defn get-all-todo | |
| [] | |
| (let [result (jdbc/with-db-connection | |
| [conn (db-connection)] | |
| (jdbc/query conn | |
| ["select * from todo_list"]))] | |
| {:status 200 | |
| :body {:result result}})) | |
| (defn create-todo | |
| [params] | |
| (let [text (get params "text" "unset") | |
| title (get params "title" "unset")] | |
| (jdbc/with-db-transaction | |
| [conn (db-connection)] | |
| (let [document (jdbc/insert! conn | |
| :todo_list | |
| [:title :text][title text]) | |
| id (first (jdbc/execute! conn ["CALL IDENTITY()"]))] | |
| {:status 201 | |
| :body {:id id}})))) | |
| (defn get-todo | |
| [id] | |
| (let [result (jdbc/with-db-connection | |
| [conn (db-connection)] | |
| (jdbc/query conn | |
| ["select * from todo_list where id = ?" id]))] | |
| (if (empty? result) | |
| {:status 404} | |
| {:status 200 | |
| :body {:message (first result)}}))) | |
| (defn update-todo | |
| [id params] | |
| (let [text (get params "text" "unset") | |
| title (get params "title" "unset")] | |
| (jdbc/with-db-transaction | |
| [conn (db-connection)] | |
| (jdbc/update! conn | |
| :todo_list | |
| {:title title :text text} | |
| ["id = ?" id]))) | |
| {:status 200 | |
| :body {:id id}}) | |
| (defn delete-todo | |
| [id] | |
| (jdbc/with-db-transaction | |
| [conn (db-connection)] | |
| (jdbc/delete! conn | |
| :todo_list | |
| ["id = ?" id])) | |
| {:status 200 | |
| :body {:message "ok"}}) | |
| (defroutes app-routes | |
| (GET "/" [] (get-all-todo)) | |
| (POST "/" {params :params} (create-todo params)) | |
| (context "/:id" [id] | |
| (GET "/" [] (get-todo id)) | |
| (PUT "/" {params :params} (update-todo id params)) | |
| (DELETE "/" [] (delete-todo id))) | |
| (not-found "sorry")) | |
| (def app | |
| (-> app-routes | |
| wrap-json-params | |
| wrap-json-response)) | |
| (defn -dev-main | |
| "dev simple app" | |
| [] | |
| (create-table) | |
| (jetty/run-jetty (wrap-reload #'app) | |
| {:port (Integer. (get (System/getenv) "PORT" "8000"))})) | |
| (defn -main | |
| "simple app" | |
| [] | |
| (create-table) | |
| (jetty/run-jetty app | |
| {:port (Integer. (get (System/getenv) "PORT" "8000"))})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment