Documentation ¶
Index ¶
- func NewThingCreateHandlerFunc(db *sql.DB) http.HandlerFunc
- func NewThingSearchHandlerFunc(db *sql.DB) http.HandlerFunc
- func NewThingUpdateHandlerFunc(db *sql.DB) http.HandlerFunc
- type Condition
- type Operator
- type Thing
- type ThingField
- type ThingFilter
- type ThingOrderBy
- type ThingSearchRequest
- func (sr *ThingSearchRequest) AddFields(fields ...ThingField) *ThingSearchRequest
- func (sr *ThingSearchRequest) AddFilter(field ThingField, value interface{}, cfg ...func(f *ThingFilter)) *ThingSearchRequest
- func (sr *ThingSearchRequest) ByID(ID int64) *ThingSearchRequest
- func (sr *ThingSearchRequest) ExecuteCount(db *sql.DB) (int32, error)
- func (sr *ThingSearchRequest) ExecuteGet(db *sql.DB) (*Thing, error)
- func (sr *ThingSearchRequest) ExecuteSearch(db *sql.DB, results *[]Thing) error
- func (sr *ThingSearchRequest) GenerateCountSQL() (string, []interface{}, error)
- func (sr *ThingSearchRequest) GenerateSelectSQL() (string, []interface{}, error)
- func (sr *ThingSearchRequest) SetLimit(limit int) *ThingSearchRequest
- func (sr *ThingSearchRequest) SetOffset(offset int) *ThingSearchRequest
- func (sr *ThingSearchRequest) SetOrderBy(field ThingField, isDescending bool) *ThingSearchRequest
- func (sr *ThingSearchRequest) SetPage(pageNumber, pageSize int) *ThingSearchRequest
- type ThingUpdateRequest
- func (u *ThingUpdateRequest) AddUpdate(field ThingField, value interface{}) *ThingUpdateRequest
- func (u *ThingUpdateRequest) ExecuteUpdate(db *sql.DB) error
- func (u *ThingUpdateRequest) GenerateUpdateSQL() (string, []interface{}, error)
- func (u ThingUpdateRequest) MarshalText() ([]byte, error)
- func (u *ThingUpdateRequest) NewThingUpdateRequest(id int64) *ThingUpdateRequest
- func (u *ThingUpdateRequest) UnmarshalText(b []byte) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewThingCreateHandlerFunc ¶
func NewThingCreateHandlerFunc(db *sql.DB) http.HandlerFunc
NewThingCreateHandlerFunc returns an HTTP handler func for Thing record creations. It returns 200 and the results on success, 400 on bad JSON, 500 on any other error.
func NewThingSearchHandlerFunc ¶
func NewThingSearchHandlerFunc(db *sql.DB) http.HandlerFunc
NewThingSearchHandlerFunc returns an HTTP handler func for ThingSearchRequests. It returns 200 and the results on success, 404 if not a POST, 400 on bad JSON, 500 on any other error.
func NewThingUpdateHandlerFunc ¶
func NewThingUpdateHandlerFunc(db *sql.DB) http.HandlerFunc
NewThingUpdateHandlerFunc returns an HTTP handler func for ThingUpdateRequests. It returns 200 and the results on success, 404 if not a POST, 400 on bad JSON, 500 on any other error.
Types ¶
type Condition ¶
type Condition int
Condition is an indicator of whether or not a filter should be added as an AND or an OR to the search predicates
func (Condition) MarshalText ¶
MarshalText implements https://golang.org/pkg/encoding/#TextMarshaler
func (*Condition) UnmarshalText ¶
UnmarshalText implements https://golang.org/pkg/encoding/#TextUnmarshaler
type Operator ¶
type Operator int
Operator is an operator to be used in a SearchRequest filter
const ( Eq Operator = iota NotEq Like NotLike GreaterThan GreaterThanOrEq LesserThan LesserThanOrEq IsNull NotNull )
Enum-like helpers for Operator
func (Operator) MarshalText ¶
MarshalText implements https://golang.org/pkg/encoding/#TextMarshaler
func (*Operator) UnmarshalText ¶
UnmarshalText implements https://golang.org/pkg/encoding/#TextUnmarshaler
type Thing ¶
type Thing struct { ID int64 `json:"id" db:"id"` Color string `json:"color" db:"color"` Description string `json:"description" db:"description"` Length int `json:"length" db:"length"` Height int `json:"height" db:"height"` CreatedAt time.Time `json:"createdAt" db:"created_at_utc"` }
Thing is a normal thing
func CallThingSearchRequest ¶
func CallThingSearchRequest(url string, sr ThingSearchRequest) ([]Thing, error)
CallThingSearchRequest will call the specified endpoint with a ThingSearchRequest
type ThingField ¶
type ThingField int
ThingField is a field within the Thing struct that is able to be filtered on, sorted on, or returned.
const ( ThingID ThingField = iota ThingColor ThingDescription ThingLength ThingHeight ThingCreatedAt )
Faux enum'd for helpfulness
func (ThingField) DbFieldName ¶
func (s ThingField) DbFieldName() string
DbFieldName returns the name of the field to use in the SQL query
func (ThingField) MarshalText ¶
func (s ThingField) MarshalText() ([]byte, error)
MarshalText implements https://golang.org/pkg/encoding/#TextMarshaler
func (*ThingField) UnmarshalText ¶
func (s *ThingField) UnmarshalText(b []byte) error
UnmarshalText implements https://golang.org/pkg/encoding/#TextUnmarshaler
type ThingFilter ¶
type ThingFilter struct { Field ThingField `json:"field"` Value interface{} `json:"value"` Operator Operator `json:"operator"` Condition Condition `json:"condition"` }
ThingFilter is a filter specific to Thing
type ThingOrderBy ¶
type ThingOrderBy struct { Field ThingField `json:"field"` IsDescending bool `json:"isDescending"` }
ThingOrderBy is a sort directive that is specific to Thing
type ThingSearchRequest ¶
type ThingSearchRequest struct { Fields []ThingField `json:"fields"` Filters []ThingFilter `json:"filters"` OrderBy *ThingOrderBy `json:"orderBy"` Limit int `json:"limit"` Offset int `json:"offset"` }
ThingSearchRequest is a serializable SearchRequest for Thing.
func (*ThingSearchRequest) AddFields ¶
func (sr *ThingSearchRequest) AddFields(fields ...ThingField) *ThingSearchRequest
AddFields adds fields to the select. Helper method for chain building a ThingSearchRequest.
func (*ThingSearchRequest) AddFilter ¶
func (sr *ThingSearchRequest) AddFilter(field ThingField, value interface{}, cfg ...func(f *ThingFilter)) *ThingSearchRequest
AddFilter is a helper method to add a filter to a SearchRequest. By default, the operator is EQ and the condition is AND. If you want to override that:
var sr SearchRequest sr.AddFilter("color", "red", func(f *ThingFilter){ f.Operator = GT f.Condition = OR })
and you're all set.
func (*ThingSearchRequest) ByID ¶
func (sr *ThingSearchRequest) ByID(ID int64) *ThingSearchRequest
ByID constructs a ThingSearchRequest to return a single result by ID
func (*ThingSearchRequest) ExecuteCount ¶
func (sr *ThingSearchRequest) ExecuteCount(db *sql.DB) (int32, error)
ExecuteCount will take a sql.DB connection and execute a SELECT COUNT
func (*ThingSearchRequest) ExecuteGet ¶
func (sr *ThingSearchRequest) ExecuteGet(db *sql.DB) (*Thing, error)
ExecuteGet will take a sql.DB connection and execute this search request and only return one result, err if nothing was found or more than one was found.
func (*ThingSearchRequest) ExecuteSearch ¶
func (sr *ThingSearchRequest) ExecuteSearch(db *sql.DB, results *[]Thing) error
ExecuteSearch will take a sql.DB connection and execute this search request
func (*ThingSearchRequest) GenerateCountSQL ¶
func (sr *ThingSearchRequest) GenerateCountSQL() (string, []interface{}, error)
GenerateCountSQL will generate an executable SQL statement and return the SQL string (with placeholders) and a slice of the values.
func (*ThingSearchRequest) GenerateSelectSQL ¶
func (sr *ThingSearchRequest) GenerateSelectSQL() (string, []interface{}, error)
GenerateSelectSQL will generate an executable SQL statement and return the SQL string (with placeholders) and a slice of the values.
func (*ThingSearchRequest) SetLimit ¶
func (sr *ThingSearchRequest) SetLimit(limit int) *ThingSearchRequest
SetLimit sets the Limit. Helper method for chain building a ThingSearchRequest
func (*ThingSearchRequest) SetOffset ¶
func (sr *ThingSearchRequest) SetOffset(offset int) *ThingSearchRequest
SetOffset sets the Offset. Helper method for chain building a ThingSearchRequest
func (*ThingSearchRequest) SetOrderBy ¶
func (sr *ThingSearchRequest) SetOrderBy(field ThingField, isDescending bool) *ThingSearchRequest
SetOrderBy sets the Order By. Helper method for chain building a SearchRequest
func (*ThingSearchRequest) SetPage ¶
func (sr *ThingSearchRequest) SetPage(pageNumber, pageSize int) *ThingSearchRequest
SetPage sets the Offset and Limit based on the page number and page size.
type ThingUpdateRequest ¶
type ThingUpdateRequest struct { ID int64 `json:"id"` Updates map[ThingField]interface{} `json:"updates"` }
ThingUpdateRequest defines a set of parameters for updating Thing. It can be serialized and passed between services as JSON, or used to generate a SQL statement.
func (*ThingUpdateRequest) AddUpdate ¶
func (u *ThingUpdateRequest) AddUpdate(field ThingField, value interface{}) *ThingUpdateRequest
AddUpdate is a convenience method for adding updates to this request. Chainable.
func (*ThingUpdateRequest) ExecuteUpdate ¶
func (u *ThingUpdateRequest) ExecuteUpdate(db *sql.DB) error
ExecuteUpdate will take a sql.DB connection and execute this update
func (*ThingUpdateRequest) GenerateUpdateSQL ¶
func (u *ThingUpdateRequest) GenerateUpdateSQL() (string, []interface{}, error)
GenerateUpdateSQL returns the SQL string and placeholder values, or an error
func (ThingUpdateRequest) MarshalText ¶
func (u ThingUpdateRequest) MarshalText() ([]byte, error)
MarshalText implements https://golang.org/pkg/encoding/#TextMarshaler
func (*ThingUpdateRequest) NewThingUpdateRequest ¶
func (u *ThingUpdateRequest) NewThingUpdateRequest(id int64) *ThingUpdateRequest
NewThingUpdateRequest creates a new update request for Thing
func (*ThingUpdateRequest) UnmarshalText ¶
func (u *ThingUpdateRequest) UnmarshalText(b []byte) error
UnmarshalText implements https://golang.org/pkg/encoding/#TextUnmarshaler