-
Notifications
You must be signed in to change notification settings - Fork 3
/
location_handler.go
67 lines (56 loc) · 2 KB
/
location_handler.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
package locationhttp
import (
"github.com/go-park-mail-ru/2019_2_Comandus/internal/app/general/respond"
"github.com/go-park-mail-ru/2019_2_Comandus/internal/app/location"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/microcosm-cc/bluemonday"
"github.com/pkg/errors"
"go.uber.org/zap"
"net/http"
"strconv"
)
type LocationHandler struct {
LocationUsecase location.Usecase
sanitizer *bluemonday.Policy
logger *zap.SugaredLogger
sessionStore sessions.Store
}
func NewLocationHandler(m *mux.Router, u location.Usecase, sanitizer *bluemonday.Policy, logger *zap.SugaredLogger, sessionStore sessions.Store) {
handler := &LocationHandler{
LocationUsecase: u,
sanitizer: sanitizer,
logger: logger,
sessionStore: sessionStore,
}
m.HandleFunc("/country-list", handler.HandleGetCountries).Methods(http.MethodGet, http.MethodOptions)
m.HandleFunc("/city-list/{id:[0-9]+}", handler.HandleGetCities).Methods(http.MethodGet, http.MethodOptions)
}
func (h *LocationHandler) HandleGetCountries(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
countries, err := h.LocationUsecase.CountryList()
if err != nil {
err = errors.Wrapf(err, "HandleGetCountries<-LocationUcase.CountryList()")
respond.Error(w, r, http.StatusInternalServerError, err)
return
}
respond.Respond(w, r, http.StatusOK, countries)
}
func (h *LocationHandler) HandleGetCities(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
ids := vars["id"]
id, err := strconv.Atoi(ids)
if err != nil {
err = errors.Wrapf(err, "HandleGetCities<-Atoi(wrong id)")
respond.Error(w, r, http.StatusBadRequest, err)
return
}
cities, err := h.LocationUsecase.CityListByCountry(int64(id))
if err != nil {
err = errors.Wrapf(err, "HandleGetCities<-LocationUcase.CityListByCountry()")
respond.Error(w, r, http.StatusBadRequest, err)
return
}
respond.Respond(w, r, http.StatusOK, cities)
}