crud

package
v0.0.0-...-470388c Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLastItemID

func GetLastItemID[T PageItem](items []T) int

func GinParentVerifier

func GinParentVerifier(parentDao ParentDao) gin.HandlerFunc

Types

type Controller

type Controller[M Model, S Response[M], R Request[M]] struct {
	Svc Service[M]
}

func (*Controller[M, S, R]) Create

func (c *Controller[M, S, R]) Create(ctx *gin.Context, req R) (*S, error)

func (*Controller[M, S, R]) Delete

func (c *Controller[M, S, R]) Delete(ctx *gin.Context, p ResourceParam) error

func (*Controller[M, S, R]) List

func (c *Controller[M, S, R]) List(ctx *gin.Context, p ListParam) (*ListResponse[S], error)

func (*Controller[M, S, R]) Retrieve

func (c *Controller[M, S, R]) Retrieve(ctx *gin.Context, p ResourceParam) (*S, error)

func (*Controller[M, S, R]) Update

func (c *Controller[M, S, R]) Update(ctx *gin.Context, p ResourceParam, req R) error

type Dao

type Dao[M Model] struct {
	*pgdb.PGDB
}

func (*Dao[M]) BulkCreate

func (db *Dao[M]) BulkCreate(ctx context.Context, m []M) error

func (*Dao[M]) Create

func (db *Dao[M]) Create(ctx context.Context, m M) (*M, error)

func (*Dao[M]) Delete

func (db *Dao[M]) Delete(ctx context.Context, id int) error

func (*Dao[M]) Get

func (db *Dao[M]) Get(ctx context.Context, id int) (*M, error)

func (*Dao[M]) List

func (db *Dao[M]) List(ctx context.Context, page pgdb.Page, creatorID int) (res []M, total int64, err error)

func (*Dao[M]) Update

func (db *Dao[M]) Update(ctx context.Context, id int, m M) (*M, error)

type DaoI

type DaoI[M any] Service[M]

type ListParam

type ListParam struct {
	After int `form:"after"`
	Limit int `form:"limit"`
	Page  int `form:"page"`
}

func (ListParam) QueryPage

func (p ListParam) QueryPage() pgdb.Page

type ListResponse

type ListResponse[M any] struct {
	Items     []M   `json:"items"`
	Total     int64 `json:"total"`
	NextAfter int   `json:"next_after,omitempty"`
}

type Model

type Model interface {
	IsDeleted() bool
	ResourceName() string
	PK() int
	Joins() []string
}

type ModelWithCreator

type ModelWithCreator interface {
	SetCreatedBy(userID int)
	CreatedByID() int
}

type NestedBulkCreateRequest

type NestedBulkCreateRequest[M NestedModel[M], R NestedResRequest[M]] []R

type NestedController

type NestedController[M NestedModel[M], S Response[M], R NestedResRequest[M]] struct {
	Svc Service[M]
}

NestedController represents crud controller taking model, request and response object as type param. Response object dependency could be eliminated if model has a method to convert to response but that would move the contract definition to model, which is avoided here. Path is expected to be in format /parent/:parentID/child/:id with exact param names

func (*NestedController[M, S, R]) BulkCreate

func (c *NestedController[M, S, R]) BulkCreate(ctx *gin.Context, p NestedParam, reqs NestedBulkCreateRequest[M, R]) (*S, error)

func (*NestedController[M, S, R]) Create

func (c *NestedController[M, S, R]) Create(ctx *gin.Context, p NestedParam, req R) (*S, error)

func (*NestedController[M, S, R]) Delete

func (c *NestedController[M, S, R]) Delete(ctx *gin.Context, p NestedResourceParam) error

func (*NestedController[M, S, R]) List

func (c *NestedController[M, S, R]) List(ctx *gin.Context, p NestedParam) (*ListResponse[S], error)

func (*NestedController[M, S, R]) Retrieve

func (c *NestedController[M, S, R]) Retrieve(ctx *gin.Context, p NestedResourceParam) (*S, error)

func (*NestedController[M, S, R]) Update

func (c *NestedController[M, S, R]) Update(ctx *gin.Context, p NestedResourceParam, req R) error

type NestedModel

type NestedModel[M Model] interface {
	Model
	ParentID() int
	SetParentID(int) M
}

type NestedParam

type NestedParam struct {
	ParentID int `uri:"parentID" binding:"required"`
	ListParam
}

type NestedResRequest

type NestedResRequest[M NestedModel[M]] interface {
	ToModel(*gin.Context) M
}

type NestedResourceParam

type NestedResourceParam struct {
	ID       int `uri:"id" binding:"required"`
	ParentID int `uri:"parentID" binding:"required"`
}

NestedResourceParam is used to bind nested uri like /parent/:parentID/child/:id first param is expected to be named "parentID" and second as "id"

type PageItem

type PageItem interface {
	ItemID() int
}

type ParentDao

type ParentDao interface {
	GetByUserID(ctx context.Context, userID int) ([]Model, error)
}

type Request

type Request[M Model] interface {
	ToModel(*gin.Context) M
}

type ResourceParam

type ResourceParam struct {
	ID int `uri:"parentID" binding:"required"`
}

type Response

type Response[M Model] interface {
	FillFromModel(m M) Response[M]
	ItemID() int
}

type Service

type Service[M any] interface {
	Get(ctx context.Context, id int) (*M, error)
	Create(ctx context.Context, m M) (*M, error)
	Update(ctx context.Context, id int, m M) (*M, error)
	Delete(ctx context.Context, id int) error
	List(ctx context.Context, page pgdb.Page, creatorID int) (res []M, total int64, err error)
	BulkCreate(ctx context.Context, m []M) error
}

Jump to

Keyboard shortcuts

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