Updating MovieModel's method from internal/data/movies.go file. Create the updateMovieHandler function inside cmd/api/movies.go. Adding the route into routes.go to update a movie.
This commit is contained in:
@@ -109,7 +109,24 @@ func (m MovieModel) Get(id int64) (*Movie, error) {
|
||||
|
||||
// Update : 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)
|
||||
}
|
||||
|
||||
// Delete : Deleting a specific record from the movies table
|
||||
|
||||
Reference in New Issue
Block a user