Skip to content

Commit

Permalink
some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
kwieloch committed Jan 20, 2019
1 parent a1d9e82 commit 5397b8d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 48 deletions.
2 changes: 1 addition & 1 deletion resources/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ VALUES (:name, :message, :timestamp)

-- :name get-messages :? :*
-- :doc selects all available messages
SELECT * FROM guestbook WHERE timestamp is not null
SELECT * FROM guestbook
17 changes: 17 additions & 0 deletions src/clj/guestbook/business.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(ns guestbook.business
(:require [guestbook.layout :as layout]
[guestbook.db.core :as db]
[compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :as response]
[struct.core :as st]))

(def message-schema
[[:name
st/required
st/string]

[:message
st/required
st/string
{:message "message must contain at least 10 characters"
:validate #(> (count %) 9)}]])
15 changes: 15 additions & 0 deletions src/clj/guestbook/comments.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(ns guestbook.comments
(:require [struct.core :as st]))

(def message-schema
[[:name
st/required
st/string]
[:message
st/required
st/string
{:message "message must contain at least 10 characters"
:validate #(> (count %) 9)}]])

(defn validate-message [params]
(first (st/validate params message-schema)))
24 changes: 4 additions & 20 deletions src/clj/guestbook/routes/home.clj
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
(ns guestbook.routes.home
(:require [guestbook.layout :as layout]
(:require [compojure.core :refer [defroutes GET]]
[guestbook.db.core :as db]
[compojure.core :refer [defroutes GET POST]]
[ring.util.http-response :as response]
[struct.core :as st]))

(def message-schema
[[:name
st/required
st/string]

[:message
st/required
st/string
{:message "message must contain at least 10 characters"
:validate #(> (count %) 9)}]])

(defn validate-message [params]
(first (st/validate params message-schema)))
[guestbook.layout :as layout]
[ring.util.http-response :as response]))

(defn home-page []
(layout/render
"home.html"))
(layout/render "home.html"))

(defn about-page []
(layout/render "about.html"))
Expand Down
13 changes: 8 additions & 5 deletions src/clj/guestbook/routes/websockets.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[clojure.tools.logging :as log]
[immutant.web.async :as async]
[cognitect.transit :as transit]
[guestbook.routes.home :as bl]
[guestbook.comments :as comments]
[guestbook.db.core :as db]
[taoensso.sente :as sente]
[mount.core :refer [defstate]]
Expand All @@ -24,21 +24,24 @@
)

(defn save-message [msg]
(if-let [errors (bl/validate-message msg)]
(if-let [errors (comments/validate-message msg)]
{:errors errors}
(let [msg-ts (assoc msg :timestamp (java.util.Date.))]
(db/save-message! msg-ts)
msg-ts)))

(defn notify-clients [event-id msg]
(doseq [uid (:any @connected-uids)]
(chsk-send! uid [event-id msg])))

(defn handle-message [{:keys [id client-id ?data ?reply-fn]}]
(when (= id :guestbook/add-message)
(let [result (save-message ?data)]
(if (:errors result)
(?reply-fn result)
(do
(?reply-fn {:guestbook/message-added result})
(doseq [uid (:any @connected-uids)]
(chsk-send! uid [:guestbook/message-added result])))))))
#_(?reply-fn {:guestbook/message-added result})
(notify-clients :guestbook/message-added result))))))

(defn stop-router [stop-fn]
(when stop-fn (stop-fn)))
Expand Down
45 changes: 23 additions & 22 deletions src/cljs/guestbook/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,32 @@
[:div.alert.alert-danger description]))

(defn message-form [fields errors]
[:div.content [:div.form-group
[:p "name: " (:name @fields)]
[:p "message: " (:message @fields)]
[:p "errors: " @errors]
[:div.content
[:div.form-group
[:p "name: " (:name @fields)]
[:p "message: " (:message @fields)]
[:p "errors: " @errors]

[:p "Name"]
[:input.form-control {:type :text
:name :name
:value (:name @fields)
:on-change #(swap! fields assoc :name (-> % .-target .-value))}]
[show-errors @errors :name]
[:p "Name"]
[:input.form-control {:type :text
:name :name
:value (:name @fields)
:on-change #(swap! fields assoc :name (-> % .-target .-value))}]
[show-errors @errors :name]

[:p "Message"]
[:textarea.form-control {:rows 4 :cols 50
:name :message
:value (:message @fields)
:on-change #(swap! fields assoc :message (-> % .-target .-value) )}]
[show-errors @errors :message]
[:p "Message"]
[:textarea.form-control {:rows 4 :cols 50
:name :message
:value (:message @fields)
:on-change #(swap! fields assoc :message (-> % .-target .-value) )}]
[show-errors @errors :message]

[:input.btn.btn-primary {:type :submit
:value "Comment"
:on-click #(ws/chsk-send!
[:guestbook/add-message @fields]
80
(feedback-handler fields errors))}]]])
[:input.btn.btn-primary {:type :submit
:value "Comment"
:on-click #(ws/chsk-send!
[:guestbook/add-message @fields]
80
(feedback-handler fields errors))}]]])

(defn home []
(let [messages (reagent/atom nil)
Expand Down

0 comments on commit 5397b8d

Please sign in to comment.