actually talks to db

This commit is contained in:
Bryce Covert
2017-12-07 17:39:38 -08:00
parent 505ccfba8e
commit 9188ef7ca6
8 changed files with 101 additions and 98 deletions

View File

@@ -6,29 +6,43 @@
[ring.middleware.multipart-params :as mp]
[ring.util.response :as response]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[ring.middleware.json :refer [wrap-json-response]]))
[ring.middleware.reload :refer [wrap-reload]]
[ring.middleware.json :refer [wrap-json-response]]
[clojure.java.jdbc :as j]))
(use 'clojure.java.jdbc)
(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"}))
(defroutes app-routes
(GET "/" [] (response/resource-response "index.html" {:root "public"}))
(GET "/api/invoices" []
(j/query db "SELECT * FROM invoices"))
(POST "/pdf-upload"
{{ files "file"} :params :as params}
(let [{:keys [filename tempfile]} (second files)]
(println tempfile)
#_(io/copy tempfile (io/file "resources" "public" filename))
(for [{:keys [total date invoice-number customer-identifier]} (parse/parse-file (.getPath tempfile))]
(let [{:keys [filename tempfile]} files]
(for [{:keys [total date invoice-number customer-identifier]}
(parse/parse-file (.getPath tempfile))]
{"customer-identifier" customer-identifier
"invoice-number" invoice-number
"date" date
"total" total})))
(route/resources "/")
(routes (ANY "*" [] (response/resource-response "index.html" {:root "public"})))
(route/not-found "Not Found"))
#_(defroutes routes
(GET "/" [] (resource-response "index.html" {:root "public"}))
(resources "/"))
(def app
(wrap-json-response (mp/wrap-multipart-params app-routes)))
(wrap-json-response (mp/wrap-multipart-params (wrap-reload app-routes))))

View File

@@ -50,5 +50,4 @@
[file]
(-> (sh/sh "pdftotext" "-layout" file "-")
:out
(doto println)
parse))

View File

@@ -3,4 +3,5 @@
(def default-db
{:company {:name "Campbell brewery"}
:invoices #{}
:unpaid-invoices #{}
})

View File

@@ -19,3 +19,8 @@
::imported-invoices
(fn [db [_ new-invoices]]
(update-in db [:invoices] into new-invoices)))
(re-frame/reg-event-db
::received-invoices
(fn [db [_ new-invoices]]
(update-in db [:unpaid-invoices] into new-invoices)))

View File

@@ -15,3 +15,8 @@
::invoices
(fn [db]
(:invoices db)))
(re-frame/reg-sub
::unpaid-invoices
(fn [db]
(:unpaid-invoices db)))

View File

@@ -1,10 +1,15 @@
(ns auto-ap.views
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame]
[reagent.core :as reagent]
[auto-ap.subs :as subs]
[auto-ap.events :as events]
[auto-ap.routes :as routes]
[bidi.bidi :as bidi]))
[bidi.bidi :as bidi]
[cljs-http.client :as http]
[cljs.core.async :refer [<!]]))
(defn active-when= [active-page candidate]
(when (= active-page candidate) " active"))
@@ -12,22 +17,25 @@
(def dropzone
(with-meta
(fn []
[:form {:action "/pdf-upload" :class ".dropzone"}
[:div {:class "card"}
[:div {:class "card-header"}
[:span {:class "card-header-title"} "Upload Invoices"]]
[:div {:class "card-content"}
[:span {:class "icon"}
[:i {:class "fa fa-cloud-download"}]]
"Drop invoice pdfs here"
[:input {:type "file", :name "file", :style {:display "none"}}]]]])
[:form {:action "/pdf-upload"}
[:div.tile.notification
[:div.has-text-centered {:style {:padding "80px 0px" :width "100%"}}
[:span
[:span {:class "icon"}
[:i {:class "fa fa-cloud-download"}]]
"Drop any invoices you want to process here"]]]])
{:component-did-mount (fn [this]
(js/Dropzone. (reagent/dom-node this)
(clj->js {:init (fn []
(.on (js-this) "success" (fn [_ files]
(re-frame/dispatch [::events/imported-invoices (js->clj (.parse js/JSON files))])
)))
:url "/pdf-upload"})))}))
:paramName "file"
:url "/pdf-upload"
:previewsContainer "#dz-hidden"
:previewTemplate "<div class='dz-hidden-preview'></div>"
})))}))
(defmulti active-page identity)
@@ -41,16 +49,48 @@
[:a {:href (bidi/path-for routes/routes :import-invoices)} "Import some invoices"]]]]]])
(defmethod active-page :unpaid-invoices []
[:div {:class "inbox-messages"}
[:h1.title "Unpaid invoices"]])
[(with-meta
(fn []
(let [invoices (re-frame/subscribe [::subs/unpaid-invoices])]
[:div {:class "inbox-messages"}
[:h1.title "Unpaid invoices"]
[:table {:class "table", :style {:width "100%"}}
[:thead
[:tr
[:th "Customer"]
[:th "Invoice #"]
[:th "Date"]
[:th "Amount"]]]
[:tbody (for [{:strs [customer_identifier invoice_number date total] :as i} @invoices]
^{:key (str customer_identifier "-" invoice_number)}
[:tr
[:td customer_identifier]
[:td invoice_number]
[:td date]
[:td total]])]]]))
{:component-did-mount (fn []
(go
(->> (<! (http/get "/api/invoices"))
:body
(.parse js/JSON )
(js->clj)
(conj [::events/received-invoices])
(re-frame/dispatch))))})])
(defmethod active-page :paid-invoices []
[:div {:class "inbox-messages"}
[:h1.title "Paid invoices"]])
(defmethod active-page :invoices []
[:div {:class "inbox-messages"}
[:h1.title "All invoices"]])
[(with-meta
(fn []
[:div {:class "inbox-messages"}
[:h1.title "All invoices"]])
{:component-did-mount (fn []
(go
(re-frame/dispatch [::events/received-invoices (<! (http/get "/api/invoices"))]))
)})])
(defmethod active-page :import-invoices []
(let [invoices (re-frame/subscribe [::subs/invoices])]
@@ -147,50 +187,6 @@
[:a {:href "https://github.com/"} "Integreat"]"."]
[:p
[:a {:class "icon", :href "https://github.com/dansup/bulma-templates"}
[:i {:class "fa fa-github"}]]]]]]]))
;;
;; <nav class="navbar has-shadow">
;; <div class="container">
;; <div class="navbar-brand">
;; <a class="navbar-item" href="../">
;; <h1>Auto-ap</h1>
;; </a>
;;
;; <div class="navbar-burger burger" data-target="navMenu">
;; <span></span>
;; <span></span>
;; <span></span>
;; </div>
;; </div>
;;
;; <div id="navMenu" class="navbar-menu">
;; <div class="navbar-end">
;; <div class="navbar-item has-dropdown is-active">
;; <a class="navbar-link login">
;; Login
;; </a>
;;
;; <div class="navbar-dropdown" style="display:none">
;; <a class="navbar-item">
;; Dashboard
;; </a>
;; <a class="navbar-item">
;; Profile
;; </a>
;; <a class="navbar-item">
;; Settings
;; </a>
;; <hr class="navbar-divider">
;; <div class="navbar-item">
;; Logout
;; </div>
;; </div>
;; </div>
;; </div>
;; </div>
;; </div>
;; </nav>
[:i {:class "fa fa-github"}]]]]]]
[:div#dz-hidden]]))