Show sessions limericks

main
Eric Ihli 4 years ago
parent d72e7ecf42
commit 0fb359c9ec

@ -13,6 +13,7 @@
[com.darklimericks.util.identicon :as identicon] [com.darklimericks.util.identicon :as identicon]
[com.darklimericks.server.limericks :as limericks] [com.darklimericks.server.limericks :as limericks]
[com.darklimericks.db.albums :as db.albums] [com.darklimericks.db.albums :as db.albums]
[com.darklimericks.db.limericks :as db.limericks]
[com.darklimericks.server.util :as util] [com.darklimericks.server.util :as util]
[com.darklimericks.server.system] [com.darklimericks.server.system]
[reitit.core :as reitit])) [reitit.core :as reitit]))
@ -62,8 +63,12 @@
(comment (comment
((-> state/system (require '[com.darklimericks.db.limericks :as db.limericks])
:app/server)) (let [session
(java.util.UUID/fromString "4605f687-4e91-47de-abdf-458ef7d47b7e")]
(db.limericks/limericks-by-session
(-> state/system :com.darklimericks.db.core/connection)
session))
(init) (init)
(auto-reset) (auto-reset)
(let [db (-> state/system :database.sql/connection) (let [db (-> state/system :database.sql/connection)

@ -57,6 +57,15 @@
:select :* :select :*
:from :limerick)))) :from :limerick))))
(defn limericks-by-session [db session]
(->> (honey.sql/build
:select :*
:from :limerick
:join [:session_limerick [:= :session_limerick.limerick_id :limerick.id]]
:where [:= :session_limerick.session_id session])
honey.sql/format
(jdbc/execute! db)))
(defn album-limericks-sql [album-id] (defn album-limericks-sql [album-id]
(honey.sql/format (honey.sql/format
(honey.sql/build (honey.sql/build

@ -1,6 +1,9 @@
(ns com.darklimericks.server.handlers (ns com.darklimericks.server.handlers
(:require [taoensso.timbre :as timbre] (:require [taoensso.timbre :as timbre]
[hiccup.core :as hiccup] [hiccup.core :as hiccup]
[next.jdbc :as jdbc]
[honeysql.core]
[honeysql.helpers]
[reitit.ring :as ring] [reitit.ring :as ring]
[reitit.http :as http] [reitit.http :as http]
[reitit.interceptor.sieppari :as sieppari] [reitit.interceptor.sieppari :as sieppari]
@ -45,21 +48,32 @@
(defn limerick-generation-post-handler (defn limerick-generation-post-handler
[db cache] [db cache]
(fn [{{:keys [scheme]} :params (fn [{{:keys [scheme]} :params
{:keys [session-id]} :session session-id :session/key
:as request}] :as request}]
(let [scheme (limericks/parse-scheme scheme) (if-let [session-id (-> session-id
mid (car/wcar db (car-mq/enqueue (string/split #":")
"limericks" (nth 2)
{:scheme scheme java.util.UUID/fromString)]
:session-id session-id}))] (let [scheme (limericks/parse-scheme scheme)
{:status 301 mid (car/wcar db (car-mq/enqueue
"limericks"
{:scheme scheme
:session-id session-id}))]
{:status 301
:headers {"Content-Type" "text/html; charset=utf-8"}
:body (views/wrapper
db
request
{}
[:h1 "Creating your limerick..."]
[:div "Submission processing... " mid])})
{:status 400
:headers {"Content-Type" "text/html; charset=utf-8"} :headers {"Content-Type" "text/html; charset=utf-8"}
:body (views/wrapper :body (views/wrapper
db db
request request
{} {}
[:h1 "Creating your limerick..."] [:div "Enable cookies to submit limericks"])})))
[:div "Submission processing... " mid])})))
(defn limericks-get-handler [db cache] (defn limericks-get-handler [db cache]
(fn [request] (fn [request]
@ -213,14 +227,25 @@
(defn submit-limericks-get-handler [db] (defn submit-limericks-get-handler [db]
(fn [request] (fn [request]
{:status 200 (if-let [session-key (:session/key request)]
:headers {"Content-Type" "text/html; charset=uft-8"} (let [session-key (-> session-key
:session (if (empty? (:session request)) (string/split #":")
{:session-id (java.util.UUID/randomUUID)} (nth 2)
(:session request)) java.util.UUID/fromString)
:body (views/wrapper limericks (db.limericks/limericks-by-session db session-key)]
db (println session-key)
request (println limericks)
{} {:status 200
(views/submit-limericks request))})) :headers {"Content-Type" "text/html; charset=uft-8"}
:body (views/wrapper
db
request
{}
(views/submit-limericks request limericks))})
{:status 200
:headers {"Content-Type" "text/html; charset=uft-8"}
:body (views/wrapper
db
request
{}
[:div "Enable cookies to submit limericks."])})))

@ -16,7 +16,7 @@
["/limerick-generation-task" ["/limerick-generation-task"
{:name ::limerick-generation-task {:name ::limerick-generation-task
:post {:handler (handlers/limerick-generation-post-handler db cache)} :post {:handler (handlers/limerick-generation-post-handler db cache)}
:get {:handler (handlers/limerick-generation-get-handler db cache)}}] :get {:handler (handlers/submit-limericks-get-handler db)}}]
["/submit" ["/submit"
{:name ::submit {:name ::submit
:get {:handler (handlers/submit-limericks-get-handler db)}}] :get {:handler (handlers/submit-limericks-get-handler db)}}]

@ -157,7 +157,7 @@
[:div line])])) [:div line])]))
(defn submit-limericks (defn submit-limericks
[{{:keys [session-id]} :session :as request}] [request limericks]
[:div [:div
[:h1 "Submit Limericks"] [:h1 "Submit Limericks"]
(form/form-to (form/form-to
@ -166,6 +166,9 @@
:com.darklimericks.server.router/limerick-generation-task)] :com.darklimericks.server.router/limerick-generation-task)]
(form/text-field "scheme") (form/text-field "scheme")
(form/submit-button "Generate dark limerick")) (form/submit-button "Generate dark limerick"))
[:div [:div "Session " (:session/key request)]
"Currently processing " (for [limerick limericks]
session-id]]) [:div
[:h3 (:limerick/name limerick)]
(for [line (string/split (:limerick/text limerick) #"\n")]
[:div line])])])

Loading…
Cancel
Save