ch7.1
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gitea.local.lab/Lbenedar/greenlight/internal/data"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,6 +19,7 @@ const version = "1.0.0"
|
|||||||
type application struct {
|
type application struct {
|
||||||
config config
|
config config
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
|
models data.Models
|
||||||
}
|
}
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
@@ -81,6 +83,7 @@ func main() {
|
|||||||
app := &application{
|
app := &application{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
|
models: data.NewModels(db),
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|||||||
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
|
package data
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
"gitea.local.lab/Lbenedar/greenlight/internal/validator"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type MovieModel struct {
|
||||||
|
DB *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
type Movie struct {
|
type Movie struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
CreatedAt time.Time `json:"-"`
|
CreatedAt time.Time `json:"-"`
|
||||||
@@ -16,6 +21,40 @@ type Movie struct {
|
|||||||
Version int32 `json:"version"`
|
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) {
|
func ValidateMovie(v *validator.Validator, movie *Movie) {
|
||||||
v.Check(movie.Title != "", "title", "must be provided")
|
v.Check(movie.Title != "", "title", "must be provided")
|
||||||
v.Check(len(movie.Title) <= 500, "title", "must not be more than 500 bytes long")
|
v.Check(len(movie.Title) <= 500, "title", "must not be more than 500 bytes long")
|
||||||
|
|||||||
Reference in New Issue
Block a user