Created
July 31, 2016 20:18
-
-
Save jacoelho/f52ef370edb9ca249c1130749b4c3166 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
| (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"}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment