Documentation
¶
Index ¶
- Variables
- func ValidateEmail(v *validator.Validator, email string)
- func ValidateFilters(v *validator.Validator, f Filters)
- func ValidateMovie(v *validator.Validator, movie *Movie)
- func ValidatePasswordPlaintext(v *validator.Validator, password string)
- func ValidateUser(v *validator.Validator, user *User)
- type Filters
- type Metadata
- type Models
- type Movie
- type MovieModel
- type Runtime
- type User
- type UserModel
Constants ¶
This section is empty.
Variables ¶
var ( ErrRecordNotFound = errors.New("record not found") ErrEditConflict = errors.New("edit conflict") )
var (
ErrDuplicateEmail = errors.New("duplicate email")
)
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")
Define an error that our UnmarshalJSON() method can return if we're unable to parse or convert the JSON string successfully.
Functions ¶
func ValidateEmail ¶
func ValidateFilters ¶
func ValidateMovie ¶
func ValidateUser ¶
Types ¶
type Metadata ¶
type Metadata struct {
CurrentPage int `json:"current_page,omitempty"`
PageSize int `json:"page_size,omitempty"`
FirstPage int `json:"first_page,omitempty"`
LastPage int `json:"last_page,omitempty"`
TotalRecords int `json:"total_records,omitempty"`
}
Define a new Metadata struct for holding the pagination metadata.
type Models ¶
type Models struct {
Movies MovieModel
Users UserModel
}
type Movie ¶
type Movie struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"-"` // Use the directive to hide fields
Title string `json:"title"`
// 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.
Year int32 `json:"year,omitempty"`
Runtime Runtime `json:"runtime,omitempty"`
Genres []string `json:"genres,omitempty"`
Version int32 `json:"version"`
}
type MovieModel ¶
func (MovieModel) Delete ¶
func (m MovieModel) Delete(id int64) error
func (MovieModel) GetAll ¶
func (m MovieModel) GetAll(title string, genres []string, filters Filters) ([]*Movie, Metadata, error)
Create a new GetAll() method which returns a slice of movies. Although we're not using them right now, we've set this up to accept the various filter parameters as arguments.
func (MovieModel) Insert ¶
func (m MovieModel) Insert(movie *Movie) error
The Insert() method accepts a pointer to a movie struct, which should contain the data for the new record.
func (MovieModel) Update ¶
func (m MovieModel) Update(movie *Movie) error
type Runtime ¶
type Runtime int32
func (Runtime) MarshalJSON ¶
Implement a MarshalJSON() method on the Runtime type so that it satisfies the json.Marshaler interface. This should return the JSON-encoded value for the movie runtime (in our case, it will return a string in the format "<runtime> mins").
func (*Runtime) UnmarshalJSON ¶
Implement a UnmarshalJSON() method on the Runtime type so that it satisfies the json.Unmarshaler interface. IMPORTANT: Because UnmarshalJSON() needs to modify the receiver (our Runtime type), we must use a pointer receiver for this to work correctly. Otherwise, we will only be modifying a copy (which is then discarded when this method returns).
type User ¶
type User struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at"`
Name string `json:"name"`
Email string `json:"email"`
Password password `json:"-"`
Activated bool `json:"activated"`
Version int `json:"-"`
}
Define a User struct to represent an individual user. Importantly, notice how we are using the json:"-" struct tag to prevent the Password and Version fields appearing in any output when we encode it to JSON. Also notice that the Password field uses the custom password type defined below.
type UserModel ¶
func (UserModel) GetByEmail ¶
Retrieve the User details from the database based on the user's email address. Because we have a UNIQUE constraint on the email column, this SQL query will only return one record (or none at all, in which case we return a ErrRecordNotFound error).
func (UserModel) Insert ¶
Insert a new record in the database for the user. Note that the id, created_at and version fields are all automatically generated by our database, so we use the RETURNING clause to read them into the User struct after the insert, in the same way that we did when creating a movie.
func (UserModel) Update ¶
Update the details for a specific user. Notice that we check against the version field to help prevent any race conditions during the request cycle, just like we did when updating a movie. And we also check for a violation of the "users_email_key" constraint when performing the update, just like we did when inserting the user record originally.