users

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package users contains the MVP user-management module.

The module covers:

  • registration with email/password,
  • login validation with account status checks,
  • admin user listing and status management,
  • password change endpoint.

Index

Constants

View Source
const (
	DefaultLimit = 20
	MaxLimit     = 100
)

Variables

View Source
var (
	ErrNotFound          = errors.New("user not found")
	ErrEmailAlreadyUsed  = errors.New("email already used")
	ErrInvalidCredential = errors.New("invalid credentials")
	ErrNotActive         = errors.New("user is not active")
	ErrEmptyQuery        = errors.New("empty query")
)

Functions

func IsValidRole

func IsValidRole(r Role) bool

IsValidRole checks if the role is one of the allowed values.

func IsValidStatus

func IsValidStatus(s Status) bool

IsValidStatus checks if the status is one of the allowed values.

func Module

func Module() fx.Option

Types

type Pagination

type Pagination = db.Pagination[*pagination]

func NewPagination

func NewPagination(limit, offset int) *Pagination

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository(db *bun.DB) *Repository

func (*Repository) Create

func (r *Repository) Create(ctx context.Context, input UserInput, passwordHash string) (*User, error)

func (*Repository) GetByEmail

func (r *Repository) GetByEmail(ctx context.Context, email string) (*UserWithPasswordHash, error)

func (*Repository) GetByID

func (r *Repository) GetByID(ctx context.Context, id int64) (*UserWithPasswordHash, error)

func (*Repository) GetByIDs

func (r *Repository) GetByIDs(ctx context.Context, ids []int64) (map[int64]User, error)

GetByIDs retrieves multiple users by their IDs in a single query. Returns a map of user ID to User. Users that are not found are omitted from the map.

func (*Repository) IsAdminByID

func (r *Repository) IsAdminByID(ctx context.Context, id int64) (bool, error)

func (*Repository) List

func (r *Repository) List(
	ctx context.Context,
	status *Status,
	role *Role,
	pagination *Pagination,
) ([]User, int, error)

func (*Repository) Search

func (r *Repository) Search(
	ctx context.Context,
	q string,
	pagination *Pagination,
) ([]User, int, error)

func (*Repository) Update

func (r *Repository) Update(ctx context.Context, id int64, update UserUpdate) error

func (*Repository) UpdatePasswordHash

func (r *Repository) UpdatePasswordHash(ctx context.Context, id int64, passwordHash string) error

type Role

type Role string
const (
	RoleAdmin Role = "admin"
	RoleUser  Role = "user"
)

type Service

type Service struct {
	// contains filtered or unexported fields
}

func NewService

func NewService(repo *Repository) *Service

func (*Service) ChangePassword

func (s *Service) ChangePassword(ctx context.Context, id int64, oldPassword, newPassword string) error

func (*Service) GetByID

func (s *Service) GetByID(ctx context.Context, id int64) (*User, error)

func (*Service) IsAdmin

func (s *Service) IsAdmin(ctx context.Context, id int64) (bool, error)

func (*Service) List

func (s *Service) List(ctx context.Context, status *Status, role *Role, pagination *Pagination) ([]User, int, error)

func (*Service) Login

func (s *Service) Login(ctx context.Context, email, password string) (*User, error)

func (*Service) LookupByIDs

func (s *Service) LookupByIDs(ctx context.Context, ids []int64) (map[int64]User, error)

LookupByIDs retrieves multiple users by their IDs in a single query. Returns a map of user ID to User. Users that are not found are omitted from the map.

func (*Service) Register

func (s *Service) Register(ctx context.Context, input UserInput) (*User, error)

func (*Service) Search

func (s *Service) Search(ctx context.Context, q string, pagination *Pagination) ([]User, int, error)

func (*Service) Update

func (s *Service) Update(ctx context.Context, id int64, update UserUpdate) error

type Status

type Status string
const (
	StatusPending Status = "pending"
	StatusActive  Status = "active"
	StatusBlocked Status = "blocked"
)

type User

type User struct {
	ID        int64
	Email     string
	Name      string
	Role      Role
	Status    Status
	CreatedAt time.Time
	UpdatedAt time.Time
}

User is the domain entity used by service and handler layers.

type UserInput

type UserInput struct {
	Email    string
	Password string
	Role     Role
}

type UserUpdate

type UserUpdate struct {
	Status *Status
	Role   *Role
}

func (UserUpdate) IsEmpty

func (u UserUpdate) IsEmpty() bool

type UserWithPasswordHash

type UserWithPasswordHash struct {
	User

	PasswordHash string
}

Jump to

Keyboard shortcuts

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