diff --git a/cmd/api/movies.go b/cmd/api/movies.go index cf74688..4d6303d 100644 --- a/cmd/api/movies.go +++ b/cmd/api/movies.go @@ -6,6 +6,7 @@ import ( "greenlight.craftr.fr/internal/data" "greenlight.craftr.fr/internal/validator" "net/http" + "strconv" ) // "POST /v1/movies" endpoint. @@ -108,6 +109,14 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques return } + // If the request contains a X-Expected-Version header, verify that the movie version in the database matches the expected version specified in the header. + if r.Header.Get("X-Expected-Version") != "" { + if strconv.Itoa(int(movie.Version)) != r.Header.Get("X-Expected-Version") { + app.editConflictResponse(w, r) + return + } + } + // Declare an input struct to hold the expected data from the client // Use pointers for the Title, Year and Runtime fields : so we can see if a client has provided a particular key/value pair in the JSON. var input struct {