Creating Movie struct inside internal/data/movies.go to be used in cmd/api/movies.go. Updating the showMovieHandler with the Movie struct.
This commit is contained in:
@@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"greenlight.craftr.fr/internal/data"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// "POST /v1/movies" endpoint.
|
||||
@@ -19,6 +21,24 @@ func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
// Otherwise, interpolate the movie ID in a placeholder response.
|
||||
fmt.Fprintf(w, "show the details of movie %d\n", id)
|
||||
/*
|
||||
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,
|
||||
}
|
||||
|
||||
// Encode the struct to JSON and send it as the HTTP response.
|
||||
err = app.writeJSON(w, http.StatusOK, movie, nil)
|
||||
if err != nil {
|
||||
app.logger.Error(err.Error())
|
||||
http.Error(w, "The server encountered a problem and could not process your request", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user