-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (150 loc) · 4.14 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"embed"
"encoding/csv"
"fmt"
"mime/multipart"
"sort"
"strings"
"github.com/antosdaniel/seats-org/pkg/organize"
"github.com/antosdaniel/seats-org/pkg/seat_layouts"
"github.com/antosdaniel/seats-org/views/layout"
"github.com/benbjohnson/hashfs"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
//go:embed assets
var embedFS embed.FS
var assetsFS = hashfs.NewFS(embedFS)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.StaticFS("/", assetsFS)
e.GET("/", func(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
keys := []string{}
for name := range seat_layouts.Presets {
keys = append(keys, name)
}
sort.Strings(keys)
layouts := make([]layout.SeatLayout, len(seat_layouts.Presets))
for i, name := range keys {
layouts[i] = layout.NewSeatLayout(name, seat_layouts.Presets[name])
}
return layout.Base(assetsFS.HashName, layouts).
Render(c.Request().Context(), c.Response())
})
e.POST("/organize", func(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
file, err := c.FormFile("passengers")
if err != nil {
return fmt.Errorf("file not provided: %w", err)
}
l, err := seat_layouts.Get(c.FormValue("layout"))
if err != nil {
return err
}
passengers, err := readListOfPassengers(file)
if err != nil {
return fmt.Errorf("could not read list of passengers: %w", err)
}
result, err := organize.Organize(l, passengers)
if err != nil {
return err
}
return layout.OrganizeResult(result).Render(c.Request().Context(), c.Response())
})
e.GET("/preview", func(c echo.Context) error {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTML)
name := c.QueryParam("layout")
l, err := seat_layouts.Get(name)
if err != nil {
_, _ = c.Response().Write([]byte(err.Error()))
return nil
}
selected := layout.NewSeatLayout(name, l)
_, _ = c.Response().Write([]byte(selected.Visual))
return nil
})
e.Logger.Fatal(e.Start(":8000"))
}
func readListOfPassengers(file *multipart.FileHeader) (organize.Passengers, error) {
src, err := file.Open()
if err != nil {
return nil, fmt.Errorf("could not open file: %w", err)
}
defer src.Close()
records, err := csv.NewReader(src).ReadAll()
if err != nil {
return nil, fmt.Errorf("could not read CSV: %w", err)
}
if len(records) < 1 {
return nil, fmt.Errorf("CSV has no rows")
}
idIndex := -1
fullNameIndex := -1
preferencesIndex := -1
for col := range records[0] {
header := strings.TrimSpace(strings.ToLower(records[0][col]))
if header == "id" {
idIndex = col
continue
}
if strings.Contains(header, "nazwisko") {
fullNameIndex = col
continue
}
if strings.Contains(header, "preferencje") {
preferencesIndex = col
continue
}
}
if idIndex < 0 {
return nil, fmt.Errorf("ID column not found")
}
if fullNameIndex < 0 {
return nil, fmt.Errorf("full name column not found")
}
if preferencesIndex < 0 {
return nil, fmt.Errorf("preferences column not found")
}
result := organize.Passengers{}
for i := 1; i < len(records); i++ {
row := records[i]
id := organize.PassengerId(row[idIndex])
if id == "" {
continue
}
preferences, err := parsePreferences(row[preferencesIndex])
if err != nil {
return nil, err
}
passenger := organize.NewPassenger(id, i, preferences...)
passenger.Details.FullName = row[fullNameIndex]
result = append(result, passenger)
}
return result, nil
}
func parsePreferences(in string) (organize.Preferences, error) {
if in == "" {
return nil, nil
}
result := organize.Preferences{}
for _, i := range strings.Split(in, ",") {
p := strings.TrimSpace(strings.ToLower(i))
switch p {
case "okno":
result = append(result, organize.WindowSeatPreference)
case "przejście":
result = append(result, organize.AisleSeatPreference)
case "przód":
result = append(result, organize.FrontSeatPreference)
case "tył":
result = append(result, organize.RearSeatPreference)
default:
return nil, fmt.Errorf("unknown preference %q", p)
}
}
// TODO: validate that preferences can be mixed
return result, nil
}