diff --git a/cmd/api/movies.go b/cmd/api/movies.go index 47d603a..6d6cafa 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -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) + } } diff --git a/internal/data/movies.go b/internal/data/movies.go new file mode 100644 index 0000000..82b56a6 --- /dev/null +++ b/internal/data/movies.go @@ -0,0 +1,18 @@ +package data + +import "time" + +// Movie +/* +Annotate the Movie struct with struct tags to control how the keys +appear in the JSON-encoded output. +*/ +type Movie struct { + ID int64 `json:"id"` + CreatedAt time.Time `json:"-"` + Title string `json:"title"` + Year int32 `json:"year,omitempty"` + Runtime int32 `json:"runtime,omitempty,string"` + Genres []string `json:"genres,omitempty"` + Version int32 `json:"version"` +}