controller

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ByModel = map[string]CRUDSController{}
View Source
var ByName = map[string]CRUDSController{}
View Source
var EnableRecover bool

Functions

func AbortIfError

func AbortIfError(c *gin.Context, err error) bool

func AbortWithError

func AbortWithError(c *gin.Context, err error)

func CheckModelPermissions

func CheckModelPermissions(c *gin.Context, modelVal reflect.Value, modelSchema *schema.Schema, cache map[string]struct{}, checkDelete bool) message.Message

func CheckQueryRelation

func CheckQueryRelation(relName string, query *gorm.DB) []string

func CheckRelatedModel

func CheckRelatedModel(db *gorm.DB, model any, label string, field string, where string, whereArgs ...any) []string

func CheckRelations

func CheckRelations(c *gin.Context, db *gorm.DB, id interface{}, relations ...Relation) message.Message

CheckRelations checks if the resource can be deleted. This method verifies whether the resource to be deleted has been used in any other user-provided relationships. If the resource is in use, it returns the controllers that use the resource; otherwise, it returns nothing in case of success.

func CheckUnique

func CheckUnique(c *gin.Context, db *gorm.DB, model interface{}, primary string, fields []string) message.Message

CheckUnique performs a uniqueness check on a specific resource.

func Count

func Count(count *int64) func(*gorm.DB) *gorm.DB

func CreateModel

func CreateModel(c *gin.Context, info *ModelInfo, slice bool) (reflect.Type, message.Message)

func CreateToDb

func CreateToDb(c *gin.Context, db *gorm.DB, model interface{}, args ...string)

func DeleteFile

func DeleteFile(pathFunc, nameFunc func(*gin.Context) string) func(*gin.Context)

func DeleteFromDb

func DeleteFromDb(c *gin.Context, models []any)

func DeleteModels

func DeleteModels(db *gorm.DB, models interface{}) (err error)

func DeleteRelations

func DeleteRelations(c *gin.Context, db *gorm.DB, modelVal reflect.Value, modelSchema *schema.Schema) error

func DynamicQuery

func DynamicQuery(c *gin.Context, info *ModelInfo, conditions *params.Conditions) func(*gorm.DB) *gorm.DB

func ErrorsToMsg

func ErrorsToMsg(c *gin.Context, errors []string) message.Message

func ExposeSQLErr

func ExposeSQLErr(c *gin.Context, err error) error

func FieldToString

func FieldToString(c *gin.Context, field *schema.Field) string

func FileSystem

func FileSystem(ctrl AddRouter, apiPath string, filePath func(c *gin.Context) string, permissions FileSystemPermissions)

func FirstLower

func FirstLower(str string) string

func Folder

func Folder(pathFunc func(*gin.Context) string) func(*gin.Context)

func GetFile

func GetFile(pathFunc, nameFunc func(*gin.Context) string) func(*gin.Context)

func GetLimit

func GetLimit(pagStart, pagEnd string) int

func GetMapKeys

func GetMapKeys(mapToFlatten map[string]interface{}) []string

func GetModelInfo

func GetModelInfo(c *gin.Context, modelSchema *schema.Schema, selects string, computedFields map[string]string, modelInfo *ModelInfo, args *QueryMapArgs) message.Message

func GetOffset

func GetOffset(pagStart string) int

func GetPathParams

func GetPathParams(c *gin.Context, model interface{}, fields []string, destination interface{})

func GetPrimary

func GetPrimary(model interface{}) (int, error)

func GetPrimaryFields

func GetPrimaryFields(modelType reflect.Type) []string

func GetPrimaryName

func GetPrimaryName(model interface{}) (string, error)

func GetRelations

func GetRelations(c *gin.Context) []string

func HandleComputedFields

func HandleComputedFields(c *gin.Context, computedFields map[string]string, data reflect.Value)

func HandleGet

func HandleGet(c *gin.Context, db *gorm.DB, primaries map[string]interface{}, model any)

func JoinRelations

func JoinRelations(c *gin.Context, d *gorm.DB, config QueryMapConfig, modelInfo *ModelInfo, relations map[string]*params.Conditions)

func LoadAndValidateMap

func LoadAndValidateMap(c *gin.Context, jsonData []byte, jsonMap map[string]interface{}, modelType reflect.Type)

func LoadAndValidateMaps

func LoadAndValidateMaps(c *gin.Context, jsonData []byte, jsonMaps *[]map[string]interface{}, modelType reflect.Type)

func LoadForeignKeys

func LoadForeignKeys(db *gorm.DB, modelVal reflect.Value, modelSchema *schema.Schema)

func LoadModel

func LoadModel(c *gin.Context, jsonData []byte, model interface{})

func ModelToTableNames

func ModelToTableNames(stmt *string, modelSchema *schema.Schema) []string

func Paginate

func Paginate(pagStart, pagEnd string) func(*gorm.DB) *gorm.DB

func ParseOrder

func ParseOrder(c *gin.Context, order string, info *ModelInfo) message.Message

func PathParamsToModels

func PathParamsToModels(c *gin.Context, modelType reflect.Type, fields []string, destination *[]interface{})

func PostFile

func PostFile(pathFunc func(*gin.Context) string) func(*gin.Context)

func QueryMap

func QueryMap(c *gin.Context, db *gorm.DB, args *QueryMapArgs, config QueryMapConfig) error

func QueryMapRecursive

func QueryMapRecursive(c *gin.Context, db *gorm.DB, args *QueryMapArgs, config QueryMapConfig, info *ModelInfo, conds *params.Conditions, result *[]map[string]any) error

func Recover

func Recover(c *gin.Context)

func RecoverIfEnabled

func RecoverIfEnabled(c *gin.Context)

func Register

func Register(container *gin.RouterGroup, toRegister string, r CRUDSController) *gin.RouterGroup

func RelationsFromModelInfo

func RelationsFromModelInfo(mdl *ModelInfo, otherRelations map[string]*params.Conditions) map[string]*params.Conditions

func RemoveParentPaths

func RemoveParentPaths(path string) string

func SentenceCase

func SentenceCase(fieldName string) string

func SetPrimary

func SetPrimary(model interface{}, value int) error

func ShouldPaginate

func ShouldPaginate(pagStart, pagEnd string) bool

func SnakeToCamelCase

func SnakeToCamelCase(snakeCase string) string

func StrLen

func StrLen(text *string) int

func UpdateToDb

func UpdateToDb(c *gin.Context, model interface{}, values any)

func UpperSnakeCase

func UpperSnakeCase(str string) string

func ValidateMap

func ValidateMap(c *gin.Context, jsonMap map[string]interface{}, modelType reflect.Type) []error

func ValidateMaps

func ValidateMaps(c *gin.Context, jsonMaps []map[string]interface{}, modelType reflect.Type) error

func ValidateMapsPrimaries

func ValidateMapsPrimaries(c *gin.Context, jsonMaps []map[string]interface{}, primaryKeys []string)

func ValidateModel

func ValidateModel(c *gin.Context, model interface{})

func ValidateModels

func ValidateModels(c *gin.Context, models interface{})

func ValidateStruct

func ValidateStruct(c *gin.Context, mdl interface{}) error

func WriteDataWithCount

func WriteDataWithCount(c *gin.Context, pagStart, pagEnd string, data any, count int64)

func WriteQueryMapResult

func WriteQueryMapResult(c *gin.Context, args *QueryMapArgs)

Types

type AbortWithErrorHook

type AbortWithErrorHook struct {
	app.Hook[func(*gin.Context, error)]
}

func (*AbortWithErrorHook) Run

func (h *AbortWithErrorHook) Run(c *gin.Context, err error)

type AddRouter

type AddRouter interface {
	AddRoute(method string, name string, permissionsFunc model.PermissionFunc, handlersFunc ...gin.HandlerFunc)
}

type CRUDSController

type CRUDSController interface {
	AddRouter
	GetModeler

	NewModel() any
	NewSliceOfModel() any

	GetModelType() reflect.Type

	SetBasePath(basePath string)
	SetEndpointIfAbsent(name string)
	GetEndpoint() string
	GetEndpointPath() string

	Get(c *gin.Context)
	GetOne(c *gin.Context)
	GetStructure(c *gin.Context)
	GetRelStructure(c *gin.Context)
	Post(c *gin.Context)
	Patch(c *gin.Context)
	PatchMany(c *gin.Context)
	Delete(c *gin.Context)

	CanImport() bool

	AddCustomRoutes()
	AdditionalModels() []reflect.Type
	GetRoutes() []Route
}

func FindControllerByModel

func FindControllerByModel(modelType reflect.Type) CRUDSController

type Controller

type Controller struct {
	Model    interface{}
	BasePath string
	Endpoint string
	Routes   []Route
}

func (Controller) AddCustomRoutes

func (r Controller) AddCustomRoutes()

func (*Controller) AddRoute

func (r *Controller) AddRoute(method string, name string, permissionsFunc model.PermissionFunc, handlersFunc ...gin.HandlerFunc)

func (Controller) AdditionalModels

func (r Controller) AdditionalModels() []reflect.Type

func (*Controller) CanImport

func (r *Controller) CanImport() bool

func (Controller) Delete

func (r Controller) Delete(c *gin.Context)

func (Controller) Get

func (r Controller) Get(c *gin.Context)

func (Controller) GetEndpoint

func (r Controller) GetEndpoint() string

func (Controller) GetEndpointPath

func (r Controller) GetEndpointPath() string

func (Controller) GetModel

func (r Controller) GetModel() interface{}

func (Controller) GetModelType

func (r Controller) GetModelType() reflect.Type

func (Controller) GetOne

func (r Controller) GetOne(c *gin.Context)

func (Controller) GetRelStructure

func (r Controller) GetRelStructure(c *gin.Context)

func (Controller) GetRoutes

func (r Controller) GetRoutes() []Route

func (Controller) GetStructure

func (r Controller) GetStructure(c *gin.Context)

func (Controller) NewModel

func (r Controller) NewModel() interface{}

