go4rest

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

controllers/generic/controller.go

Index

Constants

This section is empty.

Variables

View Source
var ErrPermissionDenied = errors.New("permission denied")
View Source
var ErrUnauthenticated = errors.New("authentication required")

Functions

func ApplyFilters

func ApplyFilters(db *gorm.DB, filters []FilterOptions) *gorm.DB

func ApplySorting added in v0.1.10

func ApplySorting(db *gorm.DB, sorts []SortOptions) *gorm.DB

func RegisterCRUDRoutes added in v0.1.14

func RegisterCRUDRoutes(r *gin.Engine, path string, controller Crudable) *gin.RouterGroup

func ToSnakeCase added in v0.1.12

func ToSnakeCase(str string) string

Types

type Authorizer added in v0.2.0

type Authorizer interface {
	Authorize(*gin.Context, string, string) error
	Scope(*gin.Context, *gorm.DB, string, string) (*gorm.DB, error)
}

Authorizer restricts queries before fetching records or counting pagination. Schema requires model-wide view permission; create requires model-wide add.

type Crudable added in v0.1.14

type Crudable interface {
	List(*gin.Context)
	Create(*gin.Context)
	GetByID(*gin.Context)
	Update(*gin.Context)
	Delete(*gin.Context)
	Schema(*gin.Context)
	Full(*gin.Context)
}

type FilterOptions

type FilterOptions struct {
	Field    string
	Value    interface{}
	Operator string // "eq", "ne", "gt", "lt", "gte", "lte", "like"
}

type Model

type Model interface {
	~struct{} // Любая структура
}

type ObjectPermissionCleaner added in v0.2.0

type ObjectPermissionCleaner interface {
	DeleteObjectPermissions(*gorm.DB, string, string) error
}

ObjectPermissionCleaner removes grants when an object is permanently deleted. The supplied DB is the same transaction that deleted the object.

type PaginatedResponse

type PaginatedResponse struct {
	Results    interface{} `json:"results"`
	Page       int         `json:"page"`
	TotalPages int         `json:"total_pages"`
	Count      int         `json:"count"`
}

type Pagination

type Pagination struct {
	Page       int
	PerPage    int
	TotalPages int
	Count      int
}

func NewPaginator

func NewPaginator(ctx *gin.Context) Pagination

func (*Pagination) GetResponse

func (p *Pagination) GetResponse(data interface{}) PaginatedResponse

func (*Pagination) PaginatedQueryset

func (p *Pagination) PaginatedQueryset(query *gorm.DB) *gorm.DB

type QueryOptions added in v0.1.10

type QueryOptions struct {
	Filters []FilterOptions
	Sorts   []SortOptions
}

func ParseQueryParams

func ParseQueryParams(params url.Values) QueryOptions

ParseQueryParams преобразует query-параметры в фильтры

type SortOptions added in v0.1.10

type SortOptions struct {
	Field string
	Order string // "asc", "desc"
}

type ViewSet

type ViewSet[T any] struct {
	PreloadField    []string
	Authorizer      Authorizer
	PermissionModel string
	// contains filtered or unexported fields
}

func NewViewSet

func NewViewSet[T any](db *gorm.DB, options ...ViewSetOption) *ViewSet[T]

NewViewSet creates a public ViewSet unless authorization is enabled by an option. Options run during construction; initialization errors cause a panic.

func (*ViewSet[T]) Create

func (c *ViewSet[T]) Create(ctx *gin.Context)

Create создает новую запись

func (*ViewSet[T]) Delete

func (c *ViewSet[T]) Delete(ctx *gin.Context)

Delete удаляет запись

func (*ViewSet[T]) Full added in v0.1.5

func (c *ViewSet[T]) Full(ctx *gin.Context)

Full возвращает список записей без учета пагинации

func (*ViewSet[T]) GetByID

func (c *ViewSet[T]) GetByID(ctx *gin.Context)

GetByID возвращает запись по ID

func (*ViewSet[T]) GetQueryset

func (c *ViewSet[T]) GetQueryset(query *gorm.DB) *gorm.DB

func (*ViewSet[T]) List

func (c *ViewSet[T]) List(ctx *gin.Context)

List возвращает список записей с пагинацией

func (*ViewSet[T]) Schema added in v0.1.4

func (c *ViewSet[T]) Schema(ctx *gin.Context)

Schema возвращает запись по ID

func (*ViewSet[T]) Update

func (c *ViewSet[T]) Update(ctx *gin.Context)

Update обновляет запись

type ViewSetConfig added in v0.2.0

type ViewSetConfig struct {
	Authorizer      Authorizer
	PermissionModel string
}

ViewSetConfig contains the settings shared by ViewSets of all model types.

type ViewSetOption added in v0.2.0

type ViewSetOption func(*gorm.DB, *ViewSetConfig) error

ViewSetOption configures a ViewSet during construction. An initialization error stops construction with a panic, rather than leaving a public ViewSet.

Directories

Path Synopsis
cmd
go4rest command
Package utils provides the project and application scaffolds used by the CLI.
Package utils provides the project and application scaffolds used by the CLI.

Jump to

Keyboard shortcuts

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