-
Notifications
You must be signed in to change notification settings - Fork 0
/
rental_handler.go
162 lines (136 loc) · 4.38 KB
/
rental_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
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
package handler
import (
"context"
"fmt"
"net/http"
"strconv"
"github.com/go-chi/chi"
"github.com/gorilla/schema"
"github.com/dragonator/rental-service/module/rental/internal/http/contract"
"github.com/dragonator/rental-service/module/rental/internal/http/service/svc"
"github.com/dragonator/rental-service/module/rental/internal/model"
"github.com/dragonator/rental-service/module/rental/internal/storage"
)
// RentalFetchingOp is a contract to a rental fetching operation.
//
//go:generate moq -rm -pkg handler_test -out rental_fetching_op_mock_test.go . RentalFetchingOp
type RentalFetchingOp interface {
GetRentalByID(ctx context.Context, rentalID int) (*model.Rental, error)
ListRentals(ctx context.Context, filters *storage.RentalFilters) (model.Rentals, error)
}
// RentalHandler holds implementation of handlers for rentals.
type RentalHandler struct {
rentalFetchingOp RentalFetchingOp
}
// NewRentalHandler is a construction function for RentalHandler.
func NewRentalHandler(rentalFetchingOp RentalFetchingOp) *RentalHandler {
return &RentalHandler{
rentalFetchingOp: rentalFetchingOp,
}
}
// GetRentalByID returns a handle that is fetching a rental by id.
func (rh *RentalHandler) GetRentalByID(method, path string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
rentalID, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
errorResponse(w, fmt.Errorf("%w: id", svc.ErrInvalidQueryParameters))
return
}
rental, err := rh.rentalFetchingOp.GetRentalByID(r.Context(), rentalID)
if err != nil {
errorResponse(w, err)
return
}
successResponse(w, toRentalContract(rental))
return
}
}
// ListRentals returns a handle that is listing rentals based on filters.
func (rh *RentalHandler) ListRentals(method, path string) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
filters, err := rentalFiltersFromRequest(r)
if err != nil {
errorResponse(w, err)
return
}
rentals, err := rh.rentalFetchingOp.ListRentals(r.Context(), filters)
if err != nil {
errorResponse(w, err)
return
}
successResponse(w, toListRentalsResponse(rentals))
return
}
}
func rentalFiltersFromRequest(r *http.Request) (*storage.RentalFilters, error) {
var query contract.ListRentalsQuery
if err := r.ParseForm(); err != nil {
return nil, fmt.Errorf("%w: parsing form: %w", svc.ErrInvalidQueryParameters, err)
}
if err := schema.NewDecoder().Decode(&query, r.Form); err != nil {
return nil, fmt.Errorf("%w: unmashalling query: %w", svc.ErrInvalidQueryParameters, err)
}
if query.Sort != nil && !storage.SortFieldAllowed(*query.Sort) {
return nil, fmt.Errorf("%w: unexpected sort field: expected one of %v",
svc.ErrInvalidQueryParameters,
storage.RentalSortFields,
)
}
filters := &storage.RentalFilters{
IDs: query.Ids,
PriceMin: query.PriceMin,
PriceMax: query.PriceMax,
OrderBy: query.Sort,
Pagination: storage.Pagination{
Limit: query.Limit,
Offset: query.Offset,
},
}
if len(query.Near) > 0 {
if len(query.Near) != 2 {
return nil, fmt.Errorf("%w: invalid number of values for near (expected 2)", svc.ErrInvalidQueryParameters)
}
filters.Near = &storage.Location{
Latitude: query.Near[0],
Longitude: query.Near[1],
}
}
return filters, nil
}
func toRentalContract(rental *model.Rental) *contract.Rental {
return &contract.Rental{
ID: rental.ID,
Name: rental.Name,
Description: rental.Description,
Type: rental.Type,
Make: rental.VehicleMake,
Model: rental.VehicleModel,
Year: rental.VehicleYear,
Length: rental.VehicleLength,
Sleeps: rental.Sleeps,
PrimaryImageURL: rental.PrimaryImageURL,
Price: contract.Price{
Day: rental.PricePerDay,
},
Location: contract.Location{
City: rental.HomeCity,
State: rental.HomeState,
Zip: rental.HomeZip,
Country: rental.HomeCountry,
Latitude: rental.Latitude,
Longitude: rental.Longitude,
},
User: contract.User{
ID: rental.User.ID,
FirstName: rental.User.FirstName,
LastName: rental.User.LastName,
},
}
}
func toListRentalsResponse(rentals model.Rentals) *contract.ListRentalsResponse {
resp := contract.ListRentalsResponse{}
for _, r := range rentals {
resp = append(resp, toRentalContract(r))
}
return &resp
}