resource

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2017 License: MIT Imports: 15 Imported by: 6

Documentation

Overview

Package resource provides some shared behaviour for resources, and basic CRUD and URL helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddUserSessionCookie

func AddUserSessionCookie(w *httptest.ResponseRecorder, r *http.Request, id int) error

AddUserSessionCookie adds a new cookie for the given user on the incoming request, so that we can test authentication in handlers.

func SetupAuthorisation

func SetupAuthorisation()

SetupAuthorisation sets up mock authorisation.

func SetupTestDatabase

func SetupTestDatabase(depth int) error

SetupTestDatabase sets up the database for all tests from the test config.

func SetupView

func SetupView(depth int) error

SetupView sets up the view package for testing by loading templates.

func ValidateBoolean

func ValidateBoolean(param interface{}) bool

ValidateBoolean returns the bool value of param or false

func ValidateFloat

func ValidateFloat(param interface{}) float64

ValidateFloat returns the float value of param or 0.0

func ValidateInt

func ValidateInt(param interface{}) int64

ValidateInt returns the int value of param or 0

func ValidateString

func ValidateString(param interface{}) string

ValidateString returns the string value of param or ""

func ValidateTime

func ValidateTime(param interface{}) time.Time

ValidateTime returns the time value of param or the zero value of time.Time

Types

type Base

type Base struct {
	// ID is the default primary key of the resource.
	ID int64

	// CreatedAt stores the creation time of the resource.
	CreatedAt time.Time

	// UpdatedAt stores the last update time of the resource.
	UpdatedAt time.Time

	// TableName is used for database queries and urls.
	TableName string

	// KeyName is used for database queries as the primary key.
	KeyName string
}

Base defines shared fields and behaviour for resources.

func (*Base) CacheKey

func (r *Base) CacheKey() string

CacheKey generates a cache key for this resource based on the TableName, ID and UpdatedAt

func (*Base) Create

func (r *Base) Create(params map[string]string) (int64, error)

Create inserts a new database record and returns the id or an error

func (*Base) CreateURL

func (r *Base) CreateURL() string

CreateURL returns the create url for this model /table/create

func (*Base) Destroy

func (r *Base) Destroy() error

Destroy deletes this resource by removing the database record.

func (*Base) DestroyURL

func (r *Base) DestroyURL() string

DestroyURL returns the destroy url for this model /table/id/destroy

func (*Base) IndexURL

func (r *Base) IndexURL() string

IndexURL returns the index url for this model - /table

func (*Base) OwnedBy

func (r *Base) OwnedBy(uid int64) bool

OwnedBy returns true if the user id passed in owns this resource.

func (*Base) PrimaryKey

func (r *Base) PrimaryKey() string

PrimaryKey returns the id for primary key by default - used by query

func (*Base) PrimaryKeyValue

func (r *Base) PrimaryKeyValue() int64

PrimaryKeyValue returns the unique id

func (*Base) PublicURL

func (r *Base) PublicURL() string

PublicURL returns the canonical url for showing this resource usually this will differ in using the name as a slug

func (*Base) Query

func (r *Base) Query() *query.Query

Query creates a new query relation referencing this specific resource by id.

func (*Base) ResourceID

func (r *Base) ResourceID() string

ResourceID returns a key unique to this resource (we use table).

func (*Base) SelectName

func (r *Base) SelectName() string

SelectName returns our name for select menus

func (*Base) SelectValue

func (r *Base) SelectValue() string

SelectValue returns our value for select options

func (*Base) ShowURL

func (r *Base) ShowURL() string

ShowURL returns the show url for this model /table/id

func (*Base) String

func (r *Base) String() string

String returns a string representation of the resource

func (*Base) Table

func (r *Base) Table() string

Table returns the table name for this object

func (*Base) Update

func (r *Base) Update(params map[string]string) error

Update the database record for this resource with the given params.

func (*Base) UpdateURL

func (r *Base) UpdateURL() string

UpdateURL returns the update url for this model /table/id/update

func (*Base) ValidateParams

func (r *Base) ValidateParams(params map[string]string, allowed []string) map[string]string

ValidateParams allows only those params by AllowedParams() to perform more sophisticated validation override it.

Jump to

Keyboard shortcuts

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