Skip to content

Instantly share code, notes, and snippets.

@jacoelho
Created July 31, 2016 20:18
Show Gist options
  • Save jacoelho/f52ef370edb9ca249c1130749b4c3166 to your computer and use it in GitHub Desktop.
Save jacoelho/f52ef370edb9ca249c1130749b4c3166 to your computer and use it in GitHub Desktop.
(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