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)
|
||||
}
|
||||
}
|
||||
|
||||
18
internal/data/movies.go
Normal file
18
internal/data/movies.go
Normal file
@@ -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"`
|
||||
}
|
||||
Reference in New Issue
Block a user