controller

package
v0.10.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// DefaultItemsPerPage stores the default amount of items per page
	DefaultItemsPerPage = 20

	// PageQueryKey stores the query key used to indicate the current page
	PageQueryKey = "page"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Controller

type Controller struct {
	// Container stores a services container which contains dependencies
	Container *services.Container
}

Controller provides base functionality and dependencies to routes. The proposed pattern is to embed a Controller in each individual route struct and to use the router to inject the container so your routes have access to the services within the container

func NewController

func NewController(c *services.Container) Controller

NewController creates a new Controller

func (*Controller) Fail

func (c *Controller) Fail(err error, log string) error

Fail is a helper to fail a request by returning a 500 error and logging the error

func (*Controller) Redirect

func (c *Controller) Redirect(ctx echo.Context, route string, routeParams ...interface{}) error

Redirect redirects to a given route name with optional route parameters

func (*Controller) RenderPage

func (c *Controller) RenderPage(ctx echo.Context, page Page) error

RenderPage renders a Page as an HTTP response

type FormSubmission

type FormSubmission struct {
	// IsSubmitted indicates if the form has been submitted
	IsSubmitted bool

	// Errors stores a slice of error message strings keyed by form struct field name
	Errors map[string][]string
}

FormSubmission represents the state of the submission of a form, not including the form itself

func (FormSubmission) FieldHasErrors

func (f FormSubmission) FieldHasErrors(fieldName string) bool

FieldHasErrors indicates if a given field on the form has any validation errors

func (FormSubmission) GetFieldErrors

func (f FormSubmission) GetFieldErrors(fieldName string) []string

GetFieldErrors gets the errors for a given field name

func (FormSubmission) GetFieldStatusClass

func (f FormSubmission) GetFieldStatusClass(fieldName string) string

GetFieldStatusClass returns an HTML class based on the status of the field

func (FormSubmission) HasErrors

func (f FormSubmission) HasErrors() bool

HasErrors indicates if the submission has any validation errors

func (FormSubmission) IsDone

func (f FormSubmission) IsDone() bool

IsDone indicates if the submission is considered done which is when it has been submitted and there are no errors.

func (*FormSubmission) Process

func (f *FormSubmission) Process(ctx echo.Context, form interface{}) error

Process processes a submission for a form

func (*FormSubmission) SetFieldError

func (f *FormSubmission) SetFieldError(fieldName string, message string)

SetFieldError sets an error message for a given field name

type Page

type Page struct {
	// AppName stores the name of the application.
	// If omitted, the configuration value will be used.
	AppName string

	// Title stores the title of the page
	Title string

	// Context stores the request context
	Context echo.Context

	// ToURL is a function to convert a route name and optional route parameters to a URL
	ToURL func(name string, params ...interface{}) string

	// Path stores the path of the current request
	Path string

	// URL stores the URL of the current request
	URL string

	// Data stores whatever additional data that needs to be passed to the templates.
	// This is what the controller uses to pass the content of the page.
	Data interface{}

	// Form stores a struct that represents a form on the page.
	// This should be a struct with fields for each form field, using both "form" and "validate" tags
	// It should also contain a Submission field of type FormSubmission if you wish to have validation
	// messagesa and markup presented to the user
	Form interface{}

	// Layout stores the name of the layout base template file which will be used when the page is rendered.
	// This should match a template file located within the layouts directory inside the templates directory.
	// The template extension should not be included in this value.
	Layout string

	// Name stores the name of the page as well as the name of the template file which will be used to render
	// the content portion of the layout template.
	// This should match a template file located within the pages directory inside the templates directory.
	// The template extension should not be included in this value.
	Name string

	// IsHome stores whether the requested page is the home page or not
	IsHome bool

	// IsAuth stores whether or not the user is authenticated
	IsAuth bool

	// AuthUser stores the authenticated user
	AuthUser *ent.User

	// StatusCode stores the HTTP status code that will be returned
	StatusCode int

	// Metatags stores metatag values
	Metatags struct {
		// Description stores the description metatag value
		Description string

		// Keywords stores the keywords metatag values
		Keywords []string
	}

	// Pager stores a pager which can be used to page lists of results
	Pager Pager

	// CSRF stores the CSRF token for the given request.
	// This will only be populated if the CSRF middleware is in effect for the given request.
	// If this is populated, all forms must include this value otherwise the requests will be rejected.
	CSRF string

	// Headers stores a list of HTTP headers and values to be set on the response
	Headers map[string]string

	// RequestID stores the ID of the given request.
	// This will only be populated if the request ID middleware is in effect for the given request.
	RequestID string

	HTMX struct {
		Request  htmx.Request
		Response *htmx.Response
	}

	// Cache stores values for caching the response of this page
	Cache struct {
		// Enabled dictates if the response of this page should be cached.
		// Cached responses are served via middleware.
		Enabled bool

		// Expiration stores the amount of time that the cache entry should live for before expiring.
		// If omitted, the configuration value will be used.
		Expiration time.Duration

		// Tags stores a list of tags to apply to the cache entry.
		// These are useful when invalidating cache for dynamic events such as entity operations.
		Tags []string
	}
}

Page consists of all data that will be used to render a page response for a given controller. While it's not required for a controller to render a Page on a route, this is the common data object that will be passed to the templates, making it easy for all controllers to share functionality both on the back and frontend. The Page can be expanded to include anything else your app wants to support. Methods on this page also then become available in the templates, which can be more useful than the funcmap if your methods require data stored in the page, such as the context.

func NewPage

func NewPage(ctx echo.Context) Page

NewPage creates and initiatizes a new Page for a given request context

func (Page) GetMessages

func (p Page) GetMessages(typ msg.Type) []template.HTML

GetMessages gets all flash messages for a given type. This allows for easy access to flash messages from the templates.

type Pager

type Pager struct {
	// Items stores the total amount of items in the result set
	Items int

	// Page stores the current page number
	Page int

	// ItemsPerPage stores the amount of items to display per page
	ItemsPerPage int

	// Pages stores the total amount of pages in the result set
	Pages int
}

Pager provides a mechanism to allow a user to page results via a query parameter

func NewPager

func NewPager(ctx echo.Context, itemsPerPage int) Pager

NewPager creates a new Pager

func (Pager) GetOffset

func (p Pager) GetOffset() int

GetOffset determines the offset of the results in order to get the items for the current page

func (Pager) IsBeginning

func (p Pager) IsBeginning() bool

IsBeginning determines if the pager is at the beginning of the pages

func (Pager) IsEnd

func (p Pager) IsEnd() bool

IsEnd determines if the pager is at the end of the pages

func (*Pager) SetItems

func (p *Pager) SetItems(items int)

SetItems sets the amount of items in total for the pager and calculate the amount of total pages based off on the item per page. This should be used rather than setting either items or pages directly.

Jump to

Keyboard shortcuts

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