Skip to content

Commit 9d38a6e

Browse files
committed
removed konserve, because of outdated dependencies. Reused the konserve api
1 parent 7689539 commit 9d38a6e

File tree

8 files changed

+153
-54
lines changed

8 files changed

+153
-54
lines changed

project.clj

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
[ring "1.9.5"]
1010
[ring-middleware-format "0.7.4"]
1111
[ring/ring-defaults "0.3.3"]
12-
[metosin/spec-tools "0.10.5"]
13-
14-
[org.clojure/core.async "1.5.648"]
15-
[io.replikativ/konserve "0.7.270" :exclusions
16-
[org.clojure/clojurescript
17-
org.clojure/tools.analyzer.jvm]]]
12+
[metosin/spec-tools "0.10.5"]]
1813
:main ^:skip-aot todo-api.core
1914
:target-path "target/%s"
2015
:profiles {:uberjar {:aot :all}})

src/todo_api/middleware/catch_spec.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@
1616
(let [{:keys [type msg]} (ex-data ex)]
1717
(case type
1818
:invalid-spec (rr/not-found msg)
19+
:invalid-key (rr/not-found msg)
1920
(handle-unknown-error ex)))))))

src/todo_api/storage/api.clj

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1-
(ns todo-api.storage.api)
1+
(ns todo-api.storage.api
2+
(:refer-clojure :exclude [get get-in update update-in assoc assoc-in exists? dissoc keys]))
23

4+
(def implementation
5+
(constantly
6+
#_:edn-file
7+
:atom))
8+
9+
(defmulti assoc
10+
implementation)
11+
12+
(defmulti dissoc
13+
implementation)
14+
15+
(defmulti get
16+
implementation)
17+
18+
(defmulti update
19+
implementation)
20+
21+
(defmulti assoc-in
22+
implementation)
23+
24+
(defmulti get-in
25+
implementation)
26+
27+
(defmulti update-in
28+
implementation)
29+
30+
(defmulti exists?
31+
implementation)
32+
33+
(defmulti keys
34+
implementation)
35+
36+
;;; Remove Old implementation:
337
(defmulti fetch
4-
(constantly :edn-file))
38+
implementation)
539

640
(defmulti persist
7-
(constantly :edn-file))
41+
implementation)

src/todo_api/storage/atom.clj

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,86 @@
22
(:require
33
[todo-api.storage.api :as api]))
44

5-
(def ^:private todos (atom {}))
5+
(def ^:private store (atom {}))
6+
7+
(defn- fetch
8+
[]
9+
@store)
10+
11+
(defn- persist
12+
[new-store]
13+
(reset! store new-store))
14+
15+
(defn- with-fetch-persist
16+
[f]
17+
(-> (fetch)
18+
(f)
19+
(persist)))
20+
21+
(defn- with-fetch
22+
[f]
23+
(f (fetch)))
24+
25+
(defmethod api/assoc :atom
26+
[k v]
27+
(with-fetch-persist
28+
(fn [store]
29+
(assoc store k v))))
30+
31+
(defmethod api/dissoc :atom
32+
[k]
33+
(with-fetch-persist
34+
(fn [store]
35+
(dissoc store k))))
36+
37+
(defmethod api/get :atom
38+
[k]
39+
(with-fetch
40+
(fn [store]
41+
(get store k))))
42+
43+
(defmethod api/update :atom
44+
[k f & args]
45+
(with-fetch-persist
46+
(fn [store]
47+
(apply update store k f args))))
48+
49+
(defmethod api/assoc-in :atom
50+
[ks v]
51+
(with-fetch-persist
52+
(fn [store]
53+
(assoc-in store ks v))))
54+
55+
(defmethod api/get-in :atom
56+
[ks]
57+
(with-fetch
58+
(fn [store]
59+
(get-in store ks))))
60+
61+
(defmethod api/update-in :atom
62+
[ks f & args]
63+
(with-fetch-persist
64+
(fn [store]
65+
(apply update-in store ks f args))))
66+
67+
(defmethod api/exists? :atom
68+
[k]
69+
(with-fetch
70+
(fn [store]
71+
(->> (if (vector? k) k (vector k))
72+
(get-in store)
73+
(some?)))))
74+
75+
(defmethod api/keys :atom
76+
[]
77+
(with-fetch
78+
(fn [store]
79+
(keys store))))
680

781
(defmethod api/fetch :atom
882
[]
9-
@todos)
83+
(fetch))
1084

1185
(defmethod api/persist :atom
12-
[new-todos]
13-
(reset! todos new-todos))
86+
[new-store]
87+
(persist new-store))

src/todo_api/storage/konserve.clj

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/todo_api/storage/utils.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(ns todo-api.storage.utils
2+
(:require [todo-api.storage.api :as store]))
3+
4+
(defn check-key-exist-or-throw
5+
[key]
6+
(when-not (store/exists? key)
7+
(throw
8+
(ex-info "Key invalid!" {:type :invalid-key
9+
:msg (str "Key not found: '" key "'")}))))

