Updating MovieModel's method from internal/data/movies.go file. Create the updateMovieHandler function inside cmd/api/movies.go. Adding the route into routes.go to update a movie.

This commit is contained in:
Maxime Delporte
2025-11-07 11:27:45 +01:00
parent 490c3174ca
commit 69651c58b7
3 changed files with 82 additions and 1 deletions

View File

@@ -87,3 +87,66 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
app.serverErrorResponse(w, r, err)
}
}
func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Request) {
// Extract the movie ID from the URL
id, err := app.readIDParam(r)
if err != nil {
app.notFoundResponse(w, r)
return
}
// Fetch the existing movie record from the database, sending a 404 Not Found response to the client if we couldn't find a matching record.
movie, err := app.models.Movies.Get(id)
if err != nil {
switch {
case errors.Is(err, data.ErrRecordNotFound):
app.notFoundResponse(w, r)
default:
app.serverErrorResponse(w, r, err)
}
return
}
// Declare an input struct to hold the expected data from the client
var input struct {
Title string `json:"title"`
Year int32 `json:"year"`
Runtime data.Runtime `json:"runtime"`
Genres []string `json:"genres"`
}
// Read the JSON request body data into the input struct
err = app.readJSON(w, r, &input)
if err != nil {
app.badRequestResponse(w, r, err)
return
}
// Copy the values from the request body to the appropriate fields of the movie record.
movie.Title = input.Title
movie.Year = input.Year
movie.Runtime = input.Runtime
movie.Genres = input.Genres
// Validate the updated movie record, sending the client a 422 Unprocessable Entity response if any checks fail.
v := validator.New()
if data.ValidateMovie(v, movie); !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return
}
// Pass the updated movie record to our new Update() method
err = app.models.Movies.Update(movie)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
// Write the updated movie record in a JSON response
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}