Documentation
¶
Index ¶
- Constants
- 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 ValidateTokenPlaintext(v *validator.Validator, tokenPlaintext string)
- func ValidateUser(v *validator.Validator, user *User)
- type Filters
- type Metadata
- type Models
- type Movie
- type MovieModel
- type PermissionModel
- type Permissions
- type Runtime
- type Token
- type TokenModel
- type User
- type UserModel
Constants ¶
const ( ScopeActivation = "activation" ScopeAuthentication = "authentication" )
Constants for the token scope.
const (
UniqueConstraintViolation = "23505"
)
Variables ¶
var ( ErrRecordNotFound = errors.New("record not found") ErrEditConflict = errors.New("edit conflict") )
var AnonymousUser = &User{}
var (
ErrDuplicateEmail = errors.New("duplicate email")
)
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")
Functions ¶
func ValidateEmail ¶
func ValidateFilters ¶
func ValidateMovie ¶
func ValidateTokenPlaintext ¶
Check that the plaintext token has been provided and is exactly 26 bytes long.
func ValidateUser ¶
Types ¶
type Models ¶
type Models struct {
Movies MovieModel
Users UserModel
Tokens TokenModel
Permissions PermissionModel
}
type Movie ¶
type Movie struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"-"`
Title string `json:"title"`
Year int32 `json:"year,omitempty"`
Runtime Runtime `json:"runtime,omitempty"` // Movie runtime (in minutes)
Genres []string `json:"genres,omitempty"`
Version int32 `json:"version"` // Starts at 1 and will be incremented with each update
}
type MovieModel ¶
A MovieModel struct type which wraps a connection pool.
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.
func (MovieModel) Insert ¶
func (m MovieModel) Insert(movie *Movie) error
func (MovieModel) Update ¶
func (m MovieModel) Update(movie *Movie) error
type PermissionModel ¶
A PermissionModel struct type which wraps a connection pool.
func (PermissionModel) AddForUser ¶
func (m PermissionModel) AddForUser(userID int64, codes ...string) error
Add the provided permission codes for a specific user.
func (PermissionModel) GetAllForUser ¶
func (m PermissionModel) GetAllForUser(userID int64) (Permissions, error)
Returns all permission codes for a specific user in a Permissions slice.
type Permissions ¶
type Permissions []string
Slice to hold the permission codes like "movies:read" and "movies:write" for a single user.
func (Permissions) Include ¶
func (p Permissions) Include(code string) bool
Method to check whether the Permissions slice contains a specific permission code.
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 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 Token ¶
type Token struct {
Plaintext string `json:"token"`
Hash []byte `json:"-"`
UserID int64 `json:"-"`
Expiry time.Time `json:"expiry"`
Scope string `json:"-"`
}
A Token struct to hold the data for an individual token.
type TokenModel ¶
A TokenModel struct type which wraps a connection pool.
func (TokenModel) DeleteAllForUser ¶
func (m TokenModel) DeleteAllForUser(scope string, userID int64) error
Deletes all tokens for a specific user and scope.
func (TokenModel) Insert ¶
func (m TokenModel) Insert(token *Token) error
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:"-"`
}
func (*User) IsAnonymous ¶
Check if a User instance is the AnonymousUser.
type UserModel ¶
A UserModel struct type which wraps a connection pool.
func (UserModel) GetByEmail ¶
Retrieve the User details from the database based on the user's email address.