Commit 1f9c04f0 authored by Johannes Knödtel's avatar Johannes Knödtel 🎶
Browse files

add feature to lock sessions

fixes #6
parent 811cda01
BEGIN TRANSACTION;
--;;
ALTER TABLE sessions
DROP COLUMN locked;
--;;
COMMIT;
BEGIN TRANSACTION;
--;;
ALTER TABLE sessions
ADD COLUMN locked BOOLEAN NOT NULL;
--;;
COMMIT;
......@@ -59,6 +59,33 @@ FROM
ORDER BY
id DESC;
-- :name session-locked? :? :1
-- :doc checks if session is locked
SELECT
locked
FROM
sessions
WHERE
id = :session;
-- :name lock-session! :!
-- :doc locks the session
UPDATE
sessions
SET
locked = true
WHERE
id = :session;
-- :name unlock-session! :!
-- :doc unlocks the session
UPDATE
sessions
SET
locked = false
WHERE
id = :session;
-- :name create-session<! :i!
-- :doc generate new session
INSERT INTO sessions
......
......@@ -9,10 +9,20 @@
<h4>Wähle eine Einkaufssession:</h4>
<ul class="collection">
{% for session in sessions %}
<li class="collection-item">
<a href="/{{session.id}}"><span style="width:100%; display:block;">
{{session.id}}. {{session.name}}
</span></a>
<li class="collection-item" style="padding-bottom:1.75em;">
<a href="/{{session.id}}" style="vertical-align:center;margin-top=0.5em">{{session.id}}. {{session.name}}</a>
<span class="secondary-content">
<form method="POST" action="/unlock-session" style="display:inline;margin-right:.5em">
{% csrf-field %}
<input type="text" name="session" value="{{session.id}}" style="display:none" />
<button class="btn btn-floating" type="submit"><i class="material-icons small cyan">lock_open</i></input>
</form>
<form method="POST" action="/lock-session" style="display:inline">
{% csrf-field %}
<input type="text" name="session" value="{{session.id}}" style="display:none;" />
<button class="btn btn-floating" type="submit"><i class="material-icons small red">lock</i></input>
</form>
</span>
</li>
{% endfor %}
</ul>
......
......@@ -100,3 +100,13 @@
:quantity quantity
:session session}
headers)))
(POST "/lock-session" [session :<< as-int
:as {headers :headers}]
(pages/lock-session {:session session}
headers))
(POST "/unlock-session" [session :<< as-int
:as {headers :headers}]
(pages/unlock-session {:session session}
headers)))
......@@ -47,7 +47,8 @@
(util/modifing-request args headers
identity
valid/validate-food-request
db/add-food!))
db/add-food!
:session-lockable? true))
(defn translate-add-price-request [request-map]
(let [food-name-comment (:food_name_comment request-map)
......@@ -81,10 +82,24 @@
(util/modifing-request args headers
identity
valid/validate-delete-order
db/delete-order!))
db/delete-order!
:session-lockable? true))
(defn edit-order [args headers]
(util/modifing-request args headers
identity
valid/validate-edit-order
db/edit-order!))
db/edit-order!
:session-lockable? true))
(defn lock-session [args headers]
(util/modifing-request args headers
identity
valid/validate-lock-session
db/lock-session!))
(defn unlock-session [args headers]
(util/modifing-request args headers
identity
valid/validate-unlock-session
db/unlock-session!))
......@@ -77,17 +77,28 @@
(defn validation-error [error-string]
(status (layout/render "error.html" {:error-string error-string}) 400))
(defn session-locked []
(status (layout/render "error.html"
{:error-string "This session is locked. No order can be placed anymore"})
400))
(defn redirect-to-referrer [headers]
(found (or (headers "referer") "/")))
(defn modifing-request [request headers
request-transformation-fn
validataion-fn
database-action-fn]
database-action-fn
& {:keys [session-lockable?]
:or {session-lockable? false}}]
(let [args (request-transformation-fn request)
validation-errors (first (validataion-fn args))]
(if validation-errors
(validation-error validation-errors)
(if (and session-lockable? (db/run db/session-locked? args))
(do
(db/run database-action-fn args)
(redirect-to-referrer headers)))))
(session-locked)
(redirect-to-referrer headers))
(if validation-errors
(validation-error validation-errors)
(do
(db/run database-action-fn args)
(redirect-to-referrer headers))))))
......@@ -62,3 +62,9 @@
(generate-request-validator validate-payment-request
#{:customer})
(generate-request-validator validate-lock-session
#{:session})
(generate-request-validator validate-unlock-session
#{:session})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment