feat: sql, migrations, sql postgress db setup, movie model
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// Define a custom ErrRecordNotFound error. We'll return this from our Get() method when
|
||||
// looking up a movie that doesn't exist in our database.
|
||||
var (
|
||||
ErrRecordNotFound = errors.New("record not found")
|
||||
)
|
||||
|
||||
// Create a Models struct which wraps the MovieModel. We'll add other models to this,
|
||||
// like a UserModel and PermissionModel, as our build progresses.
|
||||
type Models struct {
|
||||
Movies MovieModel
|
||||
}
|
||||
|
||||
// For ease of use, we also add a New()
|
||||
// method which returns a Models struct containing
|
||||
// the initialized MovieModel.
|
||||
func NewModels(db *sql.DB) Models {
|
||||
return Models{
|
||||
Movies: MovieModel{DB: db},
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,53 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"greenlight.debuggingjon.dev/internal/validator"
|
||||
)
|
||||
|
||||
// Define a MovieModel struct type which wraps a sql.DB connection pool.
|
||||
type MovieModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
// Add a placeholder method for inserting a new record in the movies table.
|
||||
func (m MovieModel) Insert(movie *Movie) error {
|
||||
// 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 := []interface{}{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)
|
||||
}
|
||||
|
||||
// Add a placeholder method for fetching a specific record from the movies table.
|
||||
func (m MovieModel) Get(id int64) (*Movie, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// Add a placeholder method for updating a specific record in the “movies table.
|
||||
func (m MovieModel) Update(movie *Movie) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Add a placeholder method for deleting a specific record from the movies table.
|
||||
func (m MovieModel) Delete(id int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type Movie struct {
|
||||
ID int64 `json:"id"`
|
||||
CreatedAt time.Time `json:"-"` // Use the - directive
|
||||
|
||||
Reference in New Issue
Block a user