Updating Insert MovieModel's method inserting a new record in the movies table. Updating createMovieHandler to use Insert MovieModel's method.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 1m0s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 1m0s
This commit is contained in:
@@ -42,8 +42,22 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
// Dump the contents of the input struct in a HTTP response.
|
||||
fmt.Fprintf(w, "%+v\n", input)
|
||||
// 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"
|
||||
|
||||
Reference in New Issue
Block a user