ginhelper

package
v0.0.0-...-1770643 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2019 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// List is list method
	List = iota
	Create
	Retrieve
	Update
	Destroy
)
View Source
const (
	// ERROR_DUPLICATE is duplicate error code
	ERROR_DUPLICATE = "ERROR_DUPLICATE"
)
View Source
const HEADER_TOTAL_COUNT = "X-TOTAL-COUNT"

HEADER_TOTAL_COUNT is total count in header for pagination

Variables

View Source
var DefaultOffsetLimitPaginator = NewOffsetLimitPaginator(100)

DefaultOffsetLimitPaginator is default OffsetLimitPaginator with 100 items per page

View Source
var ReadOnly = []uint{List, Retrieve}

ReadOnly is enum of read only methods

Functions

func BindRouter

func BindRouter(r gin.IRoutes, prefix string, restView IRestView, rest IRest, methods ...uint)

BindRouter bind rest group routers

func MustCopy

func MustCopy(toValue, fromValue interface{})

MustCopy must copy a struct or slice to other struct or slice

func NormalizeBatchQuery

func NormalizeBatchQuery(in string) ([]string, bool)

func PaginationQuery

func PaginationQuery(db *gorm.DB, t interface{}, limit, offset int) (int, error)

PaginationQuery query with pagination, return total count

func ResponsePagination

func ResponsePagination(ctx *gin.Context, count int, data interface{})

ResponsePagination response array data with total count in header

func WithCacheControl

func WithCacheControl(ctx *gin.Context, d time.Duration)

WithCacheControl add cache control header with time duration

Types

type ErrResp

type ErrResp struct {
	Code    string      `json:"code"`
	Message string      `json:"message"`
	Errors  interface{} `json:"errors"`
}

ErrResp is error response

type IRest

type IRest interface {
	List(ctx *gin.Context, restView IRestView)
	Create(ctx *gin.Context, restView IRestView)
	Retrieve(ctx *gin.Context, restView IRestView, id string)
	Update(ctx *gin.Context, restView IRestView, id string)
	Destroy(ctx *gin.Context, restView IRestView, id string)
}

IRest is rest interface

type IRestView

type IRestView interface {
	GetQuerySet() *gorm.DB
	GetModel(isMany bool) interface{}
	GetSerializer(isMany bool) interface{}
	GetCreateSerializer() interface{}
	SaveData(interface{}) (interface{}, error)
	UpdateData(interface{}, string) (interface{}, error)
	GetOrderBy() string
	LookupField() string
}

IRestView is rest view set interface

type Model

type Model struct {
	ID        uint       `json:"id" gorm:"primary_key"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt time.Time  `json:"updated_at"`
	DeletedAt *time.Time `json:"-" sql:"index"`
}

Model is same as gorm.Model but with json tag

type OffsetLimitPaginator

type OffsetLimitPaginator struct {
	DefaultNumPerPage int
}

OffsetLimitPaginator is struct of OffsetLimit style paginator

func NewOffsetLimitPaginator

func NewOffsetLimitPaginator(defaultNumPerPage int) *OffsetLimitPaginator

NewOffsetLimitPaginator init a OffsetLimitPaginator by default num per page

func (*OffsetLimitPaginator) ParsePagination

func (op *OffsetLimitPaginator) ParsePagination(c *gin.Context) (limit, offset int)

ParsePagination get limit and offset from request

type Rest

type Rest struct{}

Rest is struct impl IRest interface

func (*Rest) Create

func (r *Rest) Create(ctx *gin.Context, restView IRestView)

Create impl IRest's Create

func (*Rest) Destroy

func (r *Rest) Destroy(ctx *gin.Context, restView IRestView, id string)

Destroy impl IRest's Destroy

func (*Rest) List

func (r *Rest) List(ctx *gin.Context, restView IRestView)

List impl IRest's List

func (*Rest) Retrieve

func (r *Rest) Retrieve(ctx *gin.Context, restView IRestView, id string)

Retrieve impl IRest's Retrieve

func (*Rest) Update

func (r *Rest) Update(ctx *gin.Context, restView IRestView, id string)

Update impl IRest's Update

type RestView

type RestView struct{}

RestView is struct impl IRestView interface

func (*RestView) GetCreateSerializer

func (r *RestView) GetCreateSerializer() interface{}

GetCreateSerializer impl IRestView's GetCreateSerializer

func (*RestView) GetModel

func (r *RestView) GetModel(isMany bool) interface{}

GetModel impl IRestView's GetModel

func (*RestView) GetOrderBy

func (r *RestView) GetOrderBy() string

GetOrderBy impl IRestView's GetOrderBy

func (*RestView) GetQuerySet

func (r *RestView) GetQuerySet() *gorm.DB

GetQuerySet impl IRestView's GetQuerySet

func (*RestView) GetSerializer

func (r *RestView) GetSerializer(isMany bool) interface{}

func (*RestView) LookupField

func (r *RestView) LookupField() string

LookupField impl IRestView's LookupField

func (*RestView) SaveData

func (r *RestView) SaveData(in interface{}) (interface{}, error)

SaveData impl IRestView's SaveData

func (*RestView) UpdateData

func (r *RestView) UpdateData(in interface{}, id string) (interface{}, error)

UpdateData impl IRestView's UpdateData

Jump to

Keyboard shortcuts

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