feat: update, delete, get movies

This commit is contained in:
2026-03-16 13:39:27 +01:00
parent bf1c306ef7
commit bcf16bc4c6
7 changed files with 248 additions and 21 deletions
+106 -11
View File
@@ -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)
}
}