package data import ( "database/sql" "time" "greenlight.craftr.fr/internal/validator" ) // Movie /* Annotate the Movie struct with struct tags to control how the keys appear in the JSON-encoded output. */ type Movie struct { ID int64 `json:"id"` CreatedAt time.Time `json:"-"` Title string `json:"title"` Year int32 `json:"year,omitempty"` /* Use the Runtime type instead of int32. Note that the omitempty directive will still work on this: if the Runtime field has the underlying value 0, then it will be considered empty and omitted -- and the MarshalJSON() method we just made won't be called at all. */ Runtime Runtime `json:"runtime,omitempty"` Genres []string `json:"genres,omitempty"` Version int32 `json:"version"` } func ValidateMovie(v *validator.Validator, movie *Movie) { // Use the Check() method to execute our validation checks. This will add the provided key and error message to the errors maps if the check does not evaluate to true. For example, in the first line we "check that the title is not equal to an empty string". 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") // 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 }