domain

package
v0.0.0-...-a131ce6 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

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

Variables

View Source
var (
	ErrRecordNotFound     = errors.New("record not found")    // Record not found when we request some resource in database.
	ErrDuplicateEmail     = errors.New("duplicate email")     // Duplicate Email error.
	ErrEditConflict       = errors.New("edit conflict")       // Edit conflict while manipulating database.
	ErrInvalidCredentials = errors.New("invalid credentials") // Edit conflict while manipulating database.
	ErrFailedValidation   = errors.New("failed validation")   //  Failed validation error.
)
View Source
var AnonymousUser = &User{}

AnonymousUser represents an anonymous user.

Functions

func ValidateEmail

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

func ValidateFilters

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

ValidateFilters checks if the constraints are satisfied or not.

func ValidatePasswordPlaintext

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

func ValidateTask

func ValidateTask(v *validator.Validator, task *Task)

ValidateTask check if task match the constrains.

func ValidateTokenPlaintext

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

ValidateTokenPlaintext checks that the plaintext token has been provided and is exactly 26 bytes long.

func ValidateUser

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

Types

type Filters

type Filters struct {
	CurrentPage  int      // CurrentPage represents the current page client wants to see.
	PageSize     int      // PageSize represents the page size of each page.
	Sort         string   // Sort represent the property that data needs to be sorted by. E.g. if Sort == "id", the data is sorted by id.
	SortSafelist []string // SortSafelist field to hold the supported sort values.
}

Filters contains some properties about how client wants to view the data. including the page size, current page, the order of data, etc.

func (Filters) Limit

func (f Filters) Limit() int

Limit returns the size of each page.

func (Filters) Offset

func (f Filters) Offset() int

Offset returns the distance between first data and current data.

func (Filters) SortColumn

func (f Filters) SortColumn() string

SortColumn checks that the client-provided Sort field matches one of the entries in our safelist and if it does, extract the column name from the Sort field by stripping the leading hyphen character (if one exists).

func (Filters) SortDirection

func (f Filters) SortDirection() string

SortDirection returns the sort direction ("ASC" or "DESC") depending on the prefix character of the Sort field.

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

Metadata holds the pagination metadata.

func CalculateMetadata

func CalculateMetadata(totalRecords, page, pageSize int) Metadata

Calculate calculates the appropriate pagination metadata values given the total number of records, current page, and page size values. Note that the last page value is calculated using the math.Ceil() function, which rounds up a float to the nearest integer. So, for example, if there were 12 records in total and a page size of 5, the last page value would be math.Ceil(12/5) = 3.

type Password

type Password struct {
	Plaintext *string
	Hash      []byte
}

Password is a struct containing the plaintext and hashed versions of the password for a user. The plaintext field is a *pointer* to a string, so that we're able to distinguish between a plaintext password not being present in the struct at all, versus a plaintext password which is the empty string "".

func (*Password) Matches

func (p *Password) Matches(plaintextPassword string) (bool, error)

Matches checks whether the provided plaintext password matches the hashed password stored in the struct, returning true if it matches and false otherwise.

func (*Password) Set

func (p *Password) Set(plaintextPassword string) error

Set calculates the bcrypt hash of a plaintext password, and stores both the hash and the plaintext versions in the struct.

type Task

type Task struct {
	ID        int64     `json:"id"`      // Unique integer ID for the task
	UserID    int64     `json:"user_id"` // integer ID for the task owner
	CreatedAt time.Time `json:"-"`       // Timestamp for when the task is added to our database
	Title     string    `json:"title"`   // task title
	Content   string    `json:"content"` // task content
	Done      bool      `json:"done"`    // true if task is done
	Version   int32     `json:"version"` // The version number starts at 1 and will be incremented each

}

Task represent the data structure of our task object.

type TaskRepository

type TaskRepository interface {
	GetAll(ctx context.Context, userID int64, title string, filters Filters) ([]*Task, Metadata, error)
	GetByID(ctx context.Context, userID int64, taskID int64) (*Task, error)
	Insert(ctx context.Context, userID int64, task *Task) error
	Update(ctx context.Context, task *Task) error
	Delete(ctx context.Context, userID int64, taskID int64) error
}

type TaskUsecase

type TaskUsecase interface {
	GetAll(ctx context.Context, userID int64, title string, filters Filters) ([]*Task, Metadata, error)
	GetByID(ctx context.Context, userID int64, taskID int64) (*Task, error)
	Insert(ctx context.Context, userID int64, task *Task) error
	Update(ctx context.Context, task *Task) error
	Delete(ctx context.Context, userID int64, taskID int64) error
}

type Token

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

func GenerateToken

func GenerateToken(userID int64, ttl time.Duration, scope string) (*Token, error)

GenerateToken generates token based on userID, ttl (time-to-live), and scope.

type TokenRepository

type TokenRepository interface {
	Insert(ctx context.Context, token *Token) error
	DeleteAllForUser(ctx context.Context, scope string, userID int64) error
}

type TokenUsecase

type TokenUsecase interface {
	Insert(ctx context.Context, token *Token) error
	DeleteAllForUser(ctx context.Context, scope string, userID int64) 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:"-"`
}

User represents an individual user.

func (*User) IsAnonymous

func (u *User) IsAnonymous() bool

IsAnonymous checks if a User instance is the AnonymousUser.

type UserRepository

type UserRepository interface {
	Insert(ctx context.Context, user *User) error
	GetByEmail(ctx context.Context, email string) (*User, error)
	Update(ctx context.Context, user *User) error
	GetForToken(ctx context.Context, tokenScope, tokenPlaintext string) (*User, error)
}

type UserUsecase

type UserUsecase interface {
	Insert(ctx context.Context, user *User) error
	Update(ctx context.Context, user *User) error
	Register(ctx context.Context, user *User) error
	Activate(ctx context.Context, tokenPlaintext string) (*User, error)
	Login(ctx context.Context, email, password string) (*Token, error)
	Authenticate(ctx context.Context, tokenScope, tokenPlaintext string) (*User, error)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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