uhttpcrud

package module
v1.0.21 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDBContextKey

func SetDBContextKey(_dbContextKey string)

SetDBContextKey for users and roles

Types

type CrudOptions

type CrudOptions struct {
	// IDParameterName is the name of the GET parameter which is used
	// for getting and deleting documents
	IDParameterName string

	// Database is the name of the database which contains the documents to be modified (will be passed to all service-calls)
	Database string

	// ListEndpoint is the http-endpoint-name for list queries
	// If == nil there will be no list-enpoint
	ListEndpoint         *string
	ListPreprocess       func(context.Context) error
	ListPermission       *uauthPermissions.Permission
	ListOthersPermission *uauthPermissions.Permission

	// GetEndpoint is the http-endpoint-name for get queries
	// If == nil there will be no get-enpoint
	GetEndpoint         *string
	GetPreprocess       func(context.Context) error
	GetPermission       *uauthPermissions.Permission
	GetOthersPermission *uauthPermissions.Permission

	// CreateEndpoint is the http-endpoint-name for create queries
	// If == nil there will be no create-enpoint
	CreateEndpoint         *string
	CreatePreprocess       func(context.Context) error
	CreatePermission       *uauthPermissions.Permission
	CreateOthersPermission *uauthPermissions.Permission

	// UpdateEndpoint is the http-endpoint-name for update queries
	// If == nil there will be no update-enpoint
	UpdateEndpoint         *string
	UpdatePreprocess       func(context.Context) error
	UpdatePermission       *uauthPermissions.Permission
	UpdateOthersPermission *uauthPermissions.Permission

	// DeleteEndpoint is the http-endpoint-name for delete queries
	// If == nil there will be no delete-enpoint
	DeleteEndpoint         *string
	DeletePreprocess       func(context.Context) error
	DeletePermission       *uauthPermissions.Permission
	DeleteOthersPermission *uauthPermissions.Permission

	// ModelService will be called upon for all database interactions
	ModelService ModelService

	// Model will be used to parse and validate models given to create/update handlers
	Model WithID
}

func (CrudOptions) CreateEndpoints

func (o CrudOptions) CreateEndpoints()

CreateEndpoints adds all handlers configured in CrudOptions using the uhttp-framework

type ModelService

type ModelService interface {

	// CopyAndInit allows us to clone a service
	CopyAndInit(db *mongo.Client, database string) ModelService

	// Validate should validate the model which is created/updated
	// - called from the createHandler, before it calls service.Create
	// - called from the updateHandler, before it calls service.Update
	Validate(interface{}) bool

	// Get retrieves a document by its ID (typically a string or ObjectID etc.)
	// If limitToUser is true the service should only return documents which belong to the user
	Get(ID interface{}, user *uauthModels.User, limitToUser bool) (interface{}, error)

	// List retrieves all documents which this user has access to
	// If limitToUser is true the service should only return documents which belong to the user
	List(user *uauthModels.User, limitToUser bool) (interface{}, error)

	// Create creates a document in the database and returns the new document
	// If permissions are implemented, the service should make this created document belong to the
	// user passed into this method
	Create(obj interface{}, user *uauthModels.User) (interface{}, error)

	// Update updates a document. It is up to the implementer to get the ID-property, etc.
	// It returns the updated document
	// If limitToUser is true the service should check if this user is allowed to modify this document
	Update(obj interface{}, user *uauthModels.User, limitToUser bool) (interface{}, error)

	// Delete deletes a document by its ID (typically a string or ObjectID etc.)
	// If limitToUser is true the service should check if this user is allowed to delete this document
	Delete(id interface{}, user *uauthModels.User, limitToUser bool) error
}

ModelService is an interface which all CRUD-http-handlers will use

type WithID

type WithID interface {
	// Returns the ID of this struct (typically a string or ObjectID, etc.)
	GetID() interface{}
}

WithID makes sure the struct in question has a gettable ID property for DB-operations

Jump to

Keyboard shortcuts

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