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

@@ -12,7 +12,12 @@
[ring/ring-defaults "0.2.1"] [ring/ring-defaults "0.2.1"]
[ring/ring-json "0.4.0"] [ring/ring-json "0.4.0"]
[ring "1.4.0"] [ring "1.4.0"]
[yogthos/config "0.8"]] [yogthos/config "0.8"]
[org.clojure/java.jdbc "0.7.3"]
;; https://mvnrepository.com/artifact/postgresql/postgresql
[postgresql/postgresql "9.3-1102.jdbc41"]
[cljs-http "0.1.44"]
[org.clojure/core.async "0.3.465"]]
:plugins [[lein-ring "0.9.7"] :plugins [[lein-ring "0.9.7"]
[lein-cljsbuild "1.1.5"]] [lein-cljsbuild "1.1.5"]]
:clean-targets ^{:protect false} ["resources/public/js/compiled" "target"] :clean-targets ^{:protect false} ["resources/public/js/compiled" "target"]

View File

@@ -12,10 +12,7 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.0/css/bulma.min.css" integrity="sha256-HEtF7HLJZSC3Le1HcsWbz1hDYFPZCqDhZa9QsCgVUdw=" crossorigin="anonymous" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.0/css/bulma.min.css" integrity="sha256-HEtF7HLJZSC3Le1HcsWbz1hDYFPZCqDhZa9QsCgVUdw=" crossorigin="anonymous" />
<style> <style>
.dz-error-mark { display:none} .dz-details {display:none} form { border: 2px dashed lightgray;}
.dz-success-mark {display:none}
.dz-image {display:none}
form { width: 100% }
html,body { html,body {
font-family: 'Open Sans', serif; font-family: 'Open Sans', serif;
@@ -261,8 +258,6 @@
</div> </div>
</footer> </footer>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.min.js" integrity="sha256-5CEXP4Sh+bwJYBngjYYh2TEev9kTDwcjw60jZatTHtY=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js" integrity="sha256-QHdJObhDO++VITP6S4tMlDHRWMaUOk+s/xWIRgF/YY0=" crossorigin="anonymous"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js" integrity="sha256-4PIvl58L9q7iwjT654TQJM+C/acEyoG738iL8B8nhXg=" crossorigin="anonymous"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js" integrity="sha256-4PIvl58L9q7iwjT654TQJM+C/acEyoG738iL8B8nhXg=" crossorigin="anonymous"></script>
<script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> <script src="http://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
@@ -305,23 +300,6 @@
}) })
}); });
var myDropzone = new Dropzone("#my-dropzone");
myDropzone.on("success", function(file, a) {
$("h2").show();
$(".found-invoices").show();
JSON.parse(a).map(function(x) {
console.log(x);
var tr = $("<tr>");
tr.append($("<td>").text(x['customer-identifier']));
tr.append($("<td>").text(x['invoice-number']));
tr.append($("<td>").text(x['date']));
tr.append($("<td>").text(x['total']));
$("tbody").append(tr);
});
});
});
*/ */
</script> </script>
</div> </div>

View File

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

View File

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

View File

@@ -19,3 +19,8 @@
::imported-invoices ::imported-invoices
(fn [db [_ new-invoices]] (fn [db [_ new-invoices]]
(update-in db [:invoices] into 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 ::invoices
(fn [db] (fn [db]
(:invoices db))) (:invoices db)))
(re-frame/reg-sub
::unpaid-invoices
(fn [db]
(:unpaid-invoices db)))

View File

@@ -1,10 +1,15 @@
(ns auto-ap.views (ns auto-ap.views
(:require-macros [cljs.core.async.macros :refer [go]])
(:require [re-frame.core :as re-frame] (:require [re-frame.core :as re-frame]
[reagent.core :as reagent] [reagent.core :as reagent]
[auto-ap.subs :as subs] [auto-ap.subs :as subs]
[auto-ap.events :as events] [auto-ap.events :as events]
[auto-ap.routes :as routes] [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] (defn active-when= [active-page candidate]
(when (= active-page candidate) " active")) (when (= active-page candidate) " active"))
@@ -12,22 +17,25 @@
(def dropzone (def dropzone
(with-meta (with-meta
(fn [] (fn []
[:form {:action "/pdf-upload" :class ".dropzone"} [:form {:action "/pdf-upload"}
[:div {:class "card"} [:div.tile.notification
[:div {:class "card-header"} [:div.has-text-centered {:style {:padding "80px 0px" :width "100%"}}
[:span {:class "card-header-title"} "Upload Invoices"]] [:span
[:div {:class "card-content"} [:span {:class "icon"}
[:span {:class "icon"} [:i {:class "fa fa-cloud-download"}]]
[:i {:class "fa fa-cloud-download"}]]
"Drop invoice pdfs here" "Drop any invoices you want to process here"]]]])
[:input {:type "file", :name "file", :style {:display "none"}}]]]])
{:component-did-mount (fn [this] {:component-did-mount (fn [this]
(js/Dropzone. (reagent/dom-node this) (js/Dropzone. (reagent/dom-node this)
(clj->js {:init (fn [] (clj->js {:init (fn []
(.on (js-this) "success" (fn [_ files] (.on (js-this) "success" (fn [_ files]
(re-frame/dispatch [::events/imported-invoices (js->clj (.parse js/JSON 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) (defmulti active-page identity)
@@ -41,16 +49,48 @@
[:a {:href (bidi/path-for routes/routes :import-invoices)} "Import some invoices"]]]]]]) [:a {:href (bidi/path-for routes/routes :import-invoices)} "Import some invoices"]]]]]])
(defmethod active-page :unpaid-invoices [] (defmethod active-page :unpaid-invoices []
[:div {:class "inbox-messages"} [(with-meta
[:h1.title "Unpaid invoices"]]) (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 [] (defmethod active-page :paid-invoices []
[:div {:class "inbox-messages"} [:div {:class "inbox-messages"}
[:h1.title "Paid invoices"]]) [:h1.title "Paid invoices"]])
(defmethod active-page :invoices [] (defmethod active-page :invoices []
[:div {:class "inbox-messages"} [(with-meta
[:h1.title "All invoices"]]) (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 [] (defmethod active-page :import-invoices []
(let [invoices (re-frame/subscribe [::subs/invoices])] (let [invoices (re-frame/subscribe [::subs/invoices])]
@@ -147,50 +187,6 @@
[:a {:href "https://github.com/"} "Integreat"]"."] [:a {:href "https://github.com/"} "Integreat"]"."]
[:p [:p
[:a {:class "icon", :href "https://github.com/dansup/bulma-templates"} [:a {:class "icon", :href "https://github.com/dansup/bulma-templates"}
[:i {:class "fa fa-github"}]]]]]]])) [:i {:class "fa fa-github"}]]]]]]
[:div#dz-hidden]]))
;;
;; <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>