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

move utility functions to gms.route.utils

parent c9ba0f0f
(ns gms.routes.home
(:import [java.io ByteArrayOutputStream]
[java.text DecimalFormat])
(:require [gms.layout :as layout]
(:require [gms.routes.utils :as util]
[gms.layout :as layout]
[gms.db.core :as db]
[gms.tex :as tex]
[gms.irc :as irc]
......@@ -9,65 +8,17 @@
[compojure.core :refer [defroutes GET POST DELETE]]
[compojure.coercions :refer [as-int as-uuid]]
[ring.util.http-response :refer [ok found bad-request]]
[ring.util.response :refer [status]]
[clojure.set :refer [rename-keys]]
[clojure.pprint :as pp]
[clojure.string :as str]
[clojure.java.io :as io]))
(defn format-money [amount]
(format "%.2f" amount))
(defn complete-sum? [bill]
(every? #(and (:single_price %) (:quantity %)) bill))
(defn calculate-total-sum [bill]
(->> bill
(map #(* (or (:single_price %) 0)
(or (:quantity %) 0)))
(reduce +)))
(defn calculate-user-bills [bills paid]
(let [grouped (group-by :customer bills)]
(filter (comp not nil?)
(map
(fn [bill-entry]
(let [customer (first bill-entry)
paid? (paid customer)
bill (second bill-entry)
food (map #(select-keys % [:food_name
:single_price
:quantity]) bill)
total-sum (calculate-total-sum bill)
total-sum-formated (if total-sum
(format-money total-sum))
complete? (complete-sum? bill)
user-bill {:customer customer
:food food
:complete complete?
:paid paid?}]
(when total-sum
(assoc user-bill :total_sum total-sum-formated))))
grouped))))
(defn user-agent-curl? [headers]
(.startsWith (get headers "user-agent" "") "curl"))
(defn group-numbers [numbers]
(map (fn [[[name comment] stats]] {:food_name name
:comment comment
:food stats})
(group-by (juxt :food_name :comment) numbers)))
(defn add-difference [summed-numbers]
(map #(assoc % :difference (- (:total_ammount %) (:number %))) summed-numbers))
(defn list-page
([session headers] (list-page [] session headers))
([food-options session headers]
(let [items (db/run db/get-food {:session session})
numbers (group-numbers (db/run db/get-food-numbers {:session session}))
summed-numbers (add-difference (db/run db/get-summed-food-numbers {: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))
......@@ -75,8 +26,8 @@
too-little (->> summed-numbers
(filter (comp not (partial < 0.0M) :difference))
(filter (comp neg? :difference)))
bills (calculate-user-bills (db/run db/get-bills {:session session}) paid)]
(if (user-agent-curl? headers)
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)
......@@ -91,81 +42,44 @@
:food_options food-options
:bills bills})))))
(defn serve-pdf [content]
{:headers {"Content-Type" "application/pdf"}
:body content})
(defn list-tex-pdf [session]
(serve-pdf (io/input-stream
(tex/get-pdf (db/run db/get-food) {:session session}
(db/run db/get-food-numbers {:session session})))))
(defn price-string-to-decimal [string]
(num (try (BigDecimal. string)
(catch NumberFormatException e -1))))
(defn quantity-string-to-decimal [string]
(num (try (BigDecimal. string)
(catch NumberFormatException e -1))))
(defn number-string-to-integer [string]
(num (try (Integer. string)
(catch NumberFormatException e -1))))
(defn validation-error [error-string]
(status (layout/render "error.html" {:error-string error-string}) 400))
(defn redirect-to-referrer [headers]
(found (or (headers "referer") "/")))
(defn modifing-request [request headers
request-transformation-fn
validataion-fn
database-action-fn]
(let [args (request-transformation-fn request)
validation-errors (first (validataion-fn args))]
(if validation-errors
(validation-error validation-errors)
(do
(db/run database-action-fn args)
(redirect-to-referrer headers)))))
(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]
(modifing-request args headers
identity
valid/validate-food-request
db/add-food!))
(defn split-food-name-comment [string]
(str/split string #"\|" 2))
(util/modifing-request args headers
identity
valid/validate-food-request
db/add-food!))
;; TODO use compojure destructuring and type coersion
(defn translate-add-price-request [request-map]
(let [food-name-comment (get-in request-map ["food_name_comment"])
[food-name comment] (split-food-name-comment food-name-comment)]
[food-name comment] (util/split-food-name-comment food-name-comment)]
(-> request-map
(dissoc "food_name_comment")
(assoc :food_name food-name :comment comment)
(rename-keys {"food_buy_package_size" :ammount_per_package
"food_buy_quantity" :number_of_packages
"food_buy_packageprize" :package_price})
(update-in [:ammount_per_package] quantity-string-to-decimal)
(update-in [:package_price] price-string-to-decimal)
(update-in [:number_of_packages] number-string-to-integer))))
(update-in [:ammount_per_package] util/quantity-string-to-decimal)
(update-in [:package_price] util/price-string-to-decimal)
(update-in [:number_of_packages] util/number-string-to-integer))))
(defn add-price [request]
(modifing-request (:form-params request) (:headers request)
#(translate-add-price-request
(assoc % :session
(Integer/parseInt (% "session"))))
valid/validate-price
db/add-price!))
(util/modifing-request (:form-params request) (:headers request)
#(translate-add-price-request
(assoc % :session
(Integer/parseInt (% "session"))))
valid/validate-price
db/add-price!))
(defn pay [args headers]
(modifing-request args headers
identity
valid/validate-payment-request
db/pay!))
(util/modifing-request args headers
identity
valid/validate-payment-request
db/pay!))
(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"
......@@ -178,21 +92,21 @@
(defn create-session [args]
(let [validation-errors (first (valid/validate-session-name args))]
(if validation-errors
(validation-error 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]
(modifing-request args headers
identity
valid/validate-delete-order
db/delete-order!))
(util/modifing-request args headers
identity
valid/validate-delete-order
db/delete-order!))
(defn edit-order [args headers]
(modifing-request args headers
identity
valid/validate-edit-order
db/edit-order!))
(util/modifing-request args headers
identity
valid/validate-edit-order
db/edit-order!))
(defroutes home-routes
;; PDF Generators
......@@ -219,7 +133,7 @@
(POST "/add-food" [customer :<< str
food_name :<< str
comment :<< str
quantity :<< quantity-string-to-decimal
quantity :<< util/quantity-string-to-decimal
session :<< as-int
:as {headers :headers}]
(add-food {:customer customer
......@@ -258,7 +172,7 @@
customer :<< str
food_name :<< str
comment :<< str
quantity :<< quantity-string-to-decimal
quantity :<< util/quantity-string-to-decimal
session :<< as-int
:as {headers :headers}]
(edit-order {:old_customer old_customer
......
(ns gms.routes.utils
(:import [java.io ByteArrayOutputStream]
[java.text DecimalFormat])
(:require [gms.layout :as layout]
[gms.db.core :as db]
[clojure.string :as str]
[ring.util.http-response :refer [ok found bad-request]]
[ring.util.response :refer [status]]))
(defn price-string-to-decimal [string]
(num (try (BigDecimal. string)
(catch NumberFormatException e -1))))
(defn quantity-string-to-decimal [string]
(num (try (BigDecimal. string)
(catch NumberFormatException e -1))))
(defn number-string-to-integer [string]
(num (try (Integer. string)
(catch NumberFormatException e -1))))
(defn format-money [amount]
(format "%.2f" amount))
(defn complete-sum? [bill]
(every? #(and (:single_price %) (:quantity %)) bill))
(defn calculate-total-sum [bill]
(->> bill
(map #(* (or (:single_price %) 0)
(or (:quantity %) 0)))
(reduce +)))
(defn split-food-name-comment [string]
(str/split string #"\|" 2))
(defn calculate-user-bills [bills paid]
(let [grouped (group-by :customer bills)]
(filter (comp not nil?)
(map
(fn [bill-entry]
(let [customer (first bill-entry)
paid? (paid customer)
bill (second bill-entry)
food (map #(select-keys % [:food_name
:single_price
:quantity]) bill)
total-sum (calculate-total-sum bill)
total-sum-formated (if total-sum
(format-money total-sum))
complete? (complete-sum? bill)
user-bill {:customer customer
:food food
:complete complete?
:paid paid?}]
(when total-sum
(assoc user-bill :total_sum total-sum-formated))))
grouped))))
(defn group-numbers [numbers]
(map (fn [[[name comment] stats]] {:food_name name
:comment comment
:food stats})
(group-by (juxt :food_name :comment) numbers)))
(defn add-difference [summed-numbers]
(map #(assoc % :difference (- (:total_ammount %) (:number %))) summed-numbers))
(defn user-agent-curl? [headers]
(.startsWith (get headers "user-agent" "") "curl"))
(defn serve-pdf [content]
{:headers {"Content-Type" "application/pdf"}
:body content})
(defn validation-error [error-string]
(status (layout/render "error.html" {:error-string error-string}) 400))
(defn redirect-to-referrer [headers]
(found (or (headers "referer") "/")))
(defn modifing-request [request headers
request-transformation-fn
validataion-fn
database-action-fn]
(let [args (request-transformation-fn request)
validation-errors (first (validataion-fn args))]
(if validation-errors
(validation-error validation-errors)
(do
(db/run database-action-fn args)
(redirect-to-referrer headers)))))
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