-
Notifications
You must be signed in to change notification settings - Fork 3
/
restaurant.go
62 lines (47 loc) · 1.33 KB
/
restaurant.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
package server
import (
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/nokia-wroclaw/innovativeproject-togeather/backend/pkg/core"
)
type restaurantHandler struct {
restaurantService core.RestaurantService
}
func (h *restaurantHandler) list(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
restaurantsList, err := h.restaurantService.List(ctx)
if err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
respondJSON(w, http.StatusOK, restaurantsList)
}
func (h *restaurantHandler) menu(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
restaurantID, err := strconv.Atoi(chi.URLParam(r, "restaurantID"))
if err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
restaurantMenu, err := h.restaurantService.GetMenu(ctx, restaurantID)
if err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
respondJSON(w, http.StatusOK, restaurantMenu)
}
func (h *restaurantHandler) get(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
restaurantID, err := strconv.Atoi(chi.URLParam(r, "restaurantID"))
if err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
restaurant, err := h.restaurantService.Get(ctx, restaurantID)
if err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
respondJSON(w, http.StatusOK, restaurant)
}