Get the total price from the API
This commit is contained in:
parent
bc26e2029a
commit
f74ce8de7a
6
api.go
6
api.go
|
@ -33,13 +33,17 @@ type GetPizzasCommand struct {
|
||||||
|
|
||||||
// What to receive from the API
|
// What to receive from the API
|
||||||
type JSONReceived interface {
|
type JSONReceived interface {
|
||||||
ReceivePizzas
|
ReceivePizzas | ReceivePrice
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReceivePizzas struct {
|
type ReceivePizzas struct {
|
||||||
Pizzas []Pizza `json:"pizzas"`
|
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
|
* 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
|
* the length of the message, followed by the message itself
|
||||||
|
|
26
main.go
26
main.go
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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 {
|
type Pizza struct {
|
||||||
|
@ -53,7 +53,8 @@ func printHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
/**
|
/**
|
||||||
* Handler function for "/pizza", only accepts POST requests
|
* 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) {
|
func pizzaHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
|
@ -73,14 +74,31 @@ func pizzaHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
json_command := PizzaCommand{Command: "pizza", Pizzas: pizzas}
|
json_command := PizzaCommand{Command: "pizza", Pizzas: pizzas}
|
||||||
_, err := sendJSONToServer(nil, json_command)
|
socket, err := sendJSONToServer(nil, json_command)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(w, "Error while connecting to the printer server")
|
fmt.Fprintf(w, "Error while connecting to the printer server")
|
||||||
return
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
38
templates/ordered.html
Normal file
38
templates/ordered.html
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<title>Scontrini</title>
|
||||||
|
<link rel="stylesheet" href="">
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>ORDINE</h1>
|
||||||
|
<h3>Il tuo ordine è stato stampato</h3>
|
||||||
|
|
||||||
|
<table id="pizzas">
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
Nome
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Quantità
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{ range .Pizzas }}
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
{{ .Name }}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ .Quantity }}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{ end }}
|
||||||
|
</table>
|
||||||
|
<p id="total_price">Prezzo totale: {{ .TotalPrice }}</p>
|
||||||
|
<button><a href="/">Stampa qualcosa</a></button>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
|
@ -11,6 +11,17 @@
|
||||||
|
|
||||||
<form action="/pizza" method="post">
|
<form action="/pizza" method="post">
|
||||||
<table id="pizzas">
|
<table id="pizzas">
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
Nome
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Prezzo
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
Quantità
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
{{ range . }}
|
{{ range . }}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user