Send back the total price to the client
This commit is contained in:
parent
221c3ddc04
commit
2a7763b2d0
|
@ -7,16 +7,19 @@
|
||||||
(define port 4444)
|
(define port 4444)
|
||||||
(define hostname "127.0.0.1")
|
(define hostname "127.0.0.1")
|
||||||
|
|
||||||
|
; Send a json-expr on the output, prepending it with 4 bytes that contain the JSON length
|
||||||
|
(define (send-json json-expr-to-send output)
|
||||||
|
(write-bytes (integer->integer-bytes (bytes-length (jsexpr->bytes json-expr-to-send)) 4 #f) output)
|
||||||
|
(write-json json-expr-to-send output))
|
||||||
|
|
||||||
; Return a list of all the pizzas from the local JSON file
|
; Return a list of all the pizzas from the local JSON file
|
||||||
(define (get-all-pizzas)
|
(define (get-all-pizzas)
|
||||||
(let ((file (open-input-file "pizzas_list.json")))
|
(let ((file (open-input-file "pizzas_list.json")))
|
||||||
(read-json file)))
|
(read-json file)))
|
||||||
|
|
||||||
; Send the list of all pizzas as a JSON, prepending it with 4 bytes that contain the list length
|
; Send the list of all pizzas as a JSON
|
||||||
(define (send-pizzas-list output)
|
(define (send-pizzas-list output)
|
||||||
(let ((all-pizzas-list (get-all-pizzas)))
|
(send-json (get-all-pizzas) output))
|
||||||
(write-bytes (integer->integer-bytes (bytes-length (jsexpr->bytes all-pizzas-list)) 4 #f) output)
|
|
||||||
(write-json all-pizzas-list output)))
|
|
||||||
|
|
||||||
; Print the to-print string on the printer
|
; Print the to-print string on the printer
|
||||||
; TODO add synchronization to avoid race conditions
|
; TODO add synchronization to avoid race conditions
|
||||||
|
@ -37,23 +40,36 @@
|
||||||
(* found-price (hash-ref pizza 'quantity))))
|
(* found-price (hash-ref pizza 'quantity))))
|
||||||
|
|
||||||
; Format the pizzas list coming from the client in a human-readable way
|
; Format the pizzas list coming from the client in a human-readable way
|
||||||
(define (format-pizza pizzas-list)
|
(define (format-pizza pizzas-list price)
|
||||||
(let ((str "")
|
(let ((str ""))
|
||||||
(price 0)
|
(for-each (lambda (pizza-hashmap)
|
||||||
|
(set! str (string-append str (format "~a : ~s\n" (hash-ref pizza-hashmap 'quantity) (hash-ref pizza-hashmap 'name)))))
|
||||||
|
pizzas-list)
|
||||||
|
(set! str (string-append str (format "Total price: ~a\n" price)))
|
||||||
|
str))
|
||||||
|
|
||||||
|
; Get the price of each type of pizza and calculate the total price
|
||||||
|
(define (get-total-price pizzas-list)
|
||||||
|
(let ((price 0)
|
||||||
(all-pizzas-list (hash-ref (get-all-pizzas) 'pizzas)))
|
(all-pizzas-list (hash-ref (get-all-pizzas) 'pizzas)))
|
||||||
(for-each (lambda (pizza-hashmap)
|
(for-each (lambda (pizza-hashmap)
|
||||||
(set! str (string-append str (format "~a : ~s\n" (hash-ref pizza-hashmap 'quantity) (hash-ref pizza-hashmap 'name))))
|
|
||||||
(set! price (+ price (get-price-from-pizza pizza-hashmap all-pizzas-list))))
|
(set! price (+ price (get-price-from-pizza pizza-hashmap all-pizzas-list))))
|
||||||
pizzas-list)
|
pizzas-list)
|
||||||
(set! str (string-append str (format "Total price ~a" price)))
|
price))
|
||||||
str))
|
|
||||||
|
; Create a format string with the received pizza list, calculate the
|
||||||
|
; price of the whole order, print them and send it back to the client
|
||||||
|
(define (print-pizzas-and-send-price pizzas-list output)
|
||||||
|
(let* ((total-price (get-total-price pizzas-list)))
|
||||||
|
(printer-print (format-pizza pizzas-list total-price))
|
||||||
|
(send-json (hash 'total_price total-price) output)))
|
||||||
|
|
||||||
; Parse the commands given in the JSON
|
; Parse the commands given in the JSON
|
||||||
(define (parse-json message output)
|
(define (parse-json message output)
|
||||||
(let ((parsed-json (with-input-from-string message (lambda () (read-json))))) ; parse the JSON
|
(let ((parsed-json (with-input-from-string message (lambda () (read-json))))) ; parse the JSON
|
||||||
(case (hash-ref parsed-json 'command)
|
(case (hash-ref parsed-json 'command)
|
||||||
(("print") (printer-print (hash-ref parsed-json 'text)))
|
(("print") (printer-print (hash-ref parsed-json 'text)))
|
||||||
(("pizza") (printer-print (format-pizza (hash-ref parsed-json 'pizzas))))
|
(("pizza") (print-pizzas-and-send-price (hash-ref parsed-json 'pizzas) output))
|
||||||
(("get-pizzas") (send-pizzas-list output))
|
(("get-pizzas") (send-pizzas-list output))
|
||||||
(else (displayln "Unknown command")))))
|
(else (displayln "Unknown command")))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user