Browse Source

Refactoring

Stephen Starkey 1 year ago
parent
commit
6bf5fd7bb5
1 changed files with 41 additions and 45 deletions
  1. 41
    45
      src/xp_assessment/core.clj

+ 41
- 45
src/xp_assessment/core.clj View File

@@ -38,10 +38,7 @@
38 38
 
39 39
 (defenv port "Port to listen on" "PORT" :tfn parse-int :default "8080")
40 40
 
41
-(defonce server (atom nil))
42
-
43
-(defn- safe-int [v]
44
-  (-> v str/trim parse-int))
41
+(defn- safe-int [v] (-> v str/trim parse-int))
45 42
 
46 43
 (defn- add-question [survey current-topic current-index q y n p]
47 44
   (-> survey
@@ -185,41 +182,33 @@
185 182
              (link "https://git.calmabiding.me/scstarkey/xp-assessment"
186 183
                    "source code")]])))
187 184
 
188
-(defn- render-form
189
-  ([req]
190
-   (render-page
191
-     [:div [:div {:class "intro"}
192
-            (str "Welcome to the XP Self-Assessment. Answer the questions "
193
-                 "below and get a glimpse of where your team needs to go "
194
-                 "next!")]
195
-      (render-form (:form-params req) #{} true)]))
196
-  ([form-params missing-params shuffle?]
197
-   [:div
198
-    (when (seq missing-params)
199
-      [:div {:class "error-msg"}
200
-       (str "Please answer all questions. Questions with missing answers "
201
-            "are marked in red")])
202
-    [:form {:class "questions" :method "post"}
203
-     (if shuffle?
204
-       (->> survey-questions shuffle
205
-            (map (partial render-question false form-params missing-params)))
206
-       (into
207
-         [:div]
208
-         (:questions
209
-           (reduce (fn [{:keys [current-topic] :as m} {:keys [topic] :as q}]
210
-                     (-> m
211
-                         (#(if-not (= current-topic topic)
212
-                             (update % :questions conj [:h4 topic])
213
-                             %))
214
-                         (update
215
-                           :questions conj
216
-                           (render-question true form-params missing-params q))
217
-                         (assoc :current-topic topic)))
218
-                   {:current-topic nil
219
-                    :questions []} survey-questions))))
220
-     [:div {:class "submit"}
221
-      [:input {:class "submit" :type "submit" :name "submit"
222
-               :value "submit"}]]]]))
185
+(defn- render-form [form-params missing-params shuffle?]
186
+  [:div
187
+   (when (seq missing-params)
188
+     [:div {:class "error-msg"}
189
+      (str "Please answer all questions. Questions with missing answers "
190
+           "are marked in red")])
191
+   [:form {:class "questions" :method "post"}
192
+    (if shuffle?
193
+      (->> survey-questions shuffle
194
+           (map (partial render-question false form-params missing-params)))
195
+      (into
196
+        [:div]
197
+        (:questions
198
+          (reduce (fn [{:keys [current-topic] :as m} {:keys [topic] :as q}]
199
+                    (-> m
200
+                        (#(if-not (= current-topic topic)
201
+                            (update % :questions conj [:h4 topic])
202
+                            %))
203
+                        (update
204
+                          :questions conj
205
+                          (render-question true form-params missing-params q))
206
+                        (assoc :current-topic topic)))
207
+                  {:current-topic nil
208
+                   :questions []} survey-questions))))
209
+    [:div {:class "submit"}
210
+     [:input {:class "submit" :type "submit" :name "submit"
211
+              :value "submit"}]]]])
223 212
 
224 213
 (defn- add-to-score [base-score addition] (+ (or base-score 0) addition))
225 214
 
@@ -275,7 +264,15 @@
275 264
             "Check out James Shore's excellent book for more")]
276 265
      (render-form form-params #{} false)]))
277 266
 
278
-(defn- score-form [{:keys [form-params] :as req}]
267
+(defn- display-form [{:keys [form-params]}]
268
+  (render-page
269
+    [:div [:div {:class "intro"}
270
+           (str "Welcome to the XP Self-Assessment. Answer the questions "
271
+                "below and get a glimpse of where your team needs to go "
272
+                "next!")]
273
+     (render-form form-params #{} true)]))
274
+
275
+(defn- score-form [{:keys [form-params]}]
279 276
   (let [{:keys [missing-params]}
280 277
         (reduce
281 278
           (fn [m i]
@@ -285,14 +282,14 @@
285 282
                 m)))
286 283
           {:missing-params #{}} (keys questions-by-index))]
287 284
     (render-page (if (seq missing-params)
288
-                   (render-form (:form-params req) missing-params true)
285
+                   (render-form form-params missing-params true)
289 286
                    (render-results form-params)))))
290 287
 
291 288
 (defn- unhandled-request [{:keys [request-method] :as req}]
292 289
   (log/error req "Unhandled request")
293 290
   (str "Unable to handle request method: " request-method))
294 291
 
295
-(def ^:private handlers {:post score-form :get render-form})
292
+(def ^:private handlers {:post score-form, :get display-form})
296 293
 
297 294
 (defn- app [{:keys [request-method] :as req}]
298 295
   (let [handler (handlers request-method)]
@@ -300,8 +297,7 @@
300 297
      :headers {"Content-Type" "text/html"}
301 298
      :body ((or handler unhandled-request) req)}))
302 299
 
303
-(defn -main [& args]
300
+(defn -main [& _]
304 301
   (display-env)
305 302
   (log/info "Starting server")
306
-  (reset! server
307
-          (srv/run-server (-> #'app wrap-reload wrap-params) {:port @port})))
303
+  (srv/run-server (-> #'app wrap-reload wrap-params) {:port @port}))