feat: get movies, url parametes parsing and validating, sql exeme querying

This commit is contained in:
2026-03-18 13:52:32 +01:00
parent 03f11029a1
commit 6b170a2705
9 changed files with 265 additions and 8 deletions
+56
View File
@@ -209,3 +209,59 @@ func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Reques
app.serverErrorResponse(w, r, err)
}
}
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
data.Filters
}
// Initialize a new Validator instance.
v := validator.New()
// Call r.URL.Query() to get the url.Values map containing the query string data.
qs := r.URL.Query()
// Use our helpers to extract the title and genres query string values, falling back
// to defaults of an empty string and an empty slice respectively if they are not
// provided by the client.
input.Title = app.readString(qs, "title", "")
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)
// Extract the sort query string value, falling back to "id" if it is not provided
// by the client (which will imply a ascending sort on movie ID).
input.Sort = app.readString(qs, "sort", "id")
// Add the supported sort values for this endpoint to the sort safelist.
input.SortSafelist = []string{"id", "title", "year", "runtime", "-id", "-title", "-year", "-runtime"}
// Execute the validation checks on the Filters struct and send a response
// containing the errors if necessary.
if data.ValidateFilters(v, input.Filters); !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return
}
// Call the GetAll() method to retrieve the movies, passing in the various filter
// parameters.
movies, err := app.models.Movies.GetAll(input.Title, input.Genres, input.Filters)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
// Send a JSON response containing the movie data.
err = app.writeJSON(w, http.StatusOK, envelope{"movies": movies}, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}