feat: update changes, race condition check on version, put -> patch

This commit is contained in:
2026-03-17 16:26:33 +01:00
parent bcf16bc4c6
commit 973a9e341d
10 changed files with 67 additions and 25 deletions
+13 -2
View File
@@ -86,7 +86,7 @@ func (m MovieModel) Update(movie *Movie) error {
query := `
UPDATE movies
SET title = $1, year = $2, runtime = $3, genres = $4, version = version + 1
WHERE id = $5
WHERE id = $5 AND version = $6
RETURNING version`
// Create an args slice containing the values for the placeholder parameters.
@@ -96,11 +96,22 @@ func (m MovieModel) Update(movie *Movie) error {
movie.Runtime,
pq.Array(movie.Genres),
movie.ID,
movie.Version,
}
// 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)
err := m.DB.QueryRow(query, args...).Scan(&movie.Version)
if err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
return ErrEditConflict
default:
return err
}
}
return nil
}
func (m MovieModel) Delete(id int64) error {