Adding ValidateFilters method in filters.go and use it in our listMoviesHandler
This commit is contained in:
@@ -227,6 +227,14 @@ func (app *application) listMoviesHandler(w http.ResponseWriter, r *http.Request
|
||||
|
||||
// Extract the sort query string value, falling back to "id" if it is not provided by the client (which will imply an ascending sort on movie ID)
|
||||
input.Filters.Sort = app.readString(qs, "sort", "id")
|
||||
// Add the supported sort values for this endpoint to the sort safelist
|
||||
input.Filters.SortSafelist = []string{"id", "title", "year", "runtime", "-id", "-title", "-year", "-runtime"}
|
||||
|
||||
// Execute the validation checks on Filters struct and send a response
|
||||
if data.ValidateFilters(v, input.Filters); !v.Valid() {
|
||||
app.failedValidationResponse(w, r, v.Errors)
|
||||
return
|
||||
}
|
||||
|
||||
// Check the Validator instance for any errors and use the failedValidationResponse() helper to send the client a response if necessary
|
||||
if !v.Valid() {
|
||||
|
||||
Reference in New Issue
Block a user