package main import ( "errors" "fmt" "greenlight.craftr.fr/internal/data" "greenlight.craftr.fr/internal/validator" "net/http" ) // "POST /v1/movies" endpoint. func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Request) { // Declare an anonymous struct to hold the information that we expect to be in the HTTP request body (note that the field names and types in the struct are a subset of the Movie struct that we created earlier). This struct will be our *target decode destination*. var input struct { Title string `json:"title"` Year int32 `json:"year"` Runtime data.Runtime `json:"runtime"` Genres []string `json:"genres"` } // Use the new readJSON() helper to decode the request body into the input struct. If this returns an error, we send the client the error message along with a 400 Bad Request status code, just like before. 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 : we're doing this because a client could provide the keys *id* and *version* in their JSON request, and the corresponding values would be decoded without any error into the *ID* and *Version* fields of the *Movie* struct - even though we don't want them to be. movie := &data.Movie{ Title: input.Title, Year: input.Year, Runtime: input.Runtime, Genres: input.Genres, } // Initialize a new Validator instance 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 } // Call the Insert() method on our movies model, passing in a pointer to the validated struct. This will create a record in the database and update the movie struct with the system-generated information err = app.models.Movies.Insert(movie) if err != nil { app.serverErrorResponse(w, r, err) return } // When sending a HTTP response, we want to include a location header to let the client know which URL they can find the newly-created resource at. We make an empty http.Header map and then use the Set() method to add a new location header, interpolating the system-generated ID for our new movie in the URL. headers := make(http.Header) headers.Set("Location", fmt.Sprintf("/v1/movies/%d", movie.ID)) // Write a JSON response with a 201 Created status code, the movie data in the response body, and the Location header err = app.writeJSON(w, http.StatusCreated, envelope{"movie": movie}, headers) if err != nil { app.serverErrorResponse(w, r, err) } } // "GET /v1/movies/:id" func (app *application) showMovieHandler(w http.ResponseWriter, r *http.Request) { id, err := app.readIDParam(r) if err != nil || id < 1 { // Use the new notFoundResponse() helper app.notFoundResponse(w, r) return } // 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 } err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil) if err != nil { app.serverErrorResponse(w, r, err) } }