Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
clj-crud/src/clj_crud/data/todos.clj
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
31 lines (27 sloc)
1.28 KB
This file contains 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 clj-crud.data.todos | |
(:require [clojure.tools.logging :refer [info debug spy]] | |
[clojure.string :as string] | |
[clojure.java.jdbc :as jdbc])) | |
(defn create-todo [db account todo] | |
(let [{account_id :id} account | |
{:keys [text]} todo | |
now (.getTime (java.util.Date.)) | |
res (jdbc/insert! db :todos {:text text | |
:account_id account_id | |
:completed 0 | |
:created_at now | |
:updated_at now})] | |
(or (get (first res) :1) | |
(get (first res) :id)))) | |
(defn get-todos [db account] | |
(let [{account_id :id} account] | |
(->> (jdbc/query db ["SELECT * FROM todos WHERE account_id = ? ORDER BY id ASC" account_id]) | |
(map #(update-in % [:completed] pos?))))) | |
(defn update-todo [db account todo] | |
(let [res (jdbc/update! db :todos (assoc (dissoc todo :id) | |
:updated_at (.getTime (java.util.Date.))) | |
["id = ? AND account_id = ?" (:id todo) (:id account)])] | |
(when-not (= res [1]) | |
(throw (Exception. "DB Update has not succeeded"))))) | |
(defn delete-todo [db account todo-id] | |
(jdbc/delete! db :todos ["id = ? AND account_id = ?" todo-id (:id account)])) |