-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.clj
57 lines (46 loc) · 1.48 KB
/
db.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns nl.openweb.projector.db)
(defonce bank-accounts (atom {}))
(defonce bank-transfers (atom {}))
(defonce ibans-by-user (atom {}))
(defonce bank-transactions (atom []))
(defonce transactions-by-iban (atom {}))
(defonce users (atom {}))
(defn get-db [type]
(condp = type
:bank-accounts bank-accounts
:bank-transfers bank-transfers
:ibans-by-user ibans-by-user
:transactions-by-iban transactions-by-iban
:users users
nil))
(defn get-from-db [type id]
(get @(get-db type) id))
(defn add-to-db! [type id entry]
(swap! (get-db type) assoc id entry))
(defn update-in-db! [type id update-function]
(let [new-db (swap! (get-db type) update id update-function)]
(get new-db id)))
(defn add-transaction! [transaction]
(let [id (-> (swap! bank-transactions #(conj % transaction))
count
int)
transaction-with-id (assoc transaction :id id)]
(swap! transactions-by-iban update (:iban transaction) (fn [old] (conj old transaction-with-id)))
transaction-with-id))
(defn get-transaction
[transaction-id]
(nth @bank-transactions (- transaction-id 1) nil))
(defn get-all-last-transactions
[]
(->> @transactions-by-iban
(sort-by first)
vals
(remove empty?)
(mapv last)))
(defn add-iban-for-user!
[user iban]
(swap! ibans-by-user
(fn [current]
(if-let [ibans (get current user)]
(assoc current user (conj ibans iban))
(assoc current user [iban])))))