feat: update, delete, get movies
This commit is contained in:
@@ -2,6 +2,7 @@ package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
@@ -33,18 +34,108 @@ func (m MovieModel) Insert(movie *Movie) error {
|
||||
return m.DB.QueryRow(query, args...).Scan(&movie.ID, &movie.CreatedAt, &movie.Version)
|
||||
}
|
||||
|
||||
// Add a placeholder method for fetching a specific record from the movies table.
|
||||
func (m MovieModel) Get(id int64) (*Movie, error) {
|
||||
return nil, nil
|
||||
// The PostgreSQL bigserial type that we're using for the movie ID starts
|
||||
// auto-incrementing at 1 by default, so we know that no movies will have ID values
|
||||
// less than that. To avoid making an unnecessary database call, we take a shortcut
|
||||
// and return an ErrRecordNotFound error straight away.
|
||||
if id < 1 {
|
||||
return nil, ErrRecordNotFound
|
||||
}
|
||||
|
||||
// Define the SQL query for retrieving the movie data.
|
||||
query := `
|
||||
SELECT id, created_at, title, year, runtime, genres, version
|
||||
FROM movies
|
||||
WHERE id = $1`
|
||||
|
||||
// Declare a Movie struct to hold the data returned by the query.
|
||||
var movie Movie
|
||||
|
||||
// Execute the query using the QueryRow() method, passing in the provided id value
|
||||
// as a placeholder parameter, and scan the response data into the fields of the
|
||||
// Movie struct. Importantly, notice that we need to convert the scan target for the
|
||||
// genres column using the pq.Array() adapter function again.
|
||||
err := m.DB.QueryRow(query, id).Scan(
|
||||
&movie.ID,
|
||||
&movie.CreatedAt,
|
||||
&movie.Title,
|
||||
&movie.Year,
|
||||
&movie.Runtime,
|
||||
pq.Array(&movie.Genres),
|
||||
&movie.Version,
|
||||
) // Handle any errors. If there was no matching movie found, Scan() will return
|
||||
// a sql.ErrNoRows error. We check for this and return our custom ErrRecordNotFound
|
||||
// error instead.
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
return nil, ErrRecordNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, return a pointer to the Movie struct.
|
||||
return &movie, nil
|
||||
}
|
||||
|
||||
// Add a placeholder method for 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)
|
||||
}
|
||||
|
||||
// Add a placeholder method for deleting a specific record from the movies table.
|
||||
func (m MovieModel) Delete(id int64) error {
|
||||
// Return an ErrRecordNotFound error if the movie ID is less than 1.
|
||||
if id < 1 {
|
||||
return ErrRecordNotFound
|
||||
}
|
||||
|
||||
// Construct the SQL query to delete the record.
|
||||
query := `
|
||||
DELETE FROM movies
|
||||
WHERE id = $1`
|
||||
|
||||
// Execute the SQL query using the Exec() method, passing in the id variable as
|
||||
// the value for the placeholder parameter. The Exec() method returns a sql.Result
|
||||
// object.
|
||||
result, err := m.DB.Exec(query, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Call the RowsAffected() method on the sql.Result object to get the number of rows
|
||||
// affected by the query.
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If no rows were affected, we know that the movies table didn't contain a record
|
||||
// with the provided ID at the moment we tried to delete it. In that case we
|
||||
// return an ErrRecordNotFound error.
|
||||
if rowsAffected == 0 {
|
||||
return ErrRecordNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user