func (Controller) NewSliceOfModel

func (r Controller) NewSliceOfModel() interface{}

func (Controller) Patch

func (r Controller) Patch(c *gin.Context)

func (Controller) PatchMany

func (r Controller) PatchMany(c *gin.Context)

func (Controller) Post

func (r Controller) Post(c *gin.Context)

func (*Controller) SetBasePath

func (r *Controller) SetBasePath(basePath string)

func (*Controller) SetEndpointIfAbsent

func (r *Controller) SetEndpointIfAbsent(name string)

type ControllerHooks

type ControllerHooks struct {
	AbortWithError AbortWithErrorHook
	OnRecover      OnRecoverHook
}

type DynamicModel

type DynamicModel struct {
	Dest      interface{}
	Relations *map[string]NestedModel
}

type ErrorValidation

type ErrorValidation struct {
	message.Msg
	Field     string
	Validator string
	Value     interface{}
}

type FieldInfo

type FieldInfo struct {
	Field           string `json:"field"`
	Label           string `json:"label"`
	Descriptive     string `json:"descriptive"`
	Type            string `json:"type"`
	Primary         bool   `json:"primary"`
	Required        bool   `json:"required"`
	UpdateKey       bool   `json:"updateKey"`
	Deprecated      bool   `json:"deprecated"`
	MaxLength       int    `json:"maxLength"`
	RequiredWithout string `json:"requiredWithout"`
	RequiredWith    string `json:"requiredWith"`
	Updatable       bool   `json:"updatable"`
	Creatable       bool   `json:"creatable"`
	Query           bool   `json:"query"`
}

func GetFieldInfo

func GetFieldInfo(c *gin.Context, field *schema.Field) FieldInfo

type FileSystemPermissions

type FileSystemPermissions struct {
	Get     model.PermissionFunc
	Post    model.PermissionFunc
	GetFile model.PermissionFunc
	Delete  model.PermissionFunc
}

func DefaultFileSystemPermissions

func DefaultFileSystemPermissions(ctrl GetModeler) FileSystemPermissions

type GetModeler

type GetModeler interface {
	GetModel() any
}

type MSSqlError

type MSSqlError interface {
	Error() string
	SQLErrorClass() uint8
	SQLErrorLineNo() int32
	SQLErrorMessage() string
	SQLErrorNumber() int32
	SQLErrorProcName() string
	SQLErrorServerName() string
	SQLErrorState() uint8
}

type ModelInfo

type ModelInfo struct {
	Select     []string
	SelectArgs []any
	Fields     []reflect.StructField
	Schema     *schema.Schema
	Table      string
	Order      string
	Relations  map[string]*params.Conditions
	Nested     map[string]NestedModel
	Aggregate  bool
	Distinct   bool
}

type NestedModel

type NestedModel struct {
	References []*schema.Reference
	Slice      bool
	ModelInfo  *ModelInfo
}

func (NestedModel) NextNested

func (n NestedModel) NextNested() map[string]NestedModel

type OnRecoverHook

type OnRecoverHook struct {
	app.Hook[func(*gin.Context, string)]
}

func (*OnRecoverHook) Run

func (h *OnRecoverHook) Run(c *gin.Context, err string)

type QueryMapArgs

type QueryMapArgs struct {
	// Args
	Sel       string
	Rel       string
	Params    string
	P         string
	PagStart  string
	PagEnd    string
	Ord       string
	Primaries map[string]interface{}
	// Model
	Model any
	// Output
	Info   ModelInfo
	Result []map[string]any
	Count  int64
}

type QueryMapConfig

type QueryMapConfig struct {
	// Options
	SkipValidation bool
	SkipDefaults   bool
	P              map[string]struct{}
	Ord            map[string]struct{}
}

type Relation

type Relation struct {
	Label      string
	Model      model.TableModel
	ForeignKey string
}

type RelationInfo

type RelationInfo struct {
	Field      string      `json:"field"`
	Label      string      `json:"label"`
	ForeignKey string      `json:"foreignKey"`
	References string      `json:"references"`
	Type       string      `json:"type"`
	Endpoint   string      `json:"endpoint"`
	Struct     *StructInfo `json:"struct"`
	Updatable  bool        `json:"updatable"`
	Creatable  bool        `json:"creatable"`
}

func GetRelationInfo

func GetRelationInfo(c *gin.Context, rel *schema.Relationship, relations [][]string) RelationInfo

type Response

type Response struct {
	Data  interface{}
	Next  string
	Count int64
}

type Route

type Route struct {
	Method          string
	Name            string
	PermissionsFunc model.PermissionFunc
	HandlerFuncs    []gin.HandlerFunc
}

type StructInfo

type StructInfo struct {
	Fields           []FieldInfo              `json:"fields"`
	Relations        []RelationInfo           `json:"relations"`
	UpdateConditions []model.UpdateConditions `json:"updateConditions"`
}

func GetStructInfo

func GetStructInfo(c *gin.Context, schem *schema.Schema, relations [][]string) StructInfo

Jump to

Keyboard shortcuts

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