Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c50ed39
commit 4eae1b8
Showing
10 changed files
with
98 additions
and
41 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
{:minimal-api-lein.routes/routes {} | ||
:minimal-api-lein.server/app {:routes #ig/ref :minimal-api-lein.routes/routes} | ||
{:minimal-api-lein.boundary.db.core/db {} | ||
:minimal-api-lein.routes/routes {} | ||
:minimal-api-lein.server/app {:routes #ig/ref :minimal-api-lein.routes/routes | ||
:db #ig/ref :minimal-api-lein.boundary.db.core/db} | ||
:minimal-api-lein.server/server {:app #ig/ref :minimal-api-lein.server/app | ||
:options {:port 3000 | ||
:join? false}}} |
35 changes: 35 additions & 0 deletions
35
minimal-api-lein/src/minimal_api_lein/boundary/db/core.clj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
(ns minimal-api-lein.boundary.db.core | ||
(:require [clojure.string :as str] | ||
[integrant.core :as ig])) | ||
|
||
(defrecord Boundary [data]) | ||
|
||
(defmethod ig/init-key ::db [_ _] | ||
(->Boundary (atom {:todo {"todo1" {"task" "build an API"} | ||
"todo2" {"task" "?????"} | ||
"todo3" {"task" "profit!"}}}))) | ||
|
||
(defn select-all [{:keys [data]} table] | ||
(get @data table)) | ||
|
||
(defn selet-by-key [{:keys [data]} table k] | ||
(get-in @data [table k])) | ||
|
||
(defn insert! [{:keys [data]} table key-prefix v] | ||
(let [id (->> (keys (get @data table)) | ||
(map #(-> % | ||
(str/replace-first key-prefix "") | ||
Long/parseLong)) | ||
(apply max) | ||
inc) | ||
k (str key-prefix id)] | ||
(swap! data assoc-in [table k] v) | ||
k)) | ||
|
||
(defn update-by-key! [{:keys [data]} table k v] | ||
(swap! data assoc-in [table k] v) | ||
nil) | ||
|
||
(defn delete-by-key! [{:keys [data]} table k] | ||
(swap! data update table dissoc k) | ||
nil) |
22 changes: 22 additions & 0 deletions
22
minimal-api-lein/src/minimal_api_lein/boundary/db/todo.clj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
(ns minimal-api-lein.boundary.db.todo | ||
(:require [minimal-api-lein.boundary.db.core :as db])) | ||
|
||
(defprotocol Todo | ||
(find-todos [db]) | ||
(find-todo-by-id [db id]) | ||
(create-todo! [db todo]) | ||
(update-todo! [db id todo]) | ||
(delete-todo! [db id])) | ||
|
||
(extend-protocol Todo | ||
minimal_api_lein.boundary.db.core.Boundary | ||
(find-todos [db] | ||
(db/select-all db :todo)) | ||
(find-todo-by-id [db id] | ||
(db/selet-by-key db :todo id)) | ||
(create-todo! [db todo] | ||
(db/insert! db :todo "todo" todo)) | ||
(update-todo! [db id todo] | ||
(db/update-by-key! db :todo id todo)) | ||
(delete-todo! [db id] | ||
(db/delete-by-key! db :todo id))) |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,27 @@ | ||
(ns minimal-api-lein.handler | ||
(:require [clojure.string :as str] | ||
[minimal-api-lein.db :refer [todos]] | ||
(:require [minimal-api-lein.boundary.db.todo :as db.todo] | ||
[ring.util.http-response :as response])) | ||
|
||
(defn list-todos [_] | ||
(response/ok @todos)) | ||
(defn list-todos [{:keys [db]}] | ||
(response/ok (db.todo/find-todos db))) | ||
|
||
(defn create-todo [{:keys [params]}] | ||
(let [id (->> (keys @todos) | ||
(map #(-> % | ||
(str/replace-first "todo" "") | ||
Long/parseLong)) | ||
(apply max) | ||
inc) | ||
todo-id (str "todo" id)] | ||
(swap! todos assoc todo-id {"task" (:task params)}) | ||
(response/created (str "/todos/" todo-id) (get @todos todo-id)))) | ||
(defn create-todo [{:keys [params db]}] | ||
(let [id (db.todo/create-todo! db {"task" (:task params)}) | ||
todo (db.todo/find-todo-by-id db id)] | ||
(response/created (str "/todos/" id) todo))) | ||
|
||
(defn fetch-todo [{:keys [params]}] | ||
(if-let [todo (get @todos (:todo-id params))] | ||
(defn fetch-todo [{:keys [params db]}] | ||
(if-let [todo (db.todo/find-todo-by-id db (:todo-id params))] | ||
(response/ok todo) | ||
(response/not-found {:message (str "Todo " (:todo-id params) " doesn't exist")}))) | ||
|
||
(defn delete-todo [{:keys [params]}] | ||
(if (get @todos (:todo-id params)) | ||
(do (swap! todos dissoc (:todo-id params)) | ||
(defn delete-todo [{:keys [params db]}] | ||
(if (db.todo/find-todo-by-id db (:todo-id params)) | ||
(do (db.todo/delete-todo! db (:todo-id params)) | ||
(response/no-content)) | ||
(response/not-found {:message (str "Todo " (:todo-id params) " doesn't exist")}))) | ||
|
||
(defn update-todo [{:keys [params]}] | ||
(defn update-todo [{:keys [params db]}] | ||
(let [task {"task" (:task params)}] | ||
(swap! todos assoc (:todo-id params) task) | ||
(db.todo/update-todo! db (:todo-id params) task) | ||
(response/created (str "/todos/" (:todo-id params)) task))) |
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
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
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
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