Adding ValidateFilters method in filters.go and use it in our listMoviesHandler

This commit is contained in:
Maxime Delporte
2025-11-12 11:09:57 +01:00
parent bf7c53e565
commit 95d9de60b4
2 changed files with 25 additions and 3 deletions

View File

@@ -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() {