src/todo_api/task/api.clj

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
(ns todo-api.task.api
22
(:require
33
[todo-api.spec-tools :as spec-tools]
4-
[todo-api.storage.api :refer [fetch persist]]
4+
[todo-api.storage.api :as storage]
5+
[todo-api.storage.atom]
6+
[todo-api.storage.utils :refer [check-key-exist-or-throw]]
57
[todo-api.task.spec :as task-spec]
68
[todo-api.todo.api :as todo-api]
79
[todo-api.todo.spec :as todo-spec]))
810

911
(defn create-task
1012
[todo-id name]
1113
(spec-tools/check-spec-or-throw ::todo-spec/id todo-id)
12-
(let [new-task
13-
{:id (random-uuid)
14-
:name name}]
14+
(let [task-id (random-uuid)
15+
new-task {:name name}]
1516
(spec-tools/check-spec-or-throw ::task-spec/task new-task)
16-
(-> (fetch)
17-
(assoc-in [todo-id :tasks (:id new-task)]
18-
(dissoc new-task :id))
19-
(persist))))
17+
(storage/assoc-in [todo-id :tasks task-id] new-task)
18+
task-id))
2019

2120
(defn read-task
2221
[todo-id task-id]
2322
(spec-tools/check-spec-or-throw ::todo-spec/id todo-id)
2423
(spec-tools/check-spec-or-throw ::task-spec/id task-id)
25-
(-> (fetch)
26-
(get-in [todo-id :tasks task-id])))
24+
(check-key-exist-or-throw [todo-id :tasks task-id])
25+
(->> (storage/get-in [todo-id :tasks task-id])
26+
(assoc :id task-id)))
2727

2828
(defn update-task
2929
[todo-id task-id name]
3030
(spec-tools/check-spec-or-throw ::todo-spec/id todo-id)
3131
(spec-tools/check-spec-or-throw ::task-spec/id task-id)
3232
(spec-tools/check-spec-or-throw ::task-spec/name name)
33-
(-> (fetch)
34-
(update-in [todo-id :tasks task-id]
35-
assoc :name name)
36-
(persist)))
33+
(check-key-exist-or-throw [todo-id :tasks task-id])
34+
(storage/update-in [todo-id :tasks task-id] assoc :name name))
3735

3836
(defn delete-task
3937
[todo-id task-id]
4038
(spec-tools/check-spec-or-throw ::todo-spec/id todo-id)
4139
(spec-tools/check-spec-or-throw ::task-spec/id task-id)
42-
(-> (fetch)
43-
(update-in [todo-id :tasks] dissoc task-id)
44-
(persist)))
40+
(check-key-exist-or-throw [todo-id :tasks task-id])
41+
(storage/update-in [todo-id :tasks]
42+
dissoc task-id))
4543

4644
(defn list-tasks
4745
[todo-id]

src/todo_api/todo/api.clj

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
(ns todo-api.todo.api
22
(:require
3-
[konserve.core :as k]
43
[todo-api.spec-tools :as spec-tools]
5-
[todo-api.storage.konserve :refer [check-key-exist-or-throw opts store]]
4+
[todo-api.storage.utils :refer [check-key-exist-or-throw]]
5+
[todo-api.storage.api :as store]
6+
[todo-api.storage.atom]
67
[todo-api.todo.spec :as todo-spec]))
78

89
(defn- update-todo-entry
@@ -17,30 +18,31 @@
1718
new-todo {:name name
1819
:description description}]
1920
(spec-tools/check-spec-or-throw ::todo-spec/todo new-todo)
20-
(k/assoc store id new-todo opts)
21+
(store/assoc id new-todo)
2122
id))
2223

2324
(defn read-todo
2425
[id]
2526
(spec-tools/check-spec-or-throw ::todo-spec/id id)
26-
(check-key-exist-or-throw store id)
27-
(k/get store id opts))
27+
(check-key-exist-or-throw id)
28+
(-> (store/get id)
29+
(assoc :id id)))
2830

2931
(defn update-todo
3032
[id name description]
3133
(spec-tools/check-spec-or-throw ::todo-spec/todo
3234
{:name name
3335
:description description})
34-
(check-key-exist-or-throw store id)
35-
(k/update store id #(update-todo-entry % name description) opts))
36+
(check-key-exist-or-throw id)
37+
(store/update id #(update-todo-entry % name description)))
3638

3739
(defn delete-todo
3840
[id]
3941
(spec-tools/check-spec-or-throw ::todo-spec/id id)
40-
(check-key-exist-or-throw store id)
41-
(k/dissoc store id opts))
42+
(check-key-exist-or-throw id)
43+
(store/dissoc id))
4244

4345
(defn list-todos
4446
[]
45-
(let [keys (k/keys store opts)]
47+
(let [keys (store/keys)]
4648
(map read-todo keys)))

0 commit comments

Comments
 (0)