-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.go
171 lines (141 loc) · 3.67 KB
/
menu.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"math"
"strconv"
"strings"
"time"
)
const (
gr_lunch = "Μεσημεριανό"
gr_dinner = "Βραδινό"
gr_first = "ΠΡΩΤΟ ΠΙΑΤΟ"
gr_main = "ΚΥΡΙΩΣ ΠΙΑΤΑ"
gr_salad = "ΣΑΛΑΤΑ"
gr_bread = "ΨΩΜΙ"
gr_dessert = "ΓΛΥΚΟ"
gr_fruit = "ΦΡΟΥΤΟ"
)
type MenuArray []string
func (m MenuArray) findIndexWhereContains(value string) int {
for i, v := range m {
if strings.Contains(v, value) {
return i
}
}
return -1
}
type Menu struct {
First []string `json:"first"`
Main []string `json:"main"`
Salad []string `json:"salad"`
Bread []string `json:"bread"`
Dessert []string `json:"dessert"`
Fruit bool `json:"fruit"`
}
func (m Menu) description() string {
desc := ""
contain := [4][]string{m.First, m.Salad, m.Main, m.Dessert}
for _, d := range contain {
for i, v := range d {
if i == 0 {
desc += "- " + v
} else {
desc += " " + v
}
desc += "\n"
}
}
return strings.Trim(desc, "\n")
}
func getMaxInt(values ...int) int {
max := math.MinInt
for _, v := range values {
if v > max {
max = v
}
}
return max
}
func NewMenuFromArray(array MenuArray) *Menu {
menu := new(Menu)
firstIndex := array.findIndexWhereContains(gr_first)
mainIndex := array.findIndexWhereContains(gr_main)
saladIndex := array.findIndexWhereContains(gr_salad)
breadIndex := array.findIndexWhereContains(gr_bread)
dessertIndex := array.findIndexWhereContains(gr_dessert)
fruitIndex := array.findIndexWhereContains(gr_fruit)
lastIndex := getMaxInt(dessertIndex, fruitIndex)
menu.First = array[firstIndex+1 : mainIndex]
menu.Main = array[mainIndex+1 : saladIndex]
menu.Salad = array[saladIndex+1 : breadIndex]
menu.Bread = array[breadIndex+1 : lastIndex]
menu.Dessert = []string{}
menu.Fruit = false
if dessertIndex != -1 {
menu.Dessert = array[dessertIndex+1:]
}
if fruitIndex != -1 {
menu.Fruit = true
}
return menu
}
type DayMenu struct {
Date time.Time `json:"date"`
Lunch Menu `json:"lunch"`
Dinner Menu `json:"dinner"`
}
func (dm DayMenu) description() map[string]interface{} {
return map[string]interface{}{
"lunch": dm.Lunch.description(),
"dinner": dm.Dinner.description(),
"date": dm.Date,
}
}
func NewDayMenuFromArray(array MenuArray) *DayMenu {
date := array[0]
dateSplit := strings.Split(date, " ")
date = dateSplit[len(dateSplit)-1]
dateSplit = strings.Split(date, "/")
year, _ := strconv.Atoi(dateSplit[2])
month, _ := strconv.Atoi(dateSplit[1])
day, _ := strconv.Atoi(dateSplit[0])
newDate := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
lunchIndex := array.findIndexWhereContains(gr_lunch)
dinnerIndex := array.findIndexWhereContains(gr_dinner)
lunchArray := array[lunchIndex+1 : dinnerIndex]
lunchMenu := NewMenuFromArray(lunchArray)
dinner := array[dinnerIndex+1:]
dinnerMenu := NewMenuFromArray(dinner)
dayMenu := new(DayMenu)
dayMenu.Date = newDate
dayMenu.Lunch = *lunchMenu
dayMenu.Dinner = *dinnerMenu
return dayMenu
}
type WeekMenu struct {
Menus [7]DayMenu `json:"menus"`
UpdatedAt time.Time `json:"updated_at"`
}
func NewWeekMenuFromWeb() (*WeekMenu, bool) {
var weekArrays [7]MenuArray = getMenuFromWeb()
if len(weekArrays[0]) == 0 {
return nil, false
}
var weekMenu = new(WeekMenu)
weekMenu.UpdatedAt = time.Now()
for i, day := range weekArrays {
weekMenu.Menus[i] = *NewDayMenuFromArray(day)
}
return weekMenu, true
}
func (w WeekMenu) menuOfWeekday(weekDay Weekday) DayMenu {
return w.Menus[weekDay-1]
}
func (w WeekMenu) menuOfDate(date time.Time) (DayMenu, bool) {
for _, v := range w.Menus {
if isSameDay(v.Date, date) {
return v, true
}
}
return DayMenu{}, false
}