package main import ( "fmt" "net/http" "time" "greenlight.debuggingjon.dev/internal/data" "greenlight.debuggingjon.dev/internal/validator" ) func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { var input struct { Title string `json:"title"` Year int32 `json:"year"` Runtime data.Runtime `json:"runtime"` Genres []string `json:"genres"` } err := app.readJSON(w, r, &input) if err != nil { app.badRequestResponse(w, r, err) return } // Copy the values from the input struct to a new Movie struct. movie := &data.Movie{ Title: input.Title, Year: input.Year, Runtime: input.Runtime, Genres: input.Genres, } // Initialize a new Validator. v := validator.New() // Call the ValidateMovie() function and return a response containing the errors if // any of the checks fail. if data.ValidateMovie(v, movie); !v.Valid() { app.failedValidationResponse(w, r, v.Errors) return } fmt.Fprintf(w, "%+v\n", input) } func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) { id, err := app.readIDParam(r) if err != nil { // Use the new notFoundResponse() helper. app.notFoundResponse(w, r) return } movie := data.Movie{ ID: id, CreatedAt: time.Now(), Title: "Casablanca", Runtime: 102, Genres: []string{"drama", "romance", "war"}, Version: 1, } // Create an envelope{"movie": movie} instance and pass it to writeJSON(), instead // of passing the plain movie struct. err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil) if err != nil { // Use the new serverErrorResponse() helper. app.serverErrorResponse(w, r, err) } }