adding invoice db layer

This commit is contained in:
Bryce Covert
2017-12-08 07:48:04 -08:00
parent 252a6c5f9b
commit ef4dd52a42
3 changed files with 63 additions and 41 deletions

View File

@@ -0,0 +1,12 @@
(ns auto-ap.db.invoices
(:require [clojure.java.jdbc :as j]
[auto-ap.db.utils :use [clj->db db->clj conn]]))
(defn insert-multi! [rows]
(j/insert-multi! conn
:invoices
(map clj->db rows)))
(defn get-all []
(map db->clj (j/query conn "SELECT * FROM invoices")))

View File

@@ -0,0 +1,32 @@
(ns auto-ap.db.utils
(:require [clojure.string :as str]))
(defn snake->kebab [s]
(str/replace s #"_" "-"))
(defn kebab->snake [s]
(str/replace s #"-" "_"))
(defn db->clj [x]
(into {}
(map
(fn [[k v]]
[(keyword (snake->kebab (name k))) v])
x)))
(defn clj->db [x]
(into {}
(map
(fn [[k v]]
[(keyword (kebab->snake (name k))) v])
x)))
(def conn {:classname "org.postgresql.Driver" ; must be in classpath
:ssl true
:sslfactory "org.postgresql.ssl.NonValidatingFactory"
:subprotocol "postgresql"
:subname (str "//ec2-54-235-123-153.compute-1.amazonaws.com:5342/dbfemhppkdksfp")
; Any additional keys are passed to the driver
; as driver-specific properties.
:user "tkilrhrhzlumol"
:password "de6117f8551364ac84ed31c1231941f53ab0b5470c9956f478b2025ab5a0fb8b"})

View File

@@ -18,20 +18,22 @@
(defn kebab->snake [s]
(str/replace s #"-" "_"))
(defn db->clj [x]
(into {}
(map
(fn [[k v]]
[(keyword (snake->kebab (name k))) v])
x)))
(defn clj->db [x]
(into {}
(map
(fn [[k v]]
[(keyword (kebab->snake (name k))) v])
x)))
(let [db-host "ec2-54-235-123-153.compute-1.amazonaws.com"
db-port 5432
db-name "dbfemhppkdksfp"]
(def db {:classname "org.postgresql.Driver" ; must be in classpath
:ssl true
:sslfactory "org.postgresql.ssl.NonValidatingFactory"
:subprotocol "postgresql"
:subname (str "//" db-host ":" db-port "/" db-name)
; Any additional keys are passed to the driver
; as driver-specific properties.
:user "tkilrhrhzlumol"
:password "de6117f8551364ac84ed31c1231941f53ab0b5470c9956f478b2025ab5a0fb8b"}))
@@ -40,39 +42,15 @@
(GET "/" [] (response/resource-response "index.html" {:root "public"}))
(GET "/api/invoices" []
{:status 200
:body (pr-str (map (fn [m]
(into {}
(map
(fn [[k v]]
[(keyword (snake->kebab (name k))) v])
m)))
(j/query db "SELECT * FROM invoices")))
:body (pr-str (invoices/get-all))
:headers {"Content-Type" "application/edn"}})
(POST "/api/invoices" {:keys [edn-params]}
(prn edn-params )
(doto
(j/insert-multi! db
:invoices
(map
(fn [e]
(into {}
(map
(fn [[k v]]
[(keyword (kebab->snake (name k))) v])
e)))
(:rows edn-params)))
println)
(invoices/insert-multi! (:rows edn-params))
{:status 200
:body (pr-str (map (fn [m]
(into {}
(map
(fn [[k v]]
[(keyword (snake->kebab (name k))) v])
m)))
(j/query db "SELECT * FROM invoices")))
:headers {"Content-Type" "application/edn"}}
)
:body (pr-str (invoices/get-all))
:headers {"Content-Type" "application/edn"}})
(POST "/pdf-upload"
{{ files "file"} :params :as params}
(let [{:keys [filename tempfile]} files]