Updating Insert MovieModel's method inserting a new record in the movies table. Updating createMovieHandler to use Insert MovieModel's method.
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 1m0s
All checks were successful
Deploy Greenlight API / deploy (push) Successful in 1m0s
This commit is contained in:
@@ -2,6 +2,7 @@ package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/lib/pq"
|
||||
"time"
|
||||
|
||||
"greenlight.craftr.fr/internal/validator"
|
||||
@@ -51,7 +52,17 @@ type MovieModel struct {
|
||||
|
||||
// Insert : Inserting a new record in the movies table
|
||||
func (m MovieModel) Insert(movie *Movie) error {
|
||||
return nil
|
||||
// Define the SQL query for inserting a new record in the movies table and returning the system-generated data.
|
||||
query := `
|
||||
INSERT INTO movies (title, year, runtime, genres)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, created_at, version`
|
||||
|
||||
// Create an args slice containing the values for the placeholder parameters from the movie struct. Declaring this slice immediately next to our SQL query helps to make it nice and clear *what values are being used where* in the query
|
||||
args := []any{movie.Title, movie.Year, movie.Runtime, pq.Array(movie.Genres)}
|
||||
|
||||
// Use the QueryRow() method to execute the SQL query on our connection pool, passing in the args slice as a variadic parameter and scanning the system-generated id, created_at and version values into the movie struct.
|
||||
return m.DB.QueryRow(query, args...).Scan(&movie.ID, &movie.CreatedAt, &movie.Version)
|
||||
}
|
||||
|
||||
// Get : Fetching a specific record from the movies table
|
||||
|
||||
Reference in New Issue
Block a user