user

package
v0.0.0-...-32533e6 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidRole = errors.New("role must be one of: admin, viewer")

Functions

func CreateParamsFromRequest

func CreateParamsFromRequest(req gen.CreateUserRequest) (username, password, role string)

CreateParamsFromRequest translates a generated CreateUserRequest into the primitive parameters Service.Create expects. This is not a pure 1:1 mapper — it also applies the API-level default role ("viewer") when the client omits it.

func ToResponse

func ToResponse(u *User) gen.User

ToResponse converts a domain User into the generated API model. Callers must guarantee u != nil — a nil User means "not found", which callers are expected to translate into 404 before calling ToResponse. This function panics on nil rather than silently returning an empty User, so a caller bug surfaces immediately instead of leaking as `{}` in a response.

PasswordHash is intentionally never mapped — gen.User has no such field.

func ToResponseList

func ToResponseList(users []*User) []gen.User

ToResponseList converts a slice of domain Users into generated API models.

Types

type Handler

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

func NewHandler

func NewHandler(service *Service) *Handler

func (*Handler) Create

func (h *Handler) Create(w http.ResponseWriter, r *http.Request)

func (*Handler) Delete

func (h *Handler) Delete(w http.ResponseWriter, r *http.Request)

func (*Handler) List

func (h *Handler) List(w http.ResponseWriter, r *http.Request)

type PostgresRepository

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

func NewPostgresRepository

func NewPostgresRepository(db *pgxpool.Pool) *PostgresRepository

func (*PostgresRepository) Create

func (r *PostgresRepository) Create(ctx context.Context, u *User) (*User, error)

func (*PostgresRepository) Delete

func (r *PostgresRepository) Delete(ctx context.Context, id uuid.UUID) error

func (*PostgresRepository) GetByID

func (r *PostgresRepository) GetByID(ctx context.Context, id uuid.UUID) (*User, error)

func (*PostgresRepository) GetByUsername

func (r *PostgresRepository) GetByUsername(ctx context.Context, username string) (*User, error)

func (*PostgresRepository) List

func (r *PostgresRepository) List(ctx context.Context) ([]*User, error)

type Repository

type Repository interface {
	Create(ctx context.Context, u *User) (*User, error)
	GetByUsername(ctx context.Context, username string) (*User, error)
	GetByID(ctx context.Context, id uuid.UUID) (*User, error)
	List(ctx context.Context) ([]*User, error)
	Delete(ctx context.Context, id uuid.UUID) error
}

type Service

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

func NewService

func NewService(repo Repository) *Service

func (*Service) Create

func (s *Service) Create(ctx context.Context, username, password, role string) (*User, error)

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, id uuid.UUID) error

func (*Service) GetByID

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

func (*Service) List

func (s *Service) List(ctx context.Context) ([]*User, error)

type User

type User struct {
	ID           uuid.UUID `json:"id"`
	Username     string    `json:"username"`
	PasswordHash string    `json:"-"` //không bao giờ xuất hiện trong response JSON trả về client, dù có vô tình json.Encode(user)
	Role         string    `json:"role"`
	CreatedAt    time.Time `json:"created_at"`
}

Jump to

Keyboard shortcuts

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