77 lines
1.7 KiB
Go
77 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
type Pizza struct {
|
|
Name string `json:"name"`
|
|
Price int `json:"price"`
|
|
Quantity int `json:"quantity"`
|
|
}
|
|
|
|
/**
|
|
* Send and Receive messages from the API
|
|
*/
|
|
|
|
func SendPrintCommand(socket *net.TCPConn, text string) (*net.TCPConn, error) {
|
|
json_command := PrintCommand{Command: "print", Text: text}
|
|
socket, err := sendJSONToServer(socket, json_command)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error while connecting to the printer server")
|
|
return nil, err
|
|
}
|
|
|
|
return socket, nil
|
|
}
|
|
|
|
func SendPizzaCommand(socket *net.TCPConn, pizzas []Pizza) (*net.TCPConn, error) {
|
|
json_command := PizzaCommand{Command: "pizza", Pizzas: pizzas}
|
|
socket, err := sendJSONToServer(nil, json_command)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error while connecting to the printer server")
|
|
return nil, err
|
|
}
|
|
|
|
return socket, nil
|
|
}
|
|
|
|
func SendGetPizzasCommand(socket *net.TCPConn) (*net.TCPConn, error) {
|
|
json_command := GetPizzasCommand{Command: "get-pizzas"}
|
|
socket, err := sendJSONToServer(nil, json_command)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error while connecting to the printer server")
|
|
return nil, err
|
|
}
|
|
|
|
return socket, nil
|
|
}
|
|
|
|
func ReceivePizzasCommand(socket *net.TCPConn) (*net.TCPConn, ReceivePizzas, error) {
|
|
pizzas := ReceivePizzas{}
|
|
socket, err := receiveJSONFromServer(socket, &pizzas)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error while connecting to the printer server")
|
|
return nil, pizzas, err
|
|
}
|
|
|
|
return socket, pizzas, nil
|
|
}
|
|
|
|
func ReceivePriceCommand(socket *net.TCPConn) (*net.TCPConn, ReceivePrice, error) {
|
|
price := ReceivePrice{}
|
|
socket, err := receiveJSONFromServer(socket, &price)
|
|
|
|
if err != nil {
|
|
fmt.Println("Error while connecting to the printer server")
|
|
return nil, price, err
|
|
}
|
|
|
|
return socket, price, nil
|
|
}
|