diff --git a/api.go b/api.go index 16c30c0..8ee2e93 100644 --- a/api.go +++ b/api.go @@ -33,13 +33,17 @@ type GetPizzasCommand struct { // What to receive from the API type JSONReceived interface { - ReceivePizzas + ReceivePizzas | ReceivePrice } type ReceivePizzas struct { Pizzas []Pizza `json:"pizzas"` } +type ReceivePrice struct { + TotalPrice int `json:"total_price"` +} + /** * All messages from and to the API must have the first 4 bytes set to represent * the length of the message, followed by the message itself diff --git a/main.go b/main.go index 6549e82..f96658b 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( ) var ( - templates = template.Must(template.ParseFiles("templates/index.html", "templates/pizza.html")) + templates = template.Must(template.ParseFiles("templates/index.html", "templates/pizza.html", "templates/ordered.html")) ) type Pizza struct { @@ -53,7 +53,8 @@ func printHandler(w http.ResponseWriter, r *http.Request) { /** * Handler function for "/pizza", only accepts POST requests * -* Tells the API to print the pizza order specified in its Form +* Tells the API to print the pizza order specified in its Form, then receive +* back the correct price and return a formatted HTML page */ func pizzaHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { @@ -73,14 +74,31 @@ func pizzaHandler(w http.ResponseWriter, r *http.Request) { } json_command := PizzaCommand{Command: "pizza", Pizzas: pizzas} - _, err := sendJSONToServer(nil, json_command) + socket, err := sendJSONToServer(nil, json_command) if err != nil { fmt.Fprintf(w, "Error while connecting to the printer server") return } - http.Redirect(w, r, "/order_pizzas", http.StatusSeeOther) + price := ReceivePrice{} + _, err = receiveJSONFromServer(socket, &price) + + if err != nil { + fmt.Fprintf(w, "Error while connecting to the printer server") + return + } + + // pass the order data to the HTML + order := struct { + Pizzas []Pizza + TotalPrice int + }{ + pizzas, + price.TotalPrice, + } + + templates.ExecuteTemplate(w, "ordered.html", order) } /** diff --git a/templates/ordered.html b/templates/ordered.html new file mode 100644 index 0000000..38f224e --- /dev/null +++ b/templates/ordered.html @@ -0,0 +1,38 @@ + + + + + Scontrini + + + + +

ORDINE

+

Il tuo ordine è stato stampato

+ + + + + + + {{ range .Pizzas }} + + + + + {{ end }} +
+ Nome + + Quantità +
+ {{ .Name }} + + {{ .Quantity }} +
+

Prezzo totale: {{ .TotalPrice }}

+ + + + + diff --git a/templates/pizza.html b/templates/pizza.html index 6f1c538..36569af 100644 --- a/templates/pizza.html +++ b/templates/pizza.html @@ -11,6 +11,17 @@
+ + + + + {{ range . }}
+ Nome + + Prezzo + + Quantità +