You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.3 KiB
Go

package data
import (
"context"
"errors"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// MUST export fields to serialize them
type Movie struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"-"` // omits always
Title string `json:"title"`
Year int32 `json:"year,omitzero"`
// Runtime int32 `json:"runtime,omitzero"`
// Use the Runtime type instead of int32. Note that the omitzero directive will
// still work on this: if the Runtime field has the underlying value 0, then it will
// be considered zero and omitted -- and the MarshalJSON() method we just made
// won't be called at all.
// VSCode will complain though about reflection issue
Runtime Runtime `json:"runtime,omitzero`
Genres []string `json:"genres,omitzero"`
Version int32 `json:"version"`
}
type MovieModel struct {
pool *pgxpool.Pool
}
func (m MovieModel) Insert(ctx context.Context, movie *Movie) error {
query := `
INSERT INTO movies (title, year, runtime, genres)
VALUES ($1, $2, $3, $4)
RETURNING id, created_at, version`
args := []any{movie.Title, movie.Year, movie.Runtime, movie.Genres}
row := m.pool.QueryRow(ctx, query, args...)
// Insert is mutating the Movie struct
err := row.Scan(&movie.ID, &movie.CreatedAt, &movie.Version)
return err
}
func (m MovieModel) Get(ctx context.Context, id int64) (*Movie, error) {
// safety validation
if id < 1 {
return nil, ErrRecordNotFound
}
query := `
SELECT id, created_at, title, year, runtime, genres, version
FROM movies
WHERE id = $1
`
var movie Movie
err := m.pool.QueryRow(ctx, query, id).Scan(
&movie.ID,
&movie.CreatedAt,
&movie.Title,
&movie.Year,
&movie.Runtime,
&movie.Genres,
&movie.Version,
)
if err != nil {
switch {
case errors.Is(err, pgx.ErrNoRows):
return nil, ErrRecordNotFound
default:
return nil, err
}
}
return &movie, nil
}
func (m MovieModel) Update(ctx context.Context, movie *Movie) error {
query := `
UPDATE movies
SET title = $1, year = $2, runtime = $3, genres = $4, version = version + 1
WHERE id = $5
RETURNING version
`
args := []any{
movie.Title,
movie.Year,
movie.Runtime,
movie.Genres,
movie.ID,
}
return m.pool.QueryRow(ctx, query, args...).Scan(&movie.Version)
}
func (m MovieModel) Delete(ctx context.Context, id int64) error {
return nil
}