Updating internal/data/movies.go to Get a movie from the database. Updating cmd/api/movies.go showMovieHandler method to fetch a movie from a request (using the MovieModel's Get method).

This commit is contained in:
Maxime Delporte
2025-11-07 10:58:58 +01:00
parent 0653a46101
commit 0824a127b9
2 changed files with 49 additions and 16 deletions

View File

@@ -1,11 +1,11 @@
package main
import (
"errors"
"fmt"
"greenlight.craftr.fr/internal/data"
"greenlight.craftr.fr/internal/validator"
"net/http"
"time"
)
// "POST /v1/movies" endpoint.
@@ -70,24 +70,20 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
return
}
/*
Create a new instance of the Movie struct containing the ID we
extracted from the URL and some dummy data. Also notice that we
deliberately haven't set a value for the Year field.
*/
movie := data.Movie{
ID: id,
CreatedAt: time.Now(),
Title: "Casablanca",
Runtime: 102,
Genres: []string{"drama", "romance", "war"},
Version: 1,
// Call the Get() method to fetch the data for a specific movie. We also need to use the errors.Is() function to check if it returns a data.ErrRecordNotFound error in which case we send a 404 Not Found response to the client.
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
}
// Encode the struct to JSON and send it as the HTTP response.
err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil)
if err != nil {
// Use the new serverErrorResponse() helper
app.serverErrorResponse(w, r, err)
}
}