Documentation ¶
Index ¶
- Constants
- Variables
- func ValidateEmail(v *validator.Validator, email string)
- func ValidateFilters(v *validator.Validator, f Filters)
- func ValidatePasswordPlaintext(v *validator.Validator, password string)
- func ValidateTask(v *validator.Validator, task *Task)
- func ValidateTokenPlaintext(v *validator.Validator, tokenPlaintext string)
- func ValidateUser(v *validator.Validator, user *User)
- type Filters
- type Metadata
- type Password
- type Task
- type TaskRepository
- type TaskUsecase
- type Token
- type TokenRepository
- type TokenUsecase
- type User
- type UserRepository
- type UserUsecase
Constants ¶
const ( ScopeActivation = "activation" ScopeAuthentication = "authentication" )
Variables ¶
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. )
var AnonymousUser = &User{}
AnonymousUser represents an anonymous user.
Functions ¶
func ValidateEmail ¶
func ValidateFilters ¶
ValidateFilters checks if the constraints are satisfied or not.
func ValidateTask ¶
ValidateTask check if task match the constrains.
func ValidateTokenPlaintext ¶
ValidateTokenPlaintext checks that the plaintext token has been provided and is exactly 26 bytes long.
func ValidateUser ¶
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) SortColumn ¶
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 ¶
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 ¶
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 ¶
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 "".
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 TokenRepository ¶
type TokenUsecase ¶
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 ¶
IsAnonymous checks if a User instance is the AnonymousUser.
type UserRepository ¶
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) }