package data import ( "context" "errors" "fmt" "log/slog" "time" "git.runcible.io/learning/pulley/internal/database" "git.runcible.io/learning/pulley/internal/validator" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" ) // 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 { db database.PgxIface logger *slog.Logger } // So in regards to pulling the queries out into constants. I think it makes sense // in a world where the query gets really gnarly, but here it ends up violating code locality // and honestly doesn't provide that much utility in pgmock, because a regex is probably a better // fit anyways for testing and mocks. // If you the whitespace becomes a problem pgxmocks should use a real regex // or a strings.Join(strings.Fields(q), " ") helper function could be used to normalizes // all the sql strings. Postgres doesn't care about whitespace though. const InsertMovieQuery = ` INSERT INTO movies (title, year, runtime, genres) VALUES ($1, $2, $3, $4) RETURNING id, created_at, version` func (m MovieModel) Insert(ctx context.Context, movie *Movie) error { args := []any{movie.Title, movie.Year, movie.Runtime, movie.Genres} row := m.db.QueryRow(ctx, InsertMovieQuery, args...) // Insert is mutating the Movie struct err := row.Scan(&movie.ID, &movie.CreatedAt, &movie.Version) return err } const GetMovieQuery = ` SELECT id, created_at, title, year, runtime, genres, version FROM movies WHERE id = $1` func (m MovieModel) Get(ctx context.Context, id int64) (*Movie, error) { // safety validation if id < 1 { return nil, ErrRecordNotFound } // 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.db.QueryRow(ctx, GetMovieQuery, 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 } const UpdateMovieQuery = ` UPDATE movies SET title = $1, year = $2, runtime = $3, genres = $4, version = version + 1 WHERE id = $5 and version = $6 RETURNING version` 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() 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.db.QueryRow(ctx, UpdateMovieQuery, 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. const DeleteMovieQuery = ` DELETE FROM movies WHERE id = $1` func (m MovieModel) Delete(ctx context.Context, id int64) (err error) { if id < 1 { return ErrRecordNotFound } tx, err := m.db.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, DeleteMovieQuery, id) if err != nil { return err } if cmd.RowsAffected() == 0 { return ErrRecordNotFound } return nil } const GetAllMoviesQueryTemplate = ` SELECT count(*) OVER(), id, created_at, title, year, runtime, genres, version FROM movies WHERE (to_tsvector('simple', title) @@ plainto_tsquery('simple', $1) OR $1 = '') AND (genres @> $2 OR $2 = '{}') ORDER BY %s %s, id ASC LIMIT $3 OFFSET $4` func (m MovieModel) GetAll(ctx context.Context, title string, genres []string, filters Filters) ([]*Movie, Metadata, error) { // OLD query // query := ` // SELECT id, created_at, title, year, runtime, genres, version // FROM movies // ORDER BY id ASC // ` // The plainto_tsquery('simple', $1) function takes a search value and turns it // into a formatted query term that PostgreSQL full-text search can understand. // It normalizes the search value (again using the simple configuration), strips // any special characters, and inserts the and operator & between the words. // As an example, the search value "The Club" would result in the query term // 'the' & 'club'. // The @@ operator is the matches operator. In our statement we are using it to // check whether the generated query term matches the lexemes. To continue the // example, the query term 'the' & 'club' will match rows which contain both lexemes // 'the' and 'club'. // query := ` // SELECT id, created_at, title, year, runtime, genres, version // FROM movies // WHERE (to_tsvector('simple', title) @@ plainto_tsquery('simple', $1) OR $1 = '') // AND (genres @> $2 OR $2 = '{}') // ORDER BY id` // Using a window function to produce a totalRecords count using the WHERE parameters of // the query query := fmt.Sprintf(GetAllMoviesQueryTemplate, filters.sortColumn(), filters.sortDirection()) // ctx want some timeout for queries. When used in the handler the context passed should // be the r.Context. Since cancel functions are inherited it will cancel on client // disconnect or on time out. ctxTimeout, cancel := context.WithTimeout(ctx, 3*time.Second) defer cancel() m.logger.Debug(query) args := []any{title, genres, filters.limit(), filters.offset()} rows, err := m.db.Query(ctxTimeout, query, args...) if err != nil { return nil, Metadata{}, err } defer rows.Close() totalRecords := 0 movies := []*Movie{} for rows.Next() { var movie Movie err := rows.Scan( &totalRecords, &movie.ID, &movie.CreatedAt, &movie.Title, &movie.Year, &movie.Runtime, &movie.Genres, &movie.Version, ) if err != nil { return nil, Metadata{}, err } movies = append(movies, &movie) } err = rows.Err() if err != nil { return nil, Metadata{}, err } metadata := calculateMetadata(totalRecords, filters.Page, filters.PageSize) return movies, metadata, nil } func ValidateMovie(v *validator.Validator, m *Movie) { v.Check(m.Title != "", "title", "must be provided") v.Check(len(m.Title) <= 500, "title", "must not be more than 500 bytes long") v.Check(m.Year != 0, "year", "must be provided") v.Check(m.Year >= 1888, "year", "must be greater than 1888") v.Check(m.Year <= int32(time.Now().Year()), "year", "must not be in the future") v.Check(m.Runtime != 0, "runtime", "must be provided") v.Check(m.Runtime > 0, "runtime", "must be a positive integer") v.Check(m.Genres != nil, "genres", "must be provided") v.Check(len(m.Genres) >= 1, "genres", "must contain one genre") v.Check(len(m.Genres) <= 5, "genres", "must not contain more than 5 genres") v.Check(validator.Unique(m.Genres), "genres", "must not contain duplicate values") }