Skip to content

shaomingquan/catalyst-crud-sample

Repository files navigation

catalyst-crud-sample

it's an extention template base on catalyst-sample

benifit: build basic crud api quickly

0, prepare

install project vendors

install mysql and create test table (./infrastructure.dev could help)

import crud demo.postman_collection.json with postman, see request samples

1, define a model and define its lifecycle:

  • SingleInstance: single instance just for unmarshal
  • ListInstance: return collection of test instance, just for return values·
  • NewInstance: when new item, add some default value
  • ReturnInstance: when get list
  • Put???
package store

import (
	"time"

	"github.com/gin-gonic/gin"
)

type InstanceLifecycle struct {
	SingleInstance func(ctx *gin.Context) interface{}
	ListInstance   func(ctx *gin.Context) interface{}
	NewInstance    func(ctx *gin.Context) interface{}

	ReturnInstance func(ctx *gin.Context, ret interface{}) interface{}
}

var modelInstanceMapper = map[string]InstanceLifecycle{
	"test": InstanceLifecycle{
		SingleInstance: func(ctx *gin.Context) interface{} {
			return &Test{}
		},
		ListInstance: func(ctx *gin.Context) interface{} {
			return &[]Test{}
		},
		NewInstance: func(ctx *gin.Context) interface{} {
			return &Test{
				CreatedAt: time.Now(),
				UpdatedAt: time.Now(),
			}
		},
		ReturnInstance: func(ctx *gin.Context, ret interface{}) interface{} {

			_list := *ret.(*[]Test)
			list := make([]Test, len(_list))

			for index, item := range _list {
				item.Name = item.Name + " ~~"
				list[index] = item
			}

			return list
		},
	},
}

2, declare crud interseptor

package apps

var MiddlewaresComposer = []string{

	// crud interseptor
	"store@Crud#/api/data/test/,test",

	"middwares@Demo#root", // pkg@method#param1,param2
}

var PrefixOfRoot = "/"
var MethodOfRoot = "GET"

About

catalyst sample with curd demo encapsulation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages