Commit 04e2d3a6 authored by Johannes Knödtel's avatar Johannes Knödtel 🎶
Browse files

move pages functions to gms.routes.pages

parent eeeefaa2
(ns gms.routes.home
(:require [gms.routes.utils :as util]
[gms.layout :as layout]
[gms.db.core :as db]
[gms.tex :as tex]
[gms.routes.pages :as pages]
[gms.irc :as irc]
[gms.validators :as valid]
[compojure.core :refer [defroutes GET POST DELETE]]
[compojure.coercions :refer [as-int as-uuid]]
[ring.util.http-response :refer [ok found bad-request]]
[clojure.set :refer [rename-keys]]
[clojure.pprint :as pp]
[clojure.java.io :as io]))
(defn list-page
([session headers] (list-page [] session headers))
([food-options session headers]
(let [items (db/run db/get-food {:session session})
numbers (util/group-numbers (db/run db/get-food-numbers {:session session}))
summed-numbers (util/add-difference (db/run db/get-summed-food-numbers {:session session}))
paid (apply hash-set (mapcat vals (db/run db/get-already-paid {:session session})))
too-much (->> summed-numbers
(filter (comp not (partial > 0.0M) :difference))
(filter (comp pos? :difference)))
too-little (->> summed-numbers
(filter (comp not (partial < 0.0M) :difference))
(filter (comp neg? :difference)))
bills (util/calculate-user-bills (db/run db/get-bills {:session session}) paid)]
(if (util/user-agent-curl? headers)
(with-out-str
(println "Items:") (pp/print-table items)
(println "Aggreated:") (pp/print-table summed-numbers)
(println "Bills:") (pp/print-table (map #(dissoc % :food) bills)))
(layout/render
"list.html" {:session session
:items items
:food_stats numbers
:summed_food_stats summed-numbers
:too_much too-much
:too_little too-little
:food_options food-options
:bills bills})))))
(defn list-tex-pdf [session]
(util/serve-pdf (io/input-stream
(tex/get-pdf (db/run db/get-food) {:session session}
(db/run db/get-food-numbers {:session session})))))
(defn add-food [args headers]
(util/modifing-request args headers
identity
valid/validate-food-request
db/add-food!))
(defn translate-add-price-request [request-map]
(let [food-name-comment (:food_name_comment request-map)
[food-name comment] (util/split-food-name-comment food-name-comment)]
(-> request-map
(assoc :food_name food-name :comment comment))))
(defn add-price [args headers]
(util/modifing-request args headers
translate-add-price-request
valid/validate-price
db/add-price!))
(defn pay [args headers]
(util/modifing-request args headers
identity
valid/validate-payment-request
db/pay!))
[compojure.coercions :refer [as-int as-uuid]]))
(def doener-list ["Döner" "Döner vegetarisch" "Dürüm" "Halbes Hänchen" "Ganzes Hänchen"])
(def grill-list ["Steak, Schwein" "Steak, Rind" "Steak, Pute" "Steak, Hänchen"
"Bratwurst, Fränkisch" "Bratwurst, Nürnberger" "Baguette"
"Brötchen" "Grillkäse Natur" "Grillkäse Chili" "Grillkäse Kräuter"])
(defn session-select []
(layout/render "session.html" {:sessions (db/run db/get-sessions)}))
(defn create-session [args]
(let [validation-errors (first (valid/validate-session-name args))]
(if validation-errors
(util/validation-error validation-errors)
(let [new-session (->> args (db/run db/create-session<!) first second)]
(found (str "/" new-session))))))
(defn delete-order [args headers]
(util/modifing-request args headers
identity
valid/validate-delete-order
db/delete-order!))
(defn edit-order [args headers]
(util/modifing-request args headers
identity
valid/validate-edit-order
db/edit-order!))
(defroutes home-routes
;; PDF Generators
(GET "/tex-pdf/:session" [session :<< as-int] (list-tex-pdf session))
(GET "/tex-pdf/:session" [session :<< as-int]
(pages/list-tex-pdf session))
;(GET "/announce-deliver" [] (irc/announce-deliver))
;; Webpages
(GET "/" []
(pages/session-select))
(GET "/:session" [session :<< as-int
:as {headers :headers}]
(list-page grill-list session headers))
(GET "/" [] (session-select))
(pages/list-page grill-list session headers))
(GET "/special/doener/:session" [session :<< as-int
:as {headers :headers}]
(list-page doener-list session headers))
(pages/list-page doener-list session headers))
(GET "/special/grill/:session" [session :<< as-int
:as {headers :headers}]
(list-page grill-list session headers))
(pages/list-page grill-list session headers))
(GET "/special/" [suggest
:as {headers :headers}]
(list-page suggest headers))
(pages/list-page suggest headers))
;; Modifying Requests
(POST "/add-food" [customer :<< str
......@@ -126,7 +41,7 @@
quantity :<< util/quantity-string-to-decimal
session :<< as-int
:as {headers :headers}]
(add-food {:customer customer
(pages/add-food {:customer customer
:food_name food_name
:comment comment
:quantity quantity
......@@ -139,7 +54,7 @@
food_name_comment :<< str
session :<< as-int
:as {headers :headers}]
(add-price {:ammount_per_package food_buy_package_size
(pages/add-price {:ammount_per_package food_buy_package_size
:number_of_packages food_buy_quantity
:package_price food_buy_packageprize
:food_name_comment food_name_comment
......@@ -147,12 +62,12 @@
headers))
(POST "/create-session" [name :<< str]
(create-session {:name name}))
(pages/create-session {:name name}))
(POST "/pay" [customer :<< str
session :<< as-int
:as {headers :headers}]
(pay {:customer customer
(pages/pay {:customer customer
:session session}
headers))
......@@ -161,7 +76,7 @@
comment :<< str
session :<< as-int
:as {headers :headers}]
(delete-order {:customer customer
(pages/delete-order {:customer customer
:food_name food_name
:comment comment
:session session}
......@@ -176,7 +91,7 @@
quantity :<< util/quantity-string-to-decimal
session :<< as-int
:as {headers :headers}]
(edit-order {:old_customer old_customer
(pages/edit-order {:old_customer old_customer
:old_food_name old_food_name
:old_comment old_comment
:customer customer
......
(ns gms.routes.pages
(:require [gms.routes.utils :as util]
[gms.layout :as layout]
[gms.tex :as tex]
[gms.db.core :as db]
[gms.validators :as valid]
[ring.util.http-response :refer [ok found bad-request]]
[clojure.set :refer [rename-keys]]
[clojure.pprint :as pp]
[clojure.java.io :as io]))
(defn list-page
([session headers] (list-page [] session headers))
([food-options session headers]
(let [items (db/run db/get-food {:session session})
numbers (util/group-numbers (db/run db/get-food-numbers {:session session}))
summed-numbers (util/add-difference (db/run db/get-summed-food-numbers {:session session}))
paid (apply hash-set (mapcat vals (db/run db/get-already-paid {:session session})))
too-much (->> summed-numbers
(filter (comp not (partial > 0.0M) :difference))
(filter (comp pos? :difference)))
too-little (->> summed-numbers
(filter (comp not (partial < 0.0M) :difference))
(filter (comp neg? :difference)))
bills (util/calculate-user-bills (db/run db/get-bills {:session session}) paid)]
(if (util/user-agent-curl? headers)
(with-out-str
(println "Items:") (pp/print-table items)
(println "Aggreated:") (pp/print-table summed-numbers)
(println "Bills:") (pp/print-table (map #(dissoc % :food) bills)))
(layout/render
"list.html" {:session session
:items items
:food_stats numbers
:summed_food_stats summed-numbers
:too_much too-much
:too_little too-little
:food_options food-options
:bills bills})))))
(defn list-tex-pdf [session]
(util/serve-pdf (io/input-stream
(tex/get-pdf (db/run db/get-food) {:session session}
(db/run db/get-food-numbers {:session session})))))
(defn add-food [args headers]
(util/modifing-request args headers
identity
valid/validate-food-request
db/add-food!))
(defn translate-add-price-request [request-map]
(let [food-name-comment (:food_name_comment request-map)
[food-name comment] (util/split-food-name-comment food-name-comment)]
(-> request-map
(assoc :food_name food-name :comment comment))))
(defn add-price [args headers]
(util/modifing-request args headers
translate-add-price-request
valid/validate-price
db/add-price!))
(defn pay [args headers]
(util/modifing-request args headers
identity
valid/validate-payment-request
db/pay!))
(defn session-select []
(layout/render "session.html" {:sessions (db/run db/get-sessions)}))
(defn create-session [args]
(let [validation-errors (first (valid/validate-session-name args))]
(if validation-errors
(util/validation-error validation-errors)
(let [new-session (->> args (db/run db/create-session<!) first second)]
(found (str "/" new-session))))))
(defn delete-order [args headers]
(util/modifing-request args headers
identity
valid/validate-delete-order
db/delete-order!))
(defn edit-order [args headers]
(util/modifing-request args headers
identity
valid/validate-edit-order
db/edit-order!))
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