usecases

package
v0.0.0-...-1f9e35f Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package usecases provides user usecases with application logic.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound    = errors.New("user not found")
	ErrUniqueEmail = errors.New("email already exists")
	ErrUniqueID    = errors.New("id already exists")
)
View Source
var (
	ErrUnauthorized         = errors.New("unauthorized")
	ErrAuthenticationFailed = errors.New("authentication failed")
)

Functions

This section is empty.

Types

type Config

type Config struct {
}

Config is used to configure UserUseCases.

type NewUser

type NewUser struct {
	Name            string   `json:"name" validate:"required"`
	Email           string   `json:"email" validate:"required,email"`
	Roles           []string `json:"roles" validate:"required"`
	Password        string   `json:"password" validate:"required"`
	PasswordConfirm string   `json:"password_confirm" validate:"eqfield=Password"`
}

NewUser contains information needed to create a new User.

type UpdateUser

type UpdateUser struct {
	Name            *string  `json:"name"`
	Email           *string  `json:"email" validate:"omitempty,email"`
	Roles           []string `json:"roles"`
	Password        *string  `json:"password"`
	PasswordConfirm *string  `json:"password_confirm" validate:"omitempty,eqfield=Password"`
}

UpdateUser defines what information may be provided to modify an existing User. All fields are optional so clients can send just the fields they want changed. It uses pointer fields so we can differentiate between a field that was not provided and a field that was provided as explicitly blank. Normally we do not want to use pointers to basic types but we make exceptions around marshalling/unmarshalling.

type UserRepository

type UserRepository interface {
	Create(context.Context, user.User) error
	Query(context.Context, int, int) ([]user.User, error)
	QueryByID(context.Context, string) (user.User, error)
	QueryByEmail(context.Context, string) (user.User, error)
	Update(context.Context, user.User) error
	Delete(context.Context, string) error
}

UserRepository is an interface which is to be implemented by the layer between user usecases and storages.

type UserUseCases

type UserUseCases struct {
	Log             *zap.SugaredLogger
	Repo            UserRepository
	SessionDuration time.Duration
}

UserUseCases contain application logic for user entities.

func New

New returns an initialized UserUseCases. Requires a logger, user repository and user session duration as input.

func (UserUseCases) Authenticate

func (uc UserUseCases) Authenticate(ctx context.Context, email, password string, now time.Time) (user.Session, error)

Authenticate returns a Session after succesfully authenticating a user by email and password.

func (UserUseCases) Create

func (uc UserUseCases) Create(ctx context.Context, n NewUser, now time.Time) (user.User, error)

Create inserts the provided user at the repository.

func (UserUseCases) Delete

func (uc UserUseCases) Delete(ctx context.Context, id string) error

Delete removes a user from the repository by its id.

func (UserUseCases) Query

func (uc UserUseCases) Query(ctx context.Context, pageNumber int, rowsPerPage int) ([]user.User, error)

Query retrieves all users from the repository,

func (UserUseCases) QueryByEmail

func (uc UserUseCases) QueryByEmail(ctx context.Context, email string) (user.User, error)

QueryByEmail retrieves a single user from the repository by its email address.

func (UserUseCases) QueryByID

func (uc UserUseCases) QueryByID(ctx context.Context, id string) (user.User, error)

QueryByID retrieves a single user from the repository by its id.

func (UserUseCases) Register

func (uc UserUseCases) Register(ctx context.Context, n NewUser, now time.Time) (user.User, error)

Register inserts the provided user at the repository. If there are no users yet, the user will get the ADMIN and USER role, in any other case the user will get only the USER role.

Unlike Create, Register requires no admin priviliges. It is meant to register the first admin of the system and user signups.

func (UserUseCases) Update

func (uc UserUseCases) Update(ctx context.Context, u user.User) error

Update updates the provided user at the repository.

Jump to

Keyboard shortcuts

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