utils

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

internal/utils/tx.go

Index

Constants

This section is empty.

Variables

View Source
var ErrNoTransaction = &NoTransactionError{}

ErrNoTransaction thông báo khi context không chứa transaction

View Source
var KeyReq keyRequest

Functions

func GenerateSlug added in v1.0.2

func GenerateSlug(input string) string

func GetTxFromContext added in v1.0.1

func GetTxFromContext(ctx context.Context) (*gorm.DB, error)

GetTxFromContext lấy transaction từ context (dùng trong repository)

func RandomStr

func RandomStr() (string, error)

func SaveRequestContext

func SaveRequestContext(cxt context.Context, r RequestResponse) context.Context

Types

type Hash

type Hash struct{}

func (*Hash) CompareHashAndPassword

func (h *Hash) CompareHashAndPassword(passwordStr string, password string, salt string) bool

func (*Hash) GenerateFromPassword

func (h *Hash) GenerateFromPassword(password string, salt string) (string, error)

type JwtClaims

type JwtClaims struct {
	Role string `json:"role"`
	jwt.RegisteredClaims
}

type JwtService

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

func NewJwtServer

func NewJwtServer(secretKey string, expireAt int) *JwtService

func (*JwtService) IssueToken

func (j *JwtService) IssueToken(ctx context.Context, sub string, tid string, role Role) (*TokenResponse, error)

func (JwtService) ParseToken

func (j JwtService) ParseToken(ctx context.Context, tokenStr string) (*JwtClaims, error)

type NoTransactionError added in v1.0.1

type NoTransactionError struct{}

func (*NoTransactionError) Error added in v1.0.1

func (e *NoTransactionError) Error() string

type PageIndex

type PageIndex struct {
	Limit  int
	Offset int
}

type RequestResponse

type RequestResponse struct {
	Sub string
	Tid string
}

func GetRequestContext

func GetRequestContext(cxt context.Context) RequestResponse

func NewRequestResponse

func NewRequestResponse(sub string, tid string) *RequestResponse

type Role

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

type Token

type Token struct {
	Token    string `json:"token"`
	ExpireAt int    `json:"expire_at"`
}

type TokenResponse

type TokenResponse struct {
	AccessToken Token  `json:"access_token"`
	RefeshToken *Token `json:"refesh_token"`
}

type TransactionError added in v1.0.1

type TransactionError struct {
	OriginalError error
	RollbackError error
}

TransactionError wrap lỗi rollback và lỗi chính

func (*TransactionError) Error added in v1.0.1

func (e *TransactionError) Error() string

type TxManager added in v1.0.1

type TxManager interface {
	WithinTransaction(ctx context.Context, fn func(ctx context.Context) error) error
}

TxManager là interface cho transaction (để dễ mock khi test)

func NewTxManager added in v1.0.1

func NewTxManager(db *gorm.DB) TxManager

NewTxManager khởi tạo tx manager với db connection

type UID

type UID struct {
	LocalID  uint32
	ObjectID uint
}

func DecodeFromBase58

func DecodeFromBase58(fakeUID string) *UID

func NewUID

func NewUID(localID uint32, objectID uint) *UID

func (*UID) ToBase58

func (u *UID) ToBase58() string

Jump to

Keyboard shortcuts

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