Adding a check on the X-Expected-Version header field.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 1m0s

This commit is contained in:
Maxime Delporte
2025-11-10 10:21:14 +01:00
parent f8f78c3eec
commit fd3bca1226

View File

@@ -6,6 +6,7 @@ import (
"greenlight.craftr.fr/internal/data" "greenlight.craftr.fr/internal/data"
"greenlight.craftr.fr/internal/validator" "greenlight.craftr.fr/internal/validator"
"net/http" "net/http"
"strconv"
) )
// "POST /v1/movies" endpoint. // "POST /v1/movies" endpoint.
@@ -108,6 +109,14 @@ func (app *application) updateMovieHandler(w http.ResponseWriter, r *http.Reques
return 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 // 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. // 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 { var input struct {