on the way

This commit is contained in:
didier amyot committed 2018-10-14 20:04:18 -04:00
1 parent 82b43ddf90
commit 6e266c787b
14 files changed
+146 -45

No files matched your search

+10 -2
View File
@@ -18,17 +18,25 @@
(catch [:error-type :5XX] e (build-response request e)))))
(defn add-account-urls [user]
(let [accounts (:skirmish.broker.user/accounts user)
url #(->> % :skirmish.broker.account/id (str "http://localhost:1604/api/1/ledger/accounts/"))
ret (map #(assoc % :skirmish.broker.account/url (url %)) accounts)]
(assoc user :skirmish.broker.user/accounts ret)))
(defn get-user [conn]
(fn [{{email :email} :route-params :as request}]
(try+
(timbre/info "getting user with email" email)
(let [db (d/db conn)
users (d/q '[:find [(pull ?e [*])]
users (d/q '[:find (pull ?e [:skirmish.broker.user/email
{:skirmish.broker.user/accounts [:skirmish.broker.account/id
:skirmish.broker.account/sec-id]}]) .
:in $ ?email
:where [?e :skirmish.broker.user/email ?email]] db email)]
(if (nil? users)
(build-response request {:status 404 :body {:error (str "cannot find user with email " email)}})
(build-response request (first users))))
(build-response request (add-account-urls users))))
(catch [:error-type :4XX] e (build-response request e))
(catch [:error-type :5XX] e (build-response request e)))))
+30 -4
View File
@@ -9,29 +9,55 @@
:db/fn (d/function {:lang "clojure"
:requires ['[slingshot.slingshot]]
:params '[db email]
:code '(let [existing-user (d/pull db '[:db/id] [:skirmish.broker.user/email email])]
:code '(let [existing-user (d/pull db '[:db/id] [:skirmish.broker.user/email email
:skirmish.broker.user/accounts []])]
(when (-> existing-user :db/id some?)
(slingshot.slingshot/throw+ {:error-type :4XX :status 400 :body {:error "A user with this email already exists."}}))
[#:skirmish.broker.user{:email email}])})}])
(def accounts-schema [
{
:db/ident :skirmish.broker.account/id
:db/unique :db.unique/identity
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one
:db/doc "The id of an account."}
{
:db/ident :skirmish.broker.account/sec-id
:db/valueType :db.type/keyword
:db/cardinality :db.cardinality/one
:db/doc "The security if of the account."}
{
:db/ident :skirmish.broker.account/private-key
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "The private key of the account."}])
(def user-schema [
{
:db/ident :skirmish.broker.user/email
:db/unique :db.unique/identity
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "The email of the user; used as id."}
{
:db/ident :skirmish.broker.user/accounts
:db/unique :db.unique/identity
:db/valueType :db.type/ref
:db/cardinality :db.cardinality/many
:db/doc "The email of the user; used as id."}])
(def schema (-> []
(concat user-schema)
(concat create-user)))
(concat create-user)
(concat accounts-schema)))
(defn create-connection [uri]
(let [new-db? (d/create-database uri)
conn (d/connect uri)]
(when new-db?
@(d/transact conn schema))
@(d/transact conn schema)
conn))
+1
View File
@@ -16,6 +16,7 @@
trades-submitter
orders-submitter)))
(defn -main [config securities-api exchange-apis-mult-chan]
(let [ch (async/chan)]
(async/tap exchange-apis-mult-chan ch)
-4
View File
@@ -54,7 +54,6 @@
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/doc "The type of security. For instance :equity or :currency."}
{
:db/ident :skirmish.security/description
:db/valueType :db.type/string
@@ -81,7 +80,6 @@
(def ledger-schema
[
{
:db/ident :skirmish.ledger/owner-public-key
:db/unique :db.unique/identity
@@ -101,14 +99,12 @@
:db/cardinality :db.cardinality/one
:db/doc "The sec kept in this account."}])
(def schema (-> []
(concat schema-securities)
(concat ledger-schema)
(concat create-securities)
(concat print-securities)))
(defn create-connection [uri]
(let [new-db? (d/create-database uri)
conn (d/connect uri)]
+19 -12
View File
@@ -58,16 +58,23 @@
:emitter string?
:symbol (s/? string?)))
(defn get-new-security [raw-securities]
(if (s/valid? ::security raw-securities)
raw-securities
(throw+ {:error-type :4XX :status 400 :body {:error "Invalid security format."}})))
(defn get-security-from-db [conn sec-id]
(let [e (d/pull (d/db conn) '[*] [:skirmish.security/sec-id sec-id])
{interest-rate :skirmish.security/interest-rate id :db/id} e
ret {:sec-id sec-id :interest-rate interest-rate}]
(let [db (d/db conn)
e (d/pull db '[*] [:skirmish.security/sec-id sec-id])
{interest-rate :skirmish.security/interest-rate
description :skirmish.security/description
id :db/id
type_ :skirmish.security/type} e
volume (d/q '[:find (sum ?a) .
:where [?e :skirmish.ledger/amount ?a]
[?e :skirmish.ledger/owner-public-key ?pk]]
db)
ret {:sec-id sec-id :interest-rate interest-rate :volume volume :type type_ :description description}]
(if (nil? id)
(throw+ {:error-type :4XX :status 404 :body {:error "Security not found."}})
ret)))
@@ -156,15 +163,15 @@
(defn create-routes-for-ledger [securities conn]
(routes
(context "/api/1" []
(context "/api/1/ledger" []
(GET "/" get-api-description)
(GET "/ledger/accounts" [] (get-available-accounts conn))
(GET "/ledger/accounts/:account-id" [] (get-account conn))
(GET "/ledger/securities" [] (get-available-securities conn)) ;; done
(POST "/ledger/securities" [] (create-security conn)) ;; done
(GET "/ledger/securities/:sec-id" [] (get-security conn)) ;; done
(PUT "/ledger/securities/:sec-id" [] (update-security conn)) ;; not done
(POST "/ledger/securities/:sec-id" [] (emit-securities conn))) ;; done
(GET "/accounts" [] (get-available-accounts conn))
(GET "/accounts/:account-id" [] (get-account conn))
(GET "/securities" [] (get-available-securities conn)) ;; done
(POST "/securities" [] (create-security conn)) ;; done
(GET "/securities/:sec-id" [] (get-security conn)) ;; done
(PUT "/securities/:sec-id" [] (update-security conn)) ;; not done
(POST "/securities/:sec-id" [] (emit-securities conn))) ;; done
(not-found "not found")))
@@ -4,12 +4,6 @@
[cljs.core.async :refer [<!] :refer-macros [go]]
[secretary.core :as secretary]))
(def lorem-ipsum ["Lorem ipsum dolor amet pickled bushwick next level adaptogen ugh franzen salvia hoodie man bun organic kinfolk. Hammock pickled banjo, artisan single-origin coffee brunch 90's occupy taxidermy. Put a bird on it gentrify offal tilde. Authentic put a bird on it prism semiotics, roof party banh mi typewriter. XOXO schlitz sriracha godard."
"Succulents shoreditch twee, VHS hammock hot chicken distillery. Edison bulb aesthetic pork belly meggings. 3 wolf moon artisan keytar blue bottle. Actually pok pok asymmetrical, banh mi before they sold out PBR&B portland tumblr."
"Gentrify palo santo enamel pin, tumeric jianbing thundercats shaman green juice chambray yr. Kale chips synth meditation chambray thundercats helvetica, cornhole whatever kickstarter ennui pop-up lo-fi godard. Austin unicorn brunch woke hammock. Coloring book kombucha occupy tofu wayfarers."])
(defn email? [s]
(let [email-pattern #"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"]
(->> s str (re-matches email-pattern) some?)))
@@ -49,7 +43,5 @@
[:div
[:section.section
[:div.container
[:h1.title "lorem ipsum"]
[login state]
(for [[i sentence] (map vector (range) lorem-ipsum)]
[:p {:key i} sentence])]]])
[:img {:src "/images/amsterdam.svg"}]
[login state]]]])
@@ -2,6 +2,7 @@
(:require [reagent.core :refer [atom]]
[skirmish.wui.components.secunit :refer [secunit]]))
(defn make-portfolio-summary-table [portfolio]
[:table
[:thead
@@ -13,9 +14,11 @@
[:td [secunit (:sec-id sec-summary) (:amount sec-summary)]]
[:td [secunit :dapm (:face-value sec-summary)]]])]])
(defn page []
(defn page [{{accounts :skirmish.broker.user/accounts} :user}]
(let [porfolio [{:id 12 :sec-id :foo :amount 100 :face-value 1265}
{:id 54 :sec-id :toto :amount 100 :face-value 1265}
{:id 66 :sec-id :foo :amount 100 :face-value 1265}]]
[:div.content
[:pre (str accounts)]
(make-portfolio-summary-table porfolio)]))
@@ -15,12 +15,14 @@
[:li done "implement basic login in the wui"]
[:li done "implement basic web server"]
[:li done "implement stupid login on server side."]
[:li on-it "link the logic of the client-server login logic"]
[:li "add the notion of 'accounts' in the server. An
user should have many accounts; one for each security
he owns"]
[:li "make the " [:a {:href "/portfolio"} "portfolio"] " page representing the use accounts"]
[:li done "link the logic of the client-server login logic"]
[:li done "add the notion of 'accounts' in the server. An
user should have many accounts; one for each security
he owns"]
[:li on-it "make the " [:a {:href "/portfolio"} "portfolio"] " page representing the use accounts"]
[:li "Make sure the ledger can have accounts"]
[:li "when a new user logs in, he should receive some sort of money"]
[:li "\u22EE"]
[:li "implement buy/sell securities."]]]
[:li "Cleaning up..."
-1
View File
@@ -4,7 +4,6 @@
[skirmish.wui.components.topbar :refer [top-bar]]
[reagent.session :as session]
[cljs-http.client :as http]
;[taoensso.timbre :as timbre :refer-macros [info warn]]
[cljs.core.async :refer [<!] :refer-macros [go]]
[secretary.core :as secretary :include-macros true]
[accountant.core :as accountant]))