ch7.1
This commit is contained in:
31
internal/data/models.go
Normal file
31
internal/data/models.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrRecordNotFound = errors.New("record not found")
|
||||
)
|
||||
|
||||
type Models struct {
|
||||
Movies interface {
|
||||
Insert(movie *Movie) error
|
||||
Get(id int64) (*Movie, error)
|
||||
Update(movie *Movie) error
|
||||
Delete(id int64) error
|
||||
}
|
||||
}
|
||||
|
||||
func NewModels(db *sql.DB) Models {
|
||||
return Models{
|
||||
Movies: MovieModel{DB: db},
|
||||
}
|
||||
}
|
||||
|
||||
func NewMockModels() Models {
|
||||
return Models{
|
||||
Movies: MockMovieModel{},
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,16 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
||||
)
|
||||
|
||||
type MovieModel struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
type Movie struct {
|
||||
ID int64 `json:"id"`
|
||||
CreatedAt time.Time `json:"-"`
|
||||
@@ -16,6 +21,40 @@ type Movie struct {
|
||||
Version int32 `json:"version"`
|
||||
}
|
||||
|
||||
func (m MovieModel) Insert(movie *Movie) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MovieModel) Get(id int64) (*Movie, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m MovieModel) Update(movie *Movie) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MovieModel) Delete(id int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type MockMovieModel struct{}
|
||||
|
||||
func (m MockMovieModel) Insert(movie *Movie) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MockMovieModel) Get(id int64) (*Movie, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m MockMovieModel) Update(movie *Movie) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m MockMovieModel) Delete(id int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateMovie(v *validator.Validator, movie *Movie) {
|
||||
v.Check(movie.Title != "", "title", "must be provided")
|
||||
v.Check(len(movie.Title) <= 500, "title", "must not be more than 500 bytes long")
|
||||
|
||||
Reference in New Issue
Block a user