Documentation ¶
Overview ¶
Package usecases provides user usecases with application logic.
Index ¶
- Variables
- type Config
- type NewUser
- type UpdateUser
- type UserRepository
- type UserUseCases
- func (uc UserUseCases) Authenticate(ctx context.Context, email, password string, now time.Time) (user.Session, error)
- func (uc UserUseCases) Create(ctx context.Context, n NewUser, now time.Time) (user.User, error)
- func (uc UserUseCases) Delete(ctx context.Context, id string) error
- func (uc UserUseCases) Query(ctx context.Context, pageNumber int, rowsPerPage int) ([]user.User, error)
- func (uc UserUseCases) QueryByEmail(ctx context.Context, email string) (user.User, error)
- func (uc UserUseCases) QueryByID(ctx context.Context, id string) (user.User, error)
- func (uc UserUseCases) Register(ctx context.Context, n NewUser, now time.Time) (user.User, error)
- func (uc UserUseCases) Update(ctx context.Context, u user.User) error
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("user not found") ErrUniqueEmail = errors.New("email already exists") ErrUniqueID = errors.New("id already exists") )
var ( ErrAuthenticationFailed = errors.New("authentication failed") )
Functions ¶
This section is empty.
Types ¶
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 ¶
func New(log *zap.SugaredLogger, r UserRepository, s time.Duration) UserUseCases
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) 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 ¶
QueryByEmail retrieves a single user from the repository by its email address.
func (UserUseCases) Register ¶
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.