diff --git a/cmd/api/movies.go b/cmd/api/movies.go index cf95145..b816ea4 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -206,11 +206,9 @@ func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Reques func (app *application) listMoviesHandler(w http.ResponseWriter, r *http.Request) { // To keep things consistent with our other handlers, we'll define an input struct to hold the expected values from the request query string var input struct { - Title string - Genres []string - Page int - PageSize int - Sort string + Title string + Genres []string + data.Filters } // Initialize a new Validator instance @@ -224,11 +222,11 @@ func (app *application) listMoviesHandler(w http.ResponseWriter, r *http.Request input.Genres = app.readCSV(qs, "genres", []string{}) // Get the page and page_size query string values as integers. Notice that we set the default page value to 1 and default page_size to 20, and that we pass the validator instance as the final argument here - input.Page = app.readInt(qs, "page", 1, v) - input.PageSize = app.readInt(qs, "page_size", 20, v) + input.Filters.Page = app.readInt(qs, "page", 1, v) + input.Filters.PageSize = app.readInt(qs, "page_size", 20, v) // 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.Sort = app.readString(qs, "sort", "id") + input.Filters.Sort = app.readString(qs, "sort", "id") // Check the Validator instance for any errors and use the failedValidationResponse() helper to send the client a response if necessary if !v.Valid() { diff --git a/internal/data/filters.go b/internal/data/filters.go new file mode 100644 index 0000000..2f7fe06 --- /dev/null +++ b/internal/data/filters.go @@ -0,0 +1,7 @@ +package data + +type Filters struct { + Page int + PageSize int + Sort string +}