beerapi: github.com/baruchlubinsky/beerapi/api Index | Files

package api

import "github.com/baruchlubinsky/beerapi/api"

Package api implements the REST verbs. It also contains functions for serializing and deserialing models as JSON. This pacakge is independant of the implementation of the database adapter.

Index

Package Files

api.go delete.go get.go post.go put.go

func Delete Uses

func Delete(dbTable adapters.Table, response http.ResponseWriter, request *http.Request)

Handle a DELETE request.

- DELETE /beers/:id

If no id is specified returns a 400 error.

If the specified id is not found, returns a 404 error.

If saving the data returns an error, return a 400 error.

Returns "{}" with code 200.

func Get Uses

func Get(dbTable adapters.Table, response http.ResponseWriter, request *http.Request)

Handle a GET request. This function supports three options:

- GET /beers/ - returns all the records in the beers table.
- GET /beers/:id - returns the beer with specified id
- GET /beers?ids[]=1&ids[]=2 - returns all the specified beer records.
     see http://emberjs.com/api/data/classes/DS.RESTAdapter.html#method_findMany

If an id is specified but not found, returns a 404 error.

If none of the above patterns is follwed, return a 400 error.

func Marshal Uses

func Marshal(model adapters.Model, name string) ([]byte, error)

Returns the JSON represenation of this model with root element name.

func MarshalSet Uses

func MarshalSet(set adapters.ModelSet, name string) ([]byte, error)

Returns the JSON representation of this slice of models, with root element name which should be a plural.

func Post Uses

func Post(dbTable adapters.Table, response http.ResponseWriter, request *http.Request)

Handle a POST request.

If the body of the request cannot be deserialized, panic.

If saving the data returns an error, return a 400 error.

Returns the saved record.

func Put Uses

func Put(dbTable adapters.Table, response http.ResponseWriter, request *http.Request)

Handle a PUT request.

- PUT /beers/:id

If no id is specified returns a 400 error.

If the specified id is not found, returns a 404 error.

If the body of the request cannot be deserialized, panic.

If saving the data returns an error, return a 400 error.

Returns the saved record.

func Unmarshal Uses

func Unmarshal(data []byte, name string) (map[string]interface{}, error)

Deserialize JSON data from a request, name should be the root element.

type HandlerFunc Uses

type HandlerFunc func(adapters.Table, http.ResponseWriter, *http.Request)

Package api imports 5 packages (graph) and is imported by 2 packages. Updated 2016-07-30. Refresh now. Tools for package owners.