handler

package
v0.1.1-beta.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package handler implements content related actions(eg.create/edit/delete/import) and callback mechanism while handling.

Author xc, Created on 2019-05-11 12:33 {COPYRIGHTS}

Index

Constants

This section is empty.

Variables

View Source
var ErrorNoPermission = errors.New("The user doesn't have access to the action.")

Functions

func CanLogin

func CanLogin(ctx context.Context, usernameEmail string, password string) (error, contenttype.ContentTyper)

CanLogin check if the username/email and password matches

func Copy

func Copy(ctx context.Context, content contenttype.ContentTyper, userId int, parentInt int) error

Copy data

func Create

func Create(ctx context.Context, userID int, contentType string, inputs InputMap, parentID int) (contenttype.ContentTyper, error)

Create creates a content(same behavior as Draft&Publish but store published version directly)

func CreateVersion

func CreateVersion(ctx context.Context, content contenttype.ContentTyper, versionNumber int, tx *sql.Tx) (int, error)

CreateVersion creates a new version. It doesn't validate version number is increment

func DeleteByCID

func DeleteByCID(ctx context.Context, cid int, contenttype string, userId int) error

Delete content by content id

func DeleteByContent

func DeleteByContent(ctx context.Context, content contenttype.ContentTyper, userId int, toTrash bool) error

Delete content, relations and location. Note: this is only for when there is 1 location.

You need to judge if there are more than one locations before invoking this.

func DeleteByID

func DeleteByID(ctx context.Context, id int, userId int, toTrash bool) error

Delete content by location id

func Enable

func Enable(ctx context.Context, user contenttype.ContentTyper, enable bool, userId int) error

enable or disable user(enable = false means disable) todo: create a user interface

func FetchUserRoles

func FetchUserRoles(ctx context.Context, userID int) ([]contenttype.ContentTyper, error)

func GenerateName

func GenerateName(content contenttype.ContentTyper) string

Generate name based on name_pattern from definition.

func GetContentTypeHandler

func GetContentTypeHandler(contentType string) interface{}

func InvokeCallback

func InvokeCallback(ctx context.Context, event string, stopOnError bool, matchData map[string]interface{}, content contenttype.ContentTyper, params ...interface{}) error

InvokeCallback invokes callbacks based on condition match result see content_handler.json/yaml for conditions.

func Move

func Move(ctx context.Context, contentIds []int, targetId int, userId int) error

Move moves contents to target Check delete&create permission. note: it dosn't check if target can create sub-children(only check if it can create direct children)

func RegisterContentTypeHandler

func RegisterContentTypeHandler(contentType string, handler interface{})

func RegisterOperationHandler

func RegisterOperationHandler(handler OperationHandler)

func SaveDraft

func SaveDraft(ctx context.Context, userId int, data string, contentType string, id int) (contenttype.Version, error)

func Update

func Update(ctx context.Context, content contenttype.ContentTyper, inputs InputMap, userId int) (contenttype.ContentTyper, error)

Update content. The inputs doesn't need to include all required fields. However if it's there, it will check if it's required&empty

func UpdateByContentID

func UpdateByContentID(ctx context.Context, contentType string, contentID int, inputs InputMap, userId int) (contenttype.ContentTyper, error)

func UpdateByID

func UpdateByID(ctx context.Context, id int, inputs InputMap, userId int) (contenttype.ContentTyper, error)

func UpdateRelation

func UpdateRelation(content contenttype.ContentTyper)

Types

type ContentTypeHandlerCreate

type ContentTypeHandlerCreate interface {
	//When creating on server side, or import.
	Create(ctx context.Context, content contenttype.ContentTyper, inputs InputMap, parentID int) error
}

type ContentTypeHandlerDelete

type ContentTypeHandlerDelete interface {
	//
	// //when deleting
	Delete(ctx context.Context, content contenttype.ContentTyper) error
}

type ContentTypeHandlerUpdate

type ContentTypeHandlerUpdate interface {
	// //When after edit, the server handles the update
	Update(ctx context.Context, content contenttype.ContentTyper, inputs InputMap) error
}

type ContentTypeHandlerValidate

type ContentTypeHandlerValidate interface {
	ValidateCreate(ctx context.Context, inputs InputMap, parentID int) (bool, ValidationResult)
	ValidateUpdate(ctx context.Context, inputs InputMap, content contenttype.ContentTyper) (bool, ValidationResult)
}

todo: think it might be good to use "struct with callback method" instead of interface. This is a callback based on type. It's used for customzed content type. eg. image to set parent_id

type InputMap

type InputMap map[string]interface{}

func (InputMap) Keys

func (inputs InputMap) Keys() []string

type OperationHandler

type OperationHandler struct {
	Identifier string //Identifier for handler matching. see operation_handler.json/yaml
	//todo: use translation as parameter instead of optional interface{}
	Execute func(ctx context.Context, triggedEvent string, content contenttype.ContentTyper, params ...interface{}) error
}

Callback struct

func GetOperationHandlerByCondition

func GetOperationHandlerByCondition(event string, target map[string]interface{}) ([]OperationHandler, []string)

Get operation handler list based on rules defined in operation_handler.json/yaml target is the real vaule the condition matches to target should not include 'event' key since it's in the parameter already.

type ValidationResult

type ValidationResult struct {
	Message []string          `json:"message"`
	Fields  map[string]string `json:"fields"` //1 means required, other message means real messgae.
}

func Validate

func Validate(ctx context.Context, contentType string, fieldsDef map[string]fieldtype.FieldDef, inputs InputMap, checkAllRequired bool) (bool, ValidationResult)

Validate validates and returns a validation result. Validate is used in Create and Update, but can be also used separately

eg. when you have several steps, you want to validate one step only(only the fields in that step).

func (ValidationResult) Error

func (v ValidationResult) Error() string

can be used a error

func (ValidationResult) Passed

func (v ValidationResult) Passed() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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