data

package
v0.0.0-...-ba76496 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2021 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation     = "activation"
	ScopeAuthentication = "authentication"
)

Variables

View Source
var (
	ErrRecordNotFound = errors.New("record not found")
	ErrEditConflict   = errors.New("edit conflict")
	ErrDuplicateEmail = errors.New("duplicate email")
)
View Source
var AnonymousUser = &User{}
View Source
var ErrInvalidRuntimeFormat = errors.New("invalid runtime format")

ErrInvalidRuntimeFormat is an error that our UnmarshalJSON() method can return if we're unable to parse or convert the JSON string successfully.

Functions

func ValidateEmail

func ValidateEmail(v *validator.Validator, email string)

func ValidateFilters

func ValidateFilters(v *validator.Validator, f Filters)

func ValidateMovie

func ValidateMovie(v *validator.Validator, movie *Movie)

func ValidatePassword

func ValidatePassword(v *validator.Validator, password string)

func ValidatePlainTextToken

func ValidatePlainTextToken(v *validator.Validator, plainTextToken string)

ValidatePlainTextToken() checks that the plaintext token is provided and is exactly 52 bytes long

func ValidateUser

func ValidateUser(v *validator.Validator, user *User)

Types

type Filters

type Filters struct {
	Page         int
	PageSize     int
	Sort         string
	SortSafelist []string
}

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"`
}

type Models

type Models struct {
	Movies      MovieModel
	Users       UserModel
	Tokens      TokenModel
	Permissions PermissionModel
}

func New

func New(db *sql.DB) Models

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"`
	Genres    []string  `json:"genres,omitempty"`
	Version   int32     `json:"version"`
}

type MovieModel

type MovieModel struct {
	DB *sql.DB
}

func (MovieModel) Delete

func (m MovieModel) Delete(id int64) error

func (MovieModel) Get

func (m MovieModel) Get(id int64) (*Movie, error)

func (MovieModel) Insert

func (m MovieModel) Insert(movie *Movie) error

func (MovieModel) List

func (m MovieModel) List(title string, genres []string, filters Filters) ([]*Movie, Metadata, error)

func (MovieModel) Update

func (m MovieModel) Update(movie *Movie) error

type PermissionModel

type PermissionModel struct {
	DB *sql.DB
}

func (PermissionModel) AddForUser

func (m PermissionModel) AddForUser(userID int64, codes ...string) error

AddForUser() adds permisssion codes to a specific user

func (PermissionModel) GetAllForUser

func (m PermissionModel) GetAllForUser(userID int64) (Permissions, error)

type Permissions

type Permissions []string

Permissions type holds the permission files from the db

func (Permissions) Include

func (p Permissions) Include(code string) bool

type Runtime

type Runtime int32

func (Runtime) MarshalJSON

func (r Runtime) MarshalJSON() ([]byte, error)

func (*Runtime) UnmarshalJSON

func (r *Runtime) UnmarshalJSON(jsonValue []byte) error

type Token

type Token struct {
	PlainText string    `json:"token"`
	Hash      []byte    `json:"-"`
	UserID    int64     `json:"-"`
	Expiry    time.Time `json:"expiry"`
	Scope     string    `json:"-"`
}

Token type holds the data for an individual token. JSON tags control how the type fields appear when encoded into JSON

type TokenModel

type TokenModel struct {
	DB *sql.DB
}

func (TokenModel) DeleteAllForUser

func (m TokenModel) DeleteAllForUser(userID int64, scope string) error

func (TokenModel) Insert

func (m TokenModel) Insert(token *Token) error

Insert() adds the data for a specific token to the tokens table.

func (TokenModel) New

func (m TokenModel) New(userID int64, timeToLive time.Duration, scope string) (*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

func (u *User) IsAnonymous() bool

type UserModel

type UserModel struct {
	DB *sql.DB
}

func (UserModel) GetByEmail

func (m UserModel) GetByEmail(email string) (*User, error)

GetByEmail retrieves 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) GetForToken

func (m UserModel) GetForToken(plainTextToken, scope string) (*User, error)

func (UserModel) Insert

func (m UserModel) Insert(user *User) error

func (UserModel) Update

func (m UserModel) Update(user *User) error

Update the details for a specific user. Notice that we check against the version field to help prevent any race conditions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL