forked from Yandex-Practicum/go-testify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecode.go
More file actions
45 lines (36 loc) · 895 Bytes
/
precode.go
File metadata and controls
45 lines (36 loc) · 895 Bytes
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
package main
import (
"net/http"
"strconv"
"strings"
)
var cafeList = map[string][]string{
"moscow": []string{"Мир кофе", "Сладкоежка", "Кофе и завтраки", "Сытый студент"},
}
func mainHandle(w http.ResponseWriter, req *http.Request) {
countStr := req.URL.Query().Get("count")
if countStr == "" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("count missing"))
return
}
count, err := strconv.Atoi(countStr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("wrong count value"))
return
}
city := req.URL.Query().Get("city")
cafe, ok := cafeList[city]
if !ok {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("wrong city value"))
return
}
if count > len(cafe) {
count = len(cafe)
}
answer := strings.Join(cafe[:count], ",")
w.WriteHeader(http.StatusOK)
w.Write([]byte(answer))
}