package data import ( "context" "errors" "time" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" "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 ` // Mimicking a long running query. FYI since this changes the number of returned // fields, so I implmented the throwaway variable to take care of that // query := ` // SELECT pg_sleep(8), id, created_at, title, year, runtime, genres, version // FROM movies // WHERE id = $1 // ` var movie Movie err := m.pool.QueryRow(ctx, query, id).Scan( // &[]byte{}, // throwaway the pg_sleep value &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 { // Using version here as an optimistic lock. Look up optimistic vs pessimistic locking // https://stackoverflow.com/questions/129329/optimistic-vs-pessimistic-locking/129397#129397 // version incrementing with an integer is cheap. Don't try with a last_updated timestamp...that could // cause issues. If you don't want version to be guessable then a UUID generated by the DB is suitable. // Example: SET ... version = uuid_generate_v4() query := ` UPDATE movies SET title = $1, year = $2, runtime = $3, genres = $4, version = version + 1 WHERE id = $5 and version = $6 RETURNING version ` args := []any{ movie.Title, movie.Year, movie.Runtime, movie.Genres, movie.ID, movie.Version, } // Will not return any rows if the version number has already changed. err := m.pool.QueryRow(ctx, query, args...).Scan(&movie.Version) if err != nil { switch { case errors.Is(err, pgx.ErrNoRows): return ErrEditConflict default: return err } } return nil } // Here I decided to attempt an idiomatic version of the Delete service with // a database transaction. Since this is updating just one entry in a single table // an explicit db transaction isn't really necessary but I thought I'd practice it // none the less. If using sqlc you'd probably not even worry about implementing // it this way, but I am here to learn so. func (m MovieModel) Delete(ctx context.Context, id int64) (err error) { if id < 1 { return ErrRecordNotFound } query := `DELETE FROM movies WHERE id = $1` tx, err := m.pool.BeginTx(ctx, pgx.TxOptions{}) if err != nil { return err } defer func() { // If it's in error or the context was canceled, rollback if err != nil || ctx.Err() != nil { _ = tx.Rollback(ctx) return } // if the commit fails raises the error. err = tx.Commit(ctx) }() var cmd pgconn.CommandTag cmd, err = tx.Exec(ctx, query, id) if err != nil { return err } if cmd.RowsAffected() == 0 { return ErrRecordNotFound } return nil }