Initial working version

This commit is contained in:
zv0n 2022-10-29 00:54:59 +02:00
parent 11d69abd9f
commit 6ce3fcea0b
9 changed files with 569 additions and 0 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@
# Go workspace file
go.work
lunch-go

87
cmd/lunch-go/main.go Normal file
View File

@ -0,0 +1,87 @@
package main
import (
"fmt"
"net/http"
r "git.zvon.tech/zv0n/lunch-go/pkg/restaurants"
"github.com/gin-gonic/gin"
)
var restaurants []r.RestaurantInterface
func getAll(c *gin.Context) {
c.IndentedJSON(http.StatusOK, restaurants)
}
func dayToIndex(day string) int {
if day == "monday" {
return 0
} else if day == "tuesday" {
return 1
} else if day == "wednesday" {
return 2
} else if day == "thursday" {
return 3
} else if day == "friday" {
return 4
} else if day == "saturday" {
return 5
}
return 6
}
func get(c *gin.Context) {
day := dayToIndex(c.Query("day"))
var responseObjects []r.RestaurantJSON
for _, restaurant := range restaurants {
obj := restaurant.GetSpecificDayObject([]int{day})
responseObjects = append(responseObjects, obj)
}
c.IndentedJSON(http.StatusOK, responseObjects)
}
func refreshInternal() {
for _, restaurant := range restaurants {
restaurant.Parse()
}
}
func refresh(c *gin.Context) {
refreshInternal()
c.Status(http.StatusOK)
}
func main() {
fmt.Println("Hello, World!")
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2752-u-drevaka-beergrill.html", "U Dřeváka"))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/4116-padagali.html", "Padagali"))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/5448-light-of-india.html", "Light of India"))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/2609-pizzeria-al-capone.html", "Al Capone"))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/3830-suzies-steak-pub.html", "Suzie's"))
restaurants = append(restaurants, r.NewMenickaRestaurant("https://www.menicka.cz/6468-diva-bara.html", "Divá Bára"))
refreshInternal()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.GET("/get_all", getAll)
router.GET("/get", get)
router.GET("/refresh", refresh)
router.Run("localhost:8080")
/*
test := restaurants.MakeMenickaRestaurant("https://www.menicka.cz/2752-u-drevaka-beergrill.html", "U Dřeváka")
test.Parse()
for _, menu := range test.GetMenus() {
for _, meal := range menu.GetMeals() {
if meal.IsSoup() {
fmt.Print("Soup: ")
} else {
fmt.Print("Meal: ")
}
fmt.Println(meal.GetName(), "; ", meal.GetPrice(), "Kč")
}
}
*/
}

28
go.mod Normal file
View File

@ -0,0 +1,28 @@
module git.zvon.tech/zv0n/lunch-go
go 1.19
require (
github.com/gin-gonic/gin v1.8.1
golang.org/x/text v0.4.0
)
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.10.0 // indirect
github.com/goccy/go-json v0.9.7 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

78
go.sum Normal file
View File

@ -0,0 +1,78 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

76
pkg/restaurants/html.go Normal file
View File

@ -0,0 +1,76 @@
package restaurants
import (
"errors"
"golang.org/x/net/html"
"golang.org/x/text/encoding/charmap"
)
func getAttribute(node *html.Node, key string) (string, error) {
for _, attr := range node.Attr {
if attr.Key == key {
return attr.Val, nil
}
}
return "", errors.New("couldn't find the provided key")
}
func hasClass(node *html.Node, class string) bool {
if node.Type == html.ElementNode {
c, err := getAttribute(node, "class")
if err != nil {
return false
}
return c == class
}
return false
}
func findNodeByClass(node *html.Node, class string) (*html.Node, error) {
if hasClass(node, class) {
return node, nil
}
for n := node.FirstChild; n != nil; n = n.NextSibling {
c, err := findNodeByClass(n, class)
if err == nil {
return c, nil
}
}
return nil, errors.New("couldn't find a node with provided class")
}
func getTextInternal(node *html.Node) (string, error) {
if node.Type == html.TextNode {
return node.Data, nil
}
return "", errors.New("not a text node")
}
func getText(node *html.Node) (string, error) {
if node.Type == html.TextNode {
return node.Data, nil
}
for n := node.FirstChild; n != nil; n = n.NextSibling {
text, err := getTextInternal(n)
if err == nil {
return text, nil
}
}
return "", errors.New("couldn't find a text node")
}
func getTextDecodeWindows1250(node *html.Node) (string, error) {
text, err := getText(node)
if err != nil {
return text, err
}
return decodeWindows1250(text)
}
func decodeWindows1250(text string) (string, error) {
dec := charmap.Windows1250.NewDecoder()
out, err := dec.String(text)
return out, err
}

60
pkg/restaurants/meal.go Normal file
View File

@ -0,0 +1,60 @@
package restaurants
import "encoding/json"
type Meal struct {
isSoup bool
name string
desc string
price int
}
func MakeMeal(isSoup bool, name string, desc string, price int) Meal {
return Meal{isSoup, name, desc, price}
}
func (meal Meal) IsSoup() bool {
return meal.isSoup
}
func (meal Meal) GetName() string {
return meal.name
}
func (meal Meal) GetDescription() string {
return meal.desc
}
func (meal Meal) GetPrice() int {
return meal.price
}
func (meal *Meal) SetSoup(isSoup bool) {
meal.isSoup = isSoup
}
func (meal *Meal) SetName(name string) {
meal.name = name
}
func (meal *Meal) SetDescription(desc string) {
meal.desc = desc
}
func (meal *Meal) SetPrice(price int) {
meal.price = price
}
func (meal *Meal) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Name string `json:"name"`
Description string `json:"description"`
IsSoup bool `json:"isSoup"`
Price int `json:"price"`
}{
Name: meal.name,
Description: meal.desc,
IsSoup: meal.isSoup,
Price: meal.price,
})
}

118
pkg/restaurants/menicka.go Normal file
View File

@ -0,0 +1,118 @@
package restaurants
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"golang.org/x/net/html"
)
type MenickaRestaurant struct {
Restaurant
}
func MakeMenickaRestaurant(url string, name string) MenickaRestaurant {
restaurant := MenickaRestaurant{}
restaurant.url = url
restaurant.name = name
return restaurant
}
func NewMenickaRestaurant(url string, name string) *MenickaRestaurant {
restaurant := new(MenickaRestaurant)
restaurant.url = url
restaurant.name = name
return restaurant
}
func dayToIndex(day string) (int, error) {
if day == "Pondělí" {
return 0, nil
} else if day == "Úterý" {
return 1, nil
} else if day == "Středa" {
return 2, nil
} else if day == "Čtvrtek" {
return 3, nil
} else if day == "Pátek" {
return 4, nil
} else if day == "Sobota" {
return 5, nil
} else if day == "Neděle" {
return 6, nil
}
return -1, errors.New("couldn't parse the day")
}
func (restaurant *MenickaRestaurant) Parse() {
restaurant.clearMenus()
resp, err := http.Get(restaurant.url)
if err != nil {
return
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
fmt.Println(err)
return
}
content, err := findNodeByClass(doc, "obsah")
if err != nil {
fmt.Printf("Couldn't find content for restaurant \"%s\"\n", restaurant.name)
return
}
for menu := content.FirstChild; menu != nil; menu = menu.NextSibling {
if hasClass(menu, "menicka") {
day, err := findNodeByClass(menu, "nadpis")
if err != nil {
continue
}
meals, err := findNodeByClass(menu, "popup-gallery")
if err != nil {
continue
}
dayText, err := getTextDecodeWindows1250(day)
if err != nil {
continue
}
dayText = strings.Split(dayText, " ")[0]
dayIndex, err := dayToIndex(dayText)
if err != nil {
continue
}
for meal := meals.FirstChild; meal != nil; meal = meal.NextSibling {
nameNode, err := findNodeByClass(meal, "polozka")
if err != nil {
continue
}
name, err := getTextDecodeWindows1250(nameNode)
if err != nil {
continue
}
price := -1
priceNode, err := findNodeByClass(meal, "cena")
if err == nil {
priceStr, err := getText(priceNode)
if err != nil {
continue
}
price, err = strconv.Atoi(strings.Split(priceStr, " ")[0])
if err != nil {
price = -1
}
}
if hasClass(meal, "polevka") {
restaurant.menus[dayIndex].Add(true, strings.TrimSpace(name), "", price)
} else {
restaurant.menus[dayIndex].Add(false, strings.TrimSpace(name), "", price)
}
}
}
}
}

55
pkg/restaurants/menu.go Normal file
View File

@ -0,0 +1,55 @@
package restaurants
import "encoding/json"
type Menu struct {
meals []Meal
valid bool
day string
}
func MakeMenu(meals []Meal, day string) Menu {
return Menu{meals, true, day}
}
func (menu *Menu) Add(isSoup bool, name string, desc string, price int) {
menu.AddMeal(MakeMeal(isSoup, name, desc, price))
}
func (menu *Menu) AddMeal(meal Meal) {
menu.meals = append(menu.meals, meal)
}
func (menu Menu) GetMeals() []Meal {
return menu.meals
}
func (menu *Menu) SetInvalidMenu(invalid bool) {
menu.valid = !invalid
}
func (menu *Menu) SetValidMenu(valid bool) {
menu.valid = valid
}
func (menu Menu) IsValid() bool {
return menu.valid
}
func (menu *Menu) SetDay(day string) {
menu.day = day
}
func (menu Menu) GetDay() string {
return menu.day
}
func (menu *Menu) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Meals []Meal `json:"meals"`
Day string `json:"day"`
}{
Meals: menu.meals,
Day: menu.day,
})
}

View File

@ -0,0 +1,66 @@
package restaurants
import "encoding/json"
type RestaurantInterface interface {
// public
GetMenus() [7]Menu
Parse()
AddPermanentMeal(meal Meal)
MarshalJSON() ([]byte, error)
GetSpecificDayObject(days []int) RestaurantJSON
// private
clearMenus()
}
type Restaurant struct {
RestaurantInterface
url string
name string
menus [7]Menu
permanent []Meal
}
type RestaurantJSON struct {
Restaurant string `json:"restaurant"`
DailyMenus []Menu `json:"dailymenus"`
PermanentMeals []Meal `json:"permanentmeals"`
}
func (restaurant *Restaurant) AddPermanent(isSoup bool, name string, desc string, price int) {
restaurant.AddPermanentMeal(MakeMeal(isSoup, name, desc, price))
}
func (restaurant *Restaurant) AddPermanentMeal(meal Meal) {
restaurant.permanent = append(restaurant.permanent, meal)
}
func (restaurant Restaurant) GetMenus() [7]Menu {
return restaurant.menus
}
func (restaurant *Restaurant) clearMenus() {
for i := 0; i < 7; i++ {
restaurant.menus[i] = Menu{}
}
}
func (restaurant *Restaurant) MarshalJSON() ([]byte, error) {
return json.Marshal(&RestaurantJSON{
Restaurant: restaurant.name,
DailyMenus: restaurant.menus[:],
PermanentMeals: restaurant.permanent,
})
}
func (restaurant *Restaurant) GetSpecificDayObject(days []int) RestaurantJSON {
obj := RestaurantJSON{
Restaurant: restaurant.name,
PermanentMeals: restaurant.permanent,
}
for _, index := range days {
obj.DailyMenus = append(obj.DailyMenus, restaurant.menus[index])
}
return obj
}