diff --git a/projects/greenlight/cmd/api/movies.go b/projects/greenlight/cmd/api/movies.go index b6b54d2..fb42048 100644 --- a/projects/greenlight/cmd/api/movies.go +++ b/projects/greenlight/cmd/api/movies.go @@ -1,9 +1,9 @@ package main import ( + "errors" "fmt" "net/http" - "time" "greenlight.debuggingjon.dev/internal/data" "greenlight.debuggingjon.dev/internal/validator" @@ -68,24 +68,119 @@ func (app *application) createMovieHandler(w http.ResponseWriter, r *http.Reques 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, + // 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 } - // 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) + } +} + +func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Request) { + // Extract the movie ID from the URL. + id, err := app.readIDParam(r) + if err != nil { + app.notFoundResponse(w, r) + return + } + + // Fetch the existing movie record from the database, sending a 404 Not Found + // response to the client if we couldn't find a matching record. + 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 + } + // Declare an input struct to hold the expected data from the client. + var input struct { + Title string `json:"title"` + Year int32 `json:"year"` + Runtime data.Runtime `json:"runtime"` + Genres []string `json:"genres"` + } + + // Read the JSON request body data into the input struct. + err = app.readJSON(w, r, &input) + if err != nil { + app.badRequestResponse(w, r, err) + return + } + + // Copy the values from the request body to the appropriate fields of the movie + // record. + movie.Title = input.Title + movie.Year = input.Year + movie.Runtime = input.Runtime + movie.Genres = input.Genres + + // Validate the updated movie record, sending the client a 422 Unprocessable Entity + // response if any checks fail. + v := validator.New() + + if data.ValidateMovie(v, movie); !v.Valid() { + app.failedValidationResponse(w, r, v.Errors) + return + } + + // Pass the updated movie record to our new Update() method. + err = app.models.Movies.Update(movie) + if err != nil { + app.serverErrorResponse(w, r, err) + return + } + + // Write the updated movie record in a JSON response. + err = app.writeJSON(w, http.StatusOK, envelope{"movie": movie}, nil) + if err != nil { + app.serverErrorResponse(w, r, err) + } +} + +func (app *application) deleteMovieHandler(w http.ResponseWriter, r *http.Request) { + // Extract the movie ID from the URL. + id, err := app.readIDParam(r) + if err != nil { + app.notFoundResponse(w, r) + return + } + + // Delete the movie from the database, sending a 404 Not Found response to the + // client if there isn't a matching record. + err = app.models.Movies.Delete(id) + if err != nil { + switch { + case errors.Is(err, data.ErrRecordNotFound): + app.notFoundResponse(w, r) + default: + app.serverErrorResponse(w, r, err) + } + return + } + + // Return a 200 OK status code along with a success message. + err = app.writeJSON(w, http.StatusOK, envelope{"message": "movie successfully deleted"}, nil) + if err != nil { app.serverErrorResponse(w, r, err) } } diff --git a/projects/greenlight/cmd/api/routes.go b/projects/greenlight/cmd/api/routes.go index 852d63b..5b01ef6 100644 --- a/projects/greenlight/cmd/api/routes.go +++ b/projects/greenlight/cmd/api/routes.go @@ -21,6 +21,8 @@ func (app *application) routes() *httprouter.Router { router.HandlerFunc(http.MethodGet, "/v1/healthcheck", app.healthcheckHandler) router.HandlerFunc(http.MethodPost, "/v1/movies", app.createMovieHandler) router.HandlerFunc(http.MethodGet, "/v1/movies/:id", app.showMovieHandler) + router.HandlerFunc(http.MethodPut, "/v1/movies/:id", app.updateMovieHandler) + router.HandlerFunc(http.MethodDelete, "/v1/movies/:id", app.deleteMovieHandler) return router } diff --git a/projects/greenlight/docs/api/greenlight-bruno/Create Movie.yml b/projects/greenlight/docs/api/greenlight-bruno/Create Movie.yml index 7e97970..9a5fd40 100644 --- a/projects/greenlight/docs/api/greenlight-bruno/Create Movie.yml +++ b/projects/greenlight/docs/api/greenlight-bruno/Create Movie.yml @@ -10,10 +10,10 @@ http: type: json data: |- { - "title": "The Breakfast Club", - "runtime": "96 mins", - "year": 1986, - "genres": ["drama"] + "title": "Test", + "runtime": "120 mins", + "year": 2025, + "genres": ["action", "horror"] } auth: inherit diff --git a/projects/greenlight/docs/api/greenlight-bruno/Delete Movie.yml b/projects/greenlight/docs/api/greenlight-bruno/Delete Movie.yml new file mode 100644 index 0000000..0b1c428 --- /dev/null +++ b/projects/greenlight/docs/api/greenlight-bruno/Delete Movie.yml @@ -0,0 +1,15 @@ +info: + name: Delete Movie + type: http + seq: 5 + +http: + method: DELETE + url: "{{URL}}{{VERSION}}/movies/{{MOVIE_ID}}" + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 diff --git a/projects/greenlight/docs/api/greenlight-bruno/Update Movie.yml b/projects/greenlight/docs/api/greenlight-bruno/Update Movie.yml new file mode 100644 index 0000000..98d1838 --- /dev/null +++ b/projects/greenlight/docs/api/greenlight-bruno/Update Movie.yml @@ -0,0 +1,24 @@ +info: + name: Update Movie + type: http + seq: 4 + +http: + method: PUT + url: "{{URL}}{{VERSION}}/movies/{{MOVIE_ID}}" + body: + type: json + data: |- + { + "title": "Sinners", + "runtime": "120 mins", + "year": 2025, + "genres": ["action", "horror"] + } + auth: inherit + +settings: + encodeUrl: true + timeout: 0 + followRedirects: true + maxRedirects: 5 diff --git a/projects/greenlight/docs/api/greenlight-bruno/environments/Development.yml b/projects/greenlight/docs/api/greenlight-bruno/environments/Development.yml index fdeaeb1..c21d9c7 100644 --- a/projects/greenlight/docs/api/greenlight-bruno/environments/Development.yml +++ b/projects/greenlight/docs/api/greenlight-bruno/environments/Development.yml @@ -5,4 +5,4 @@ variables: - name: VERSION value: v1 - name: MOVIE_ID - value: "1" + value: "8" diff --git a/projects/greenlight/internal/data/movies.go b/projects/greenlight/internal/data/movies.go index 8fe1cf8..98d811a 100644 --- a/projects/greenlight/internal/data/movies.go +++ b/projects/greenlight/internal/data/movies.go @@ -2,6 +2,7 @@ package data import ( "database/sql" + "errors" "time" "github.com/lib/pq" @@ -33,18 +34,108 @@ func (m MovieModel) Insert(movie *Movie) error { return m.DB.QueryRow(query, args...).Scan(&movie.ID, &movie.CreatedAt, &movie.Version) } -// Add a placeholder method for fetching a specific record from the movies table. func (m MovieModel) Get(id int64) (*Movie, error) { - return nil, nil + // The PostgreSQL bigserial type that we're using for the movie ID starts + // auto-incrementing at 1 by default, so we know that no movies will have ID values + // less than that. To avoid making an unnecessary database call, we take a shortcut + // and return an ErrRecordNotFound error straight away. + if id < 1 { + return nil, ErrRecordNotFound + } + + // Define the SQL query for retrieving the movie data. + query := ` + SELECT id, created_at, title, year, runtime, genres, version + FROM movies + WHERE id = $1` + + // Declare a Movie struct to hold the data returned by the query. + var movie Movie + + // Execute the query using the QueryRow() method, passing in the provided id value + // as a placeholder parameter, and scan the response data into the fields of the + // Movie struct. Importantly, notice that we need to convert the scan target for the + // genres column using the pq.Array() adapter function again. + err := m.DB.QueryRow(query, id).Scan( + &movie.ID, + &movie.CreatedAt, + &movie.Title, + &movie.Year, + &movie.Runtime, + pq.Array(&movie.Genres), + &movie.Version, + ) // Handle any errors. If there was no matching movie found, Scan() will return + // a sql.ErrNoRows error. We check for this and return our custom ErrRecordNotFound + // error instead. + if err != nil { + switch { + case errors.Is(err, sql.ErrNoRows): + return nil, ErrRecordNotFound + default: + return nil, err + } + } + + // Otherwise, return a pointer to the Movie struct. + return &movie, nil } -// Add a placeholder method for updating a specific record in the “movies table. func (m MovieModel) Update(movie *Movie) error { - return nil + // Declare the SQL query for updating the record and returning the new version + // number. + query := ` + UPDATE movies + SET title = $1, year = $2, runtime = $3, genres = $4, version = version + 1 + WHERE id = $5 + RETURNING version` + + // Create an args slice containing the values for the placeholder parameters. + args := []any{ + movie.Title, + movie.Year, + movie.Runtime, + pq.Array(movie.Genres), + movie.ID, + } + + // Use the QueryRow() method to execute the query, passing in the args slice as a + // variadic parameter and scanning the new version value into the movie struct. + return m.DB.QueryRow(query, args...).Scan(&movie.Version) } -// Add a placeholder method for deleting a specific record from the movies table. func (m MovieModel) Delete(id int64) error { + // Return an ErrRecordNotFound error if the movie ID is less than 1. + if id < 1 { + return ErrRecordNotFound + } + + // Construct the SQL query to delete the record. + query := ` + DELETE FROM movies + WHERE id = $1` + + // Execute the SQL query using the Exec() method, passing in the id variable as + // the value for the placeholder parameter. The Exec() method returns a sql.Result + // object. + result, err := m.DB.Exec(query, id) + if err != nil { + return err + } + + // Call the RowsAffected() method on the sql.Result object to get the number of rows + // affected by the query. + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + // If no rows were affected, we know that the movies table didn't contain a record + // with the provided ID at the moment we tried to delete it. In that case we + // return an ErrRecordNotFound error. + if rowsAffected == 0 { + return ErrRecordNotFound + } + return nil }