data

package
v0.0.0-...-edef37a Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ScopeActivation     = "activation"
	ScopeAuthentication = "authentication"
	ScopePasswordReset  = "password-reset"
)

Constants for each kind of token scope.

Variables

View Source
var (
	ErrRecordNotFound = errors.New("record not found")
	ErrEditConflict   = errors.New("edit conflict")
)
View Source
var AnonymousUser = &User{}

A user is considered anonymous if no Authorization header is provided with their request.

View Source
var (
	ErrDuplicateEmail = errors.New("duplicate email")
)
View Source
var ErrInvalidRuntimeValue = errors.New("invalid runtime value")

Functions

func ValidateEmail

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

func ValidateFilters

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

Runs validation checks on the filter parameters provided by the client.

func ValidateMovie

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

func ValidatePasswordPlaintext

func ValidatePasswordPlaintext(v *validator.Validator, plaintextPassword string)

func ValidateTokenPlaintext

func ValidateTokenPlaintext(v *validator.Validator, tokenPlaintext string)

func ValidateUser

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

Types

type Filters

type Filters struct {
	Page         int      // Pagination page number
	PageSize     int      // Number of records per page
	Sort         string   // Field name to sort by
	SortSafeList []string // List of allowed field names to sort by
}

type Metadata

type Metadata struct {
	CurrentPage  int `json:"currentPage,omitempty"`
	PageSize     int `json:"pageSize,omitempty"`
	FirstPage    int `json:"firstPage,omitempty"`
	LastPage     int `json:"lastPage,omitempty"`
	TotalRecords int `json:"totalRecords,omitempty"`
}

type Models

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

Holds all application db models.

func NewModels

func NewModels(db *sql.DB) Models

Simple helper to initialize all db models with the provided db connection.

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

Represents a movie table in the database.

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) GetAll

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

func (MovieModel) Insert

func (m MovieModel) Insert(movie *Movie) 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

func (PermissionModel) GetAllForUser

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

type Permissions

type Permissions []string

func (Permissions) Include

func (p Permissions) Include(code string) bool

type Runtime

type Runtime int32

func (Runtime) MarshalJSON

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

Implementation of json.Marshaler interface.

Encodes a Runtime of type int32 in the format of "<runtime> mins".

func (*Runtime) UnmarshalJSON

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

Implementation of json.Unmarshaler interface.

Decodes a Runtime of format "<runtime> mins" into an int32 Runtime value.

type Token

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

type TokenModel

type TokenModel struct {
	DB *sql.DB
}

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

Adds a new token to the tokens table.

func (TokenModel) New

func (m TokenModel) New(userID int64, ttl time.Duration, scope string) (*Token, error)

Helper to generate a new token based on the provided arguments and insert it into the database.

type User

type User struct {
	ID        int64     `json:"id"`
	CreatedAt time.Time `json:"createdAt"`
	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)

func (UserModel) GetForToken

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

func (UserModel) Insert

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

func (UserModel) Update

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

Jump to

Keyboard shortcuts

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