adding invoice db layer
This commit is contained in:
12
src/clj/auto_ap/db/invoices.clj
Normal file
12
src/clj/auto_ap/db/invoices.clj
Normal 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")))
|
||||||
|
|
||||||
32
src/clj/auto_ap/db/utils.clj
Normal file
32
src/clj/auto_ap/db/utils.clj
Normal 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"})
|
||||||
@@ -18,20 +18,22 @@
|
|||||||
(defn kebab->snake [s]
|
(defn kebab->snake [s]
|
||||||
(str/replace 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 "/" [] (response/resource-response "index.html" {:root "public"}))
|
||||||
(GET "/api/invoices" []
|
(GET "/api/invoices" []
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (pr-str (map (fn [m]
|
:body (pr-str (invoices/get-all))
|
||||||
(into {}
|
|
||||||
(map
|
|
||||||
(fn [[k v]]
|
|
||||||
[(keyword (snake->kebab (name k))) v])
|
|
||||||
m)))
|
|
||||||
(j/query db "SELECT * FROM invoices")))
|
|
||||||
:headers {"Content-Type" "application/edn"}})
|
:headers {"Content-Type" "application/edn"}})
|
||||||
|
|
||||||
(POST "/api/invoices" {:keys [edn-params]}
|
(POST "/api/invoices" {:keys [edn-params]}
|
||||||
(prn edn-params )
|
(invoices/insert-multi! (:rows 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)
|
|
||||||
|
|
||||||
{:status 200
|
{:status 200
|
||||||
:body (pr-str (map (fn [m]
|
:body (pr-str (invoices/get-all))
|
||||||
(into {}
|
:headers {"Content-Type" "application/edn"}})
|
||||||
(map
|
|
||||||
(fn [[k v]]
|
|
||||||
[(keyword (snake->kebab (name k))) v])
|
|
||||||
m)))
|
|
||||||
(j/query db "SELECT * FROM invoices")))
|
|
||||||
:headers {"Content-Type" "application/edn"}}
|
|
||||||
)
|
|
||||||
(POST "/pdf-upload"
|
(POST "/pdf-upload"
|
||||||
{{ files "file"} :params :as params}
|
{{ files "file"} :params :as params}
|
||||||
(let [{:keys [filename tempfile]} files]
|
(let [{:keys [filename tempfile]} files]
|
||||||
|
|||||||
Reference in New Issue
Block a user