resource

package module
v0.0.0-...-9312a3b Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2018 License: MIT Imports: 5 Imported by: 1

README

api2go-resource

Build Status GoDoc MIT License

This package serves as a bridge between GORM and api2go, reducing the amount of boilerplate code needed for implementing CRUD actions for GORM models.

Features

Here are some of the features that api2go-resource provides:

  • Works with your existing GORM models
  • Enables filtering to be limited to specific fields
  • Provides hooks to enable access control and normalization

Usage

Let's suppose you have the following model definition:

type Article struct {
    ID      int64
    Title   string
    Content string
}

In order to use the model with api2go, three important methods must be implemented:

func (a *Article) GetName() string {
    return "articles"
}

func (a *Article) GetID() string {
    return strconv.FormatInt(a.ID, 10)
}

func (a *Article) SetID(id string) error {
    a.ID, _ = strconv.ParseInt(id, 10, 64)
    return nil
}

The next step is to create a Resource instance for the model:

import "github.com/nathan-osman/api2go-resource"

// db is an instance of *gorm.DB

var articleResource = &resource.Resource{
    DB:   db,
    Type: &Article{},
}

This resource can now be registered with api2go:

api := api2go.NewAPI("api")
api.AddResource(&Article{}, articleResource)

Hooks

Hooks can be used for a number of different purposes.

For example, to make a resource read-only:

func readOnly(p *resource.Params) error {
    switch p.Action {
    case resource.BeforeCreate, resource.BeforeDelete, resource.BeforeUpdate:
        return api2go.NewHTTPError(nil, "read only", http.StatusBadRequest)
    }
    return nil
}

var articleResource = &resource.Resource{
    // ...
    Hooks: []Hook{readOnly},
}

To ensure that articles are always retrieved in alphabetical order:

func alphabeticalOrder(p *resource.Params) error {
    switch p.Action {
    case resource.BeforeFindAll, resource.BeforeFindOne:
        p.DB = p.DB.Order('title')
    }
    return nil
}

var articleResource = &resource.Resource{
    // ...
    Hooks: []Hook{alphabeticalOrder},
}

To remove any extra whitespace in an article title before saving:

func trimTitle(p *resource.Params) error {
    switch p.Action {
    case resource.BeforeCreate, resource.BeforeUpdate:
        p.Obj.Title = strings.TrimSpace(p.Obj.Title)
    }
    return nil
}

var articleResource = &resource.Resource{
    // ...
    Hooks: []Hook{trimTitle},
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action indicates the type of action being performed.

const (
	// BeforeCreate indicates that a resource is about to be created.
	BeforeCreate Action = iota
	// AfterCreate indicates that a resource has been created.
	AfterCreate

	// BeforeDelete indicates that a resource is about to be destroyed.
	BeforeDelete
	// AfterDelete indicates that a resource has been destroyed.
	AfterDelete

	// BeforeUpdate indicates that a resource is about to be modified.
	BeforeUpdate
	// AfterUpdate indicates that a resource has been modified.
	AfterUpdate

	// BeforeFindAll indicates that multiple resources are about to be retrieved.
	BeforeFindAll
	// AfterFindAll indicates that multiple resources may have been retrieved.
	AfterFindAll

	// BeforeFindOne indicates that a single resource is about to be retrieved.
	BeforeFindOne
	// AfterFindOne indicates that a single resource may have been retrieved.
	AfterFindOne
)

type Hook

type Hook func(*Params) error

Hook is a callback that is run immediately before processing each action. If the hook returns an error, the action does not run.

type Params

type Params struct {
	Action  Action
	Request api2go.Request
	DB      *gorm.DB
	Obj     interface{}
}

Params contains information about an API request. Only certain members will contain valid data, depending on the action.

type Resource

type Resource struct {
	// DB is a pointer to an open database connection.
	DB *gorm.DB

	// Type is an instance of the model for this resource.
	Type interface{}

	// Hooks is a list of callbacks to run before each action.
	Hooks []Hook

	// Fields is a list of valid field names for filtering.
	Fields []string
}

Resource implements the interfaces necessary to use a GORM model with the api2go package.

func (*Resource) Create

func (r *Resource) Create(obj interface{}, req api2go.Request) (api2go.Responder, error)

Create attempts to save a new model instance to the database.

func (*Resource) Delete

func (r *Resource) Delete(id string, req api2go.Request) (api2go.Responder, error)

Delete attempts to delete the specified model instance from the database.

func (*Resource) FindAll

func (r *Resource) FindAll(req api2go.Request) (api2go.Responder, error)

FindAll attempts to retrieve all instances of a model from the database.

func (*Resource) FindOne

func (r *Resource) FindOne(ID string, req api2go.Request) (api2go.Responder, error)

FindOne attempts to retrieve a single model instance from the database.

func (*Resource) Update

func (r *Resource) Update(obj interface{}, req api2go.Request) (api2go.Responder, error)

Update attempts to update a model instance in the database.

Jump to

Keyboard shortcuts

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