Skip to content

Commit a136385

Browse files
committed
Make :effect optional, add :return, integrate with ring helper
1 parent 6be1f7e commit a136385

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

README.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ So far, only events have been somewhat implemented.
5959

6060
;; and some database functions:
6161

62-
(defn transfer! [from-account-id to-account-id amount])
62+
(defn get-account [account-id] ...)
63+
64+
(defn transfer! [from-account-id to-account-id amount] ...)
6365

6466
(defn deposit! [account-id amount])
6567

@@ -87,18 +89,22 @@ So far, only events have been somewhat implemented.
8789
[[(user-exists? user-id) :forbidden "User with this id does not exist"]
8890
[(account-exists? account-id) :not-found "Account with this id does not exist"]
8991
[(user-owns-account? user-id account-id) :forbidden "User does not own this account"]
90-
[(= currency (:currency (get-account account-id)) :incorrect "Deposit currency must match account"]])
92+
[(= currency (:currency (get-account account-id))) :incorrect "Deposit currency must match account"]])
9193

9294
:effect
9395
(fn [{:keys [account-id amount]}]
94-
(deposit! account-id amount))}
96+
(deposit! account-id amount))
97+
98+
:return
99+
(fn [{:keys [account-id]}]
100+
(get-account account-id))}
95101

96102
{:id :transfer!
97103

98104
:params {:user-id :bank.user/id
99105
:from-account-id :bank.account/id
100106
:to-account-id :bank.account/id
101-
:amount (and integer? pos?)})}
107+
:amount (and integer? pos?)}
102108

103109
:conditions
104110
(fn [{:keys [user-id from-account-id to-account-id amount]}]
@@ -107,11 +113,12 @@ So far, only events have been somewhat implemented.
107113
[(user-owns-account? user-id from-account-id) :forbidden "User does not own this account"]
108114
[(account-exists? from-account-id) :incorrect "Account with this id does not exist"]
109115
[(>= (:balance (get-account from-account-id)) amount) :conflict "Insufficient funds in account"]
110-
[(= (:currency (get-account from-account-id)
111-
(:currency (get-account to-account-id)) :conflict "Currency of accounts must match"]])
116+
[(= (:currency (get-account from-account-id))
117+
(:currency (get-account to-account-id))) :conflict "Currency of accounts must match"]])
112118

113-
:effect (fn [{:keys [from-account-id to-account-id amount]}]
114-
(transfer! from-account-id to-account-id amount)}]}])
119+
:effect
120+
(fn [{:keys [from-account-id to-account-id amount]}]
121+
(transfer! from-account-id to-account-id amount))}])
115122

116123

117124
;; register our events

src/tada/events/core.cljc

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
:fn fn?
3333
:spec s/spec?})}
3434
:conditions fn? ;; :tada/condition-fn
35-
:effect fn?}}))
35+
(ds/opt :effect) fn?
36+
(ds/opt :return) fn?}}))
3637

3738
(s/def :tada/events
3839
(ds/spec
@@ -101,8 +102,11 @@
101102
(let [errors (rule-errors ev sanitized-params)]
102103
(if (empty? errors)
103104
(do
104-
((ev :effect) sanitized-params)
105-
true)
105+
(when (ev :effect)
106+
((ev :effect) sanitized-params))
107+
(if (ev :return)
108+
((ev :return) sanitized-params)
109+
nil))
106110
(throw (ex-info (str "Event conditions are not met:\n"
107111
(string/join "\n" (map :message errors)))
108112
{:anomaly :incorrect}))))

src/tada/events/ring.clj

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
(:require
33
[tada.events.core :as tada]))
44

5-
(defn route [ev-id]
5+
(defn route [ev-id]
66
(fn [request]
77
(try
8-
(tada/do! ev-id (request :params))
9-
{:status 200}
8+
(if-let [return (tada/do! ev-id (request :params))]
9+
{:status 200
10+
:body return}
11+
{:status 200})
1012
(catch clojure.lang.ExceptionInfo e
1113
{:body (.getMessage e)
1214
:status (case (:anomaly (ex-data e))
1315
:incorrect 400
14-
:unsupported 500)}))))
16+
:forbidden 403
17+
:unsupported 405
18+
:not-found 404)}))))

0 commit comments

Comments
 (0)