utils

package
v0.0.0-...-43927cf Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const AdminRole = "admin"
View Source
const DbContextKey = ContextKey(1)
View Source
const DefaultOrderBy = "id"
View Source
const DefaultPageNumber = 1
View Source
const DefaultPageSize = 20
View Source
const OnlyCurrentUserContextKey = ContextKey(4)
View Source
const OnlyCurrentUserPath = "/users/me"
View Source
const OrganizerRole = "organizer"
View Source
const RecordIdContextKey = ContextKey(3)
View Source
const UserContextKey = ContextKey(0)
View Source
const UserRole = "user"
View Source
const WorkersContextKey = ContextKey(2)

Variables

This section is empty.

Functions

func DecodeAndValidate

func DecodeAndValidate[T DbModel](r *http.Request) (T, error)

func Encode

func Encode[T any](w http.ResponseWriter, r *http.Request, status int, v T) error

func IntPathValue

func IntPathValue(r *http.Request, key string) int

func IntQueryValue

func IntQueryValue(r *http.Request, key string) int

func RegisterValidations

func RegisterValidations(db *gorm.DB)

func Validate

func Validate(instance any) error

validates the instance of a struct using defined tags on its fields

Types

type ActionForbiddenError

type ActionForbiddenError struct {
	AppError
}

type AnyMap

type AnyMap map[string]any

type AppError

type AppError struct {
	Message string
}

func (AppError) Error

func (e AppError) Error() string

type AppValidationError

type AppValidationError struct {
	AppError
	FieldPath string
}

type AuthHeaderMissingError

type AuthHeaderMissingError struct {
	AppError
}

type ContextKey

type ContextKey uint

type DbModel

type DbModel interface {
	GetID() uint
	Validate(*http.Request) error
	BeforeCreateCtx(context.Context, *gorm.DB) error
	AfterCreateCtx(context.Context, *gorm.DB) error
	BeforeUpdateCtx(context.Context, *gorm.DB) error
	AfterUpdateCtx(context.Context, *gorm.DB) error
	BeforeDeleteCtx(context.Context, *gorm.DB) error
	AfterDeleteCtx(context.Context, *gorm.DB) error
	GetAllQuery(*gorm.DB, *http.Request) *gorm.DB
	GetQuery(*gorm.DB, *http.Request) *gorm.DB
	UpdateQuery(*gorm.DB, *http.Request) *gorm.DB
	DeleteQuery(*gorm.DB, *http.Request) *gorm.DB
	BuildResponse() any
}

type ErrorsResponse

type ErrorsResponse struct {
	ErrorType string `json:"error_type"`
	Details   any    `json:"details"` // should be of type: string, utils.AnyMap
}

type HandlerWithError

type HandlerWithError func(http.ResponseWriter, *http.Request) error

func (HandlerWithError) ServeHTTP

type InternalError

type InternalError struct {
	AppError
}

type InvalidAuthHeaderError

type InvalidAuthHeaderError struct {
	AppError
}

type InvalidJwtClaimsError

type InvalidJwtClaimsError struct {
	AppError
}

type InvalidUserError

type InvalidUserError struct {
	AppError
}

type JwtTokenExpiredError

type JwtTokenExpiredError struct {
	AppError
}

type JwtTokenParsingError

type JwtTokenParsingError struct {
	AppError
}

type LoginError

type LoginError struct {
	AppError
}

type PaginatedResponse

type PaginatedResponse struct {
	Data           interface{}    `json:"data"`
	PaginationInfo PaginationInfo `json:"pagination_info"`
}

func BuildPaginatedResponse

func BuildPaginatedResponse(responseRecords []any, totalCount int64, paginationParams *PaginationParams) *PaginatedResponse

type PaginationInfo

type PaginationInfo struct {
	Count            int  `json:"count"`
	TotalCount       int  `json:"total_count"`
	TotalPages       int  `json:"total_pages"`
	CurrentPage      int  `json:"current_page"`
	NextPage         *int `json:"next_page"`
	PrevPage         *int `json:"prev_page"`
	IsFirstPage      bool `json:"is_first_page"`
	IsLastPage       bool `json:"is_last_page"`
	IsOutOfRangePage bool `json:"is_out_of_range_page"`
}

type PaginationParams

type PaginationParams struct {
	PageNo         int
	PerPage        int
	OrderBy        string
	OrderDirection string
}

func BuildPaginationParams

func BuildPaginationParams(r *http.Request) *PaginationParams

type RecordNotFoundError

type RecordNotFoundError struct {
	AppError
}

type UserNotFoundError

type UserNotFoundError struct {
	AppError
}

type ValidationResponseItem

type ValidationResponseItem struct {
	ErrorsResponse
	Path []string `json:"path"`
}

func AppValidationErrorDetails

func AppValidationErrorDetails(err AppValidationError) []ValidationResponseItem

func ValidationErrorDetails

func ValidationErrorDetails(errors validator.ValidationErrors) []ValidationResponseItem

Jump to

Keyboard shortcuts

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