Files
go-playground/projects/greenlight/internal/data/movies.go
T

35 lines
1.3 KiB
Go

package data
import (
"time"
"greenlight.debuggingjon.dev/internal/validator"
)
type Movie struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"-"` // Use the - directive
Title string `json:"title"`
Year int32 `json:"year,omitempty"` // Add the omitempty directive
Runtime Runtime `json:"runtime,omitempty"` // Add the omitempty directive
Genres []string `json:"genres,omitempty"` // Add the omitempty directive
Version int32 `json:"version"`
}
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")
v.Check(movie.Year != 0, "year", "must be provided")
v.Check(movie.Year >= 1888, "year", "must be greater than 1888")
v.Check(movie.Year <= int32(time.Now().Year()), "year", "must not be in the future")
v.Check(movie.Runtime != 0, "runtime", "must be provided")
v.Check(movie.Runtime > 0, "runtime", "must be a positive integer")
v.Check(movie.Genres != nil, "genres", "must be provided")
v.Check(len(movie.Genres) >= 1, "genres", "must contain at least 1 genre")
v.Check(len(movie.Genres) <= 5, "genres", "must not contain more than 5 genres")
v.Check(validator.Unique(movie.Genres), "genres", "must not contain duplicate values")
}