Creating Filters struct and using it inside our listMoviesHandler
This commit is contained in:
@@ -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() {
|
||||
|
||||
7
internal/data/filters.go
Normal file
7
internal/data/filters.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package data
|
||||
|
||||
type Filters struct {
|
||||
Page int
|
||||
PageSize int
|
||||
Sort string
|
||||
}
|
||||
Reference in New Issue
Block a user