major structure overhaul

integration-tests
Josha von Gizycki 6 years ago
parent b62c25df8d
commit 82cd9ed367

@ -1,15 +1,15 @@
(ns wanijo.auth (ns wanijo.framework.auth
(:require [compojure.core :refer [POST GET defroutes]] (:require [compojure.core :refer [POST GET defroutes]]
[ring.util.response :refer [redirect]] [ring.util.response :refer [redirect]]
[ring.util.anti-forgery :refer [anti-forgery-field]] [ring.util.anti-forgery :refer [anti-forgery-field]]
[buddy.hashers :as hashers] [buddy.hashers :as hashers]
[hiccup.form :as hform] [hiccup.form :as hform]
[wanijo.view.page :as page] [wanijo.framework.view :as view]
[wanijo.domain.user :as domain-user])) [wanijo.user.domain :as user-domain]))
(defn- login-check! [req] (defn- login-check! [req]
(let [{{:keys [uname pw]} :params} req (let [{{:keys [uname pw]} :params} req
unode (domain-user/find! uname) unode (user-domain/find! uname)
pwmatch (when-let [hash (:pw unode)] pwmatch (when-let [hash (:pw unode)]
(hashers/check pw hash))] (hashers/check pw hash))]
(if pwmatch (if pwmatch
@ -20,7 +20,7 @@
(assoc :flash :invalid-credentials))))) (assoc :flash :invalid-credentials)))))
(defn login [req] (defn login [req]
(page/layout! (view/layout!
:content :content
[[:h1 "Kama ken"] [[:h1 "Kama ken"]
(hform/form-to (hform/form-to

@ -1,4 +1,4 @@
(ns wanijo.devmode (ns wanijo.framework.devmode
(:require [hiccup.core :as hcore])) (:require [hiccup.core :as hcore]))
(def bar-entries (atom [])) (def bar-entries (atom []))

@ -0,0 +1,43 @@
(ns wanijo.framework.form
(:require [clojure.spec.alpha :as spec]
[hiccup.form :as hform]
[wanijo.framework.view :as view]))
(defn spec-to-errmsg [label spec-key field-value]
(view/flash-error
(map
(fn [prob]
[:p
"Field "
[:span.flash__field label]
" must comply to "
[:span.flash__pred (:pred prob)]])
(:clojure.spec.alpha/problems
(spec/explain-data spec-key field-value)))))
(defn field-valid? [value spec-key req]
(or (empty? (:form-params req))
(spec/valid? spec-key value)))
(defn field [form-def field req]
(let [field-value (get-in req [:params field])
field-def (get-in form-def [:fields field])
{:keys [label required] spec-key :spec} field-def]
(list
(when-not (field-valid? field-value spec-key req)
(spec-to-errmsg label spec-key field-value))
(hform/label field label)
(hform/text-field {:required (when required "required")}
field
field-value))))
(defn valid? [form-def req]
(reduce-kv
(fn [result field value]
(if-let [field-spec (get-in form-def [:fields field :spec])]
(if (spec/valid? field-spec value)
true
(reduced false))
result))
true
(:params req)))

@ -1,6 +1,7 @@
(ns wanijo.neo4j (ns wanijo.framework.neo4j
(:require [neo4j-clj.core :as db] (:require [neo4j-clj.core :as db]
[wanijo.devmode :as devmode])) [wanijo.framework.devmode :as devmode])
(:import (java.util UUID)))
(def conn (def conn
(delay (db/connect (delay (db/connect
@ -11,7 +12,7 @@
(defmacro defquery [& args] `(db/defquery ~@args)) (defmacro defquery [& args] `(db/defquery ~@args))
(defn uuid [] (defn uuid []
(str (java.util.UUID/randomUUID))) (str (UUID/randomUUID)))
(defn butiful-query [qry] (defn butiful-query [qry]
(->> qry (->> qry

@ -1,4 +1,4 @@
(ns wanijo.repl (ns wanijo.framework.repl
(:require [buddy.hashers :as hashers] (:require [buddy.hashers :as hashers]
[neo4j-clj.core :as db] [neo4j-clj.core :as db]
[wanijo.neo4j :as neo4j])) [wanijo.neo4j :as neo4j]))

@ -0,0 +1,49 @@
(ns wanijo.framework.view
(:require [hiccup.page :refer
[html5 include-css]]))
(defn btnlink
([target caption]
(btnlink target caption ""))
([target caption class]
[:a {:href target}
[:button {:class class}
caption]]))
(defn layout!
[& {:keys [content title session]
:or {content []
title nil
session {}}}]
(let [ident (:ident session)
authed? (some? ident)
devmode? (:devmode session)]
(html5
[:head
[:meta {:charset "utf-8"}]
[:meta {:name "viewport"
:content "width=device-width,initial-scale=1,shrink-to-fit=no"}]
[:title (str "wan ijo" (when title (str " - " title)))]
(include-css "/css/app.css")
(when devmode? (include-css "/css/devmode.css"))]
[:body
[:section.grid
[:header
[:h1.app-title "wan ijo"
(when authed?
[:small.app-title__hello
(str "O, " ident)])]
(when authed?
[:section.header-content
(btnlink "/schema" "Jaki ijo" "header-content__link")
(btnlink "/logout" "Lape" "header-content__link")])]
[:nav (when authed? "nav")]
(vec (concat [:main] content))
[:aside (when authed? "aside")]
[:footer
[:small "Ilo pali e ijo"]]]])))
(defn flash-error [content]
[:section.flash--error
[:h2.flash__heading--error "Pakala"]
content])

@ -6,10 +6,10 @@
:refer [wrap-defaults site-defaults]] :refer [wrap-defaults site-defaults]]
[ring.util.response :as rur] [ring.util.response :as rur]
[ring.middleware.session.cookie :as session-cookie] [ring.middleware.session.cookie :as session-cookie]
[wanijo.auth :refer [auth-routes]]
[wanijo.home.routes :refer [home-routes]] [wanijo.home.routes :refer [home-routes]]
[wanijo.schema.routes :refer [schema-routes]] [wanijo.schema.routes :refer [schema-routes]]
[wanijo.devmode :as devmode])) [wanijo.framework.auth :refer [auth-routes]]
[wanijo.framework.devmode :as devmode]))
(defn- wrap-login-redirect [handler] (defn- wrap-login-redirect [handler]
(fn [req] (fn [req]

@ -1,5 +1,5 @@
(ns wanijo.home.view (ns wanijo.home.view
(:require [wanijo.view.page :as page])) (:require [wanijo.framework.view :as view]))
(defn root! [req] (defn root! [req]
(page/layout! :session (:session req))) (view/layout! :session (:session req)))

@ -1,6 +1,6 @@
(ns wanijo.domain.schema (ns wanijo.schema.domain
(:require [clojure.spec.alpha :as spec] (:require [clojure.spec.alpha :as spec]
[wanijo.neo4j :as neo4j])) [wanijo.framework.neo4j :as neo4j]))
(spec/def ::name (spec/def ::name
(spec/and string? not-empty)) (spec/and string? not-empty))

@ -1,14 +1,15 @@
(ns wanijo.schema.routes (ns wanijo.schema.routes
(:require [compojure.core :refer [defroutes GET POST]] (:require [compojure.core :refer [defroutes GET POST]]
[ring.util.response :as resp] [ring.util.response :as resp]
[wanijo.domain.schema :as domain-schema] [wanijo.framework.view :as view]
[wanijo.view.forms :as forms] [wanijo.framework.form :as form]
[wanijo.schema.domain :as domain]
[wanijo.schema.view :as view-schema])) [wanijo.schema.view :as view-schema]))
(defn- new! [req] (defn- new! [req]
(if (forms/valid? view-schema/new-form req) (if (form/valid? view-schema/new-form req)
(do (do
(domain-schema/create-new! (domain/create-new!
(get-in req [:params :schema-name]) (get-in req [:params :schema-name])
(get-in req [:session :uuid])) (get-in req [:session :uuid]))
(resp/redirect "/schema")) (resp/redirect "/schema"))

@ -1,9 +1,9 @@
(ns wanijo.schema.view (ns wanijo.schema.view
(:require [hiccup.form :as hform] (:require [hiccup.form :as hform]
[ring.util.anti-forgery :refer [anti-forgery-field]] [ring.util.anti-forgery :refer [anti-forgery-field]]
[wanijo.view.page :as page] [wanijo.framework.view :as view]
[wanijo.domain.schema :as domain-schema] [wanijo.framework.form :as form]
[wanijo.view.forms :as forms])) [wanijo.schema.domain :as domain]))
(def new-form (def new-form
{:fields {:schema-name {:label "Nimi" {:fields {:schema-name {:label "Nimi"
@ -13,8 +13,8 @@
(defn overview! [req] (defn overview! [req]
(let [session (:session req) (let [session (:session req)
uuid (:uuid session) uuid (:uuid session)
schemas (domain-schema/all-created-by! uuid)] schemas (domain/all-created-by! uuid)]
(page/layout! (view/layout!
:session session :session session
:content :content
[[:h1 "Ali jaki ijo"] [[:h1 "Ali jaki ijo"]
@ -31,6 +31,6 @@
[:h1 "Pali sin e jaki ijo"] [:h1 "Pali sin e jaki ijo"]
(hform/form-to (hform/form-to
[:post "/schema/new"] [:post "/schema/new"]
(forms/field new-form :schema-name req) (form/field new-form :schema-name req)
(hform/submit-button "Pali") (hform/submit-button "Pali")
(anti-forgery-field))]))) (anti-forgery-field))])))

@ -1,5 +1,5 @@
(ns wanijo.domain.user (ns wanijo.user.domain
(:require [wanijo.neo4j :as neo4j])) (:require [wanijo.framework.neo4j :as neo4j]))
(neo4j/defquery (neo4j/defquery
find-user find-user

@ -1,43 +0,0 @@
(ns wanijo.view.forms
(:require [hiccup.form :as hform]
[clojure.spec.alpha :as spec]
[wanijo.view.page :as page]))
(defn spec-to-errmsg [label spec-key field-value]
(page/flash-error
(map
(fn [prob]
[:p
"Field "
[:span.flash__field label]
" must comply to "
[:span.flash__pred (:pred prob)]])
(:clojure.spec.alpha/problems
(spec/explain-data spec-key field-value)))))
(defn field-valid? [value spec-key req]
(or (empty? (:form-params req))
(spec/valid? spec-key value)))
(defn field [form-def field req]
(let [field-value (get-in req [:params field])
field-def (get-in form-def [:fields field])
{:keys [label required] spec-key :spec} field-def]
(list
(when-not (field-valid? field-value spec-key req)
(spec-to-errmsg label spec-key field-value))
(hform/label field label)
(hform/text-field {:required (when required "required")}
field
field-value))))
(defn valid? [form-def req]
(reduce-kv
(fn [result field value]
(if-let [field-spec (get-in form-def [:fields field :spec])]
(if (spec/valid? field-spec value)
true
(reduced false))
result))
true
(:params req)))

@ -1,49 +0,0 @@
(ns wanijo.view.page
(:require [hiccup.page :refer
[html5 include-css]]))
(defn btnlink
([target caption]
(btnlink target caption ""))
([target caption class]
[:a {:href target}
[:button {:class class}
caption]]))
(defn layout!
[& {:keys [content title session]
:or {content []
title nil
session {}}}]
(let [ident (:ident session)
authed? (some? ident)
devmode? (:devmode session)]
(html5
[:head
[:meta {:charset "utf-8"}]
[:meta {:name "viewport"
:content "width=device-width,initial-scale=1,shrink-to-fit=no"}]
[:title (str "wan ijo" (when title (str " - " title)))]
(include-css "/css/app.css")
(when devmode? (include-css "/css/devmode.css"))]
[:body
[:section.grid
[:header
[:h1.app-title "wan ijo"
(when authed?
[:small.app-title__hello
(str "O, " ident)])]
(when authed?
[:section.header-content
(btnlink "/schema" "Jaki ijo" "header-content__link")
(btnlink "/logout" "Lape" "header-content__link")])]
[:nav (when authed? "nav")]
(vec (concat [:main] content))
[:aside (when authed? "aside")]
[:footer
[:small "Ilo pali e ijo"]]]])))
(defn flash-error [content]
[:section.flash--error
[:h2.flash__heading--error "Pakala"]
content])
Loading…
Cancel
Save