From 1a658d30631b66a8c304a5beb56e9f617fedb7d4 Mon Sep 17 00:00:00 2001 From: Maxime Delporte Date: Thu, 6 Nov 2025 10:41:12 +0100 Subject: [PATCH] Updating movies.go with MovieModel: this struct is the gateway between our app and the database. Adding CRUD methods alongside for the Movie object. Creating models.go: this file holds a Models struct wrapping all of our Models. This way, we are able to use in our application struct in our main.go. --- cmd/api/main.go | 8 ++++---- internal/data/models.go | 23 +++++++++++++++++++++++ internal/data/movies.go | 29 ++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 internal/data/models.go diff --git a/cmd/api/main.go b/cmd/api/main.go index f350ffb..2310e1a 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -5,6 +5,7 @@ import ( "database/sql" "flag" "fmt" + "greenlight.craftr.fr/internal/data" "log/slog" "net/http" "os" @@ -40,13 +41,11 @@ type config struct { } } -/* -Define an application struct to hold the dependencies for our HTTP handlers, helpers, -and middleware. -*/ +// application struct hold the dependencies for our HTTP handlers, helpers, and middleware. type application struct { config config logger *slog.Logger + models data.Models } func main() { @@ -91,6 +90,7 @@ func main() { app := &application{ config: cfg, logger: logger, + models: data.NewModels(db), } /* diff --git a/internal/data/models.go b/internal/data/models.go new file mode 100644 index 0000000..e3826c3 --- /dev/null +++ b/internal/data/models.go @@ -0,0 +1,23 @@ +package data + +import ( + "database/sql" + "errors" +) + +// ErrRecordNotFound : 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") +) + +// Models : Wraps the MovieModel. We'll add other models to this, like a UserModel and PermissionModel, as our build progresses. +type Models struct { + Movies MovieModel +} + +// NewModels : For ease of use, this method returns a Models struct containing the initialized MovieModel. +func NewModels(db *sql.DB) Models { + return Models{ + Movies: MovieModel{DB: db}, + } +} diff --git a/internal/data/movies.go b/internal/data/movies.go index 9fab01b..fda2ae5 100644 --- a/internal/data/movies.go +++ b/internal/data/movies.go @@ -1,8 +1,10 @@ package data import ( - "greenlight.craftr.fr/internal/validator" + "database/sql" "time" + + "greenlight.craftr.fr/internal/validator" ) // Movie @@ -41,3 +43,28 @@ func ValidateMovie(v *validator.Validator, movie *Movie) { // Not that we're using the Unique helper in the line below to check that all values in the input.Genres slice are unique v.Check(validator.Unique(movie.Genres), "genres", "must not contain duplicate values") } + +// MovieModel struct type wraps a sql.DB connection pool +type MovieModel struct { + DB *sql.DB +} + +// Insert : Inserting a new record in the movies table +func (m MovieModel) Insert(movie *Movie) error { + return nil +} + +// Get : Fetching a specific record from the movies table +func (m MovieModel) Get(id int64) (*Movie, error) { + return nil, nil +} + +// Update : Updating a specific record in the movies table +func (m MovieModel) Update(movie *Movie) error { + return nil +} + +// Delete : Deleting a specific record from the movies table +func (m MovieModel) Delete(id int64) error { + return nil +}