app

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

controllers/base_controller.go

models/IModel.go

app/ISchema.go

service/base_service.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DTOListToModelList

func DTOListToModelList[TDTO any, T any](dtos []TDTO) ([]T, error)

func DTOToModel

func DTOToModel(dto interface{}, model interface{})

func MapToStructWithColumn

func MapToStructWithColumn(m map[string]interface{}, s interface{}) error

MapToStructWithColumnTag 将包含gorm column标签作为键的map转换为结构体

func ModelListToVOList

func ModelListToVOList[T IModel, TVO IVO](modelList []T) (*[]TVO, error)

转换函数

func ModelToVO

func ModelToVO[T1 any, T2 any](model T1, voPointer interface{}) error

ModelToVO 将 Model 转换为 VO,假设两者拥有相同的公共字段

func StructToMapWithColumn

func StructToMapWithColumn(s interface{}) (map[string]interface{}, error)

StructToMapWithColumnTag 将结构体转换为map,使用gorm的column标签作为key

Types

type BaseController

type BaseController[T IModel, CreateDTO ICreateDTO, QueryDTO IQueryDTO, TVO IVO] struct {
	Service IBaseService[T]
}

BaseController 是一个泛型基类,提供CRUD方法的默认实现。

func NewBaseController

func NewBaseController[T IModel, CreateDTO ICreateDTO, QueryDTO IQueryDTO, TVO IVO](s IBaseService[T]) *BaseController[T, CreateDTO, QueryDTO, TVO]

NewBaseController 创建一个新的 BaseController 实例。

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) BulkCreate

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) BulkCreate(c *gin.Context)

批量创建

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) BulkDelete

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) BulkDelete(c *gin.Context)

Deletes 删除资源

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) Create

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) Create(c *gin.Context)

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) Delete

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) Delete(c *gin.Context)

Delete 删除资源

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) GetById

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) GetById(c *gin.Context)

Get 读取资源

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) GetTreeById

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) GetTreeById(c *gin.Context)

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) PageByDTO

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) PageByDTO(c *gin.Context)

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) TreePage

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) TreePage(c *gin.Context)

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) Update

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) Update(c *gin.Context)

Update 更新资源,接收 DTO 类型的参数,并将其转换为模型后提交到服务操作

func (*BaseController[T, CreateDTO, QueryDTO, TVO]) UpdateByMap

func (bc *BaseController[T, CreateDTO, QueryDTO, TVO]) UpdateByMap(c *gin.Context)

type BaseDTO

type BaseDTO struct{}

type BaseService

type BaseService[T IModel] struct {
	DB    *gorm.DB // GORM 数据库连接实例
	Model T        // 模型引用
}

BaseService 定义了一个通用的基础服务,提供了一些通用的字段和方法。

func NewBaseService

func NewBaseService[T IModel](db *gorm.DB, model T) *BaseService[T]

NewBaseService 创建一个新的 BaseService 实例。

func (*BaseService[T]) BulkCreate

func (bs *BaseService[T]) BulkCreate(models []T) (int, *[]T, error)

BulkCreate 实现批量创建

func (*BaseService[T]) BulkDelete

func (bs *BaseService[T]) BulkDelete(ids []string) (int, error)

Create 实现基础服务接口中的创建方法

func (*BaseService[T]) Create

func (bs *BaseService[T]) Create(model T) (*T, error)

Create 实现基础服务接口中的创建方法

func (*BaseService[T]) Delete

func (bs *BaseService[T]) Delete(id string) error

Delete 实现基础服务接口中的删除方法

func (*BaseService[T]) GetById

func (bs *BaseService[T]) GetById(id string) (*T, error)

Read 实现基础服务接口中的读取方法

func (*BaseService[T]) GetTreeById

func (bs *BaseService[T]) GetTreeById(id string) (*[]T, error)

func (*BaseService[T]) PageByDTO

func (bs *BaseService[T]) PageByDTO(queryDto interface{}, queryPage *QueryPage) (*[]T, int, error)
	return &models, int(count), nil
}

func (*BaseService[T]) TreePageByDTO

func (bs *BaseService[T]) TreePageByDTO(queryDto interface{}, queryPage *QueryPage) (*[]T, int, error)

func (*BaseService[T]) Update

func (bs *BaseService[T]) Update(id string, model T) (*T, error)

Update 实现基础服务接口中的更新方法

func (*BaseService[T]) UpdateByMap

func (bs *BaseService[T]) UpdateByMap(id string, dtoMap map[string]interface{}) error

Update 实现基础服务接口中的更新方法

type BaseVO

type BaseVO struct{}

func (BaseVO) GetDetailVO

func (BV BaseVO) GetDetailVO() interface{}

func (BaseVO) GetListVO

func (BV BaseVO) GetListVO() interface{}

type GenericModel

type GenericModel struct {
	Sort   int    `gorm:"column:sort;type:int" json:"Sort"`
	Remark string `gorm:"column:remark;size:1024" json:"Remark"`
	Status *int   `gorm:"column:status;type:int" json:"Status"`
}

type IBaseController

type IBaseController interface {
	Create(c *gin.Context)
	BulkCreate(c *gin.Context)
	Update(c *gin.Context)
	Delete(c *gin.Context)
	BulkDelete(c *gin.Context)
	PageByDTO(c *gin.Context)
	GetTreeById(c *gin.Context)
	TreePage(c *gin.Context)
	GetById(c *gin.Context)
	UpdateByMap(c *gin.Context)
}

type IBaseService

type IBaseService[T IModel] interface {
	Create(model T) (*T, error)
	BulkCreate(models []T) (int, *[]T, error)
	// CreateOrUpdateByKeys(keys []string, model T, many bool) (int64, error)
	// CreateOrUpdateByDTO(model T) (*T, error)
	GetById(id string) (*T, error)
	Update(id string, model T) (*T, error)
	Delete(id string) error
	BulkDelete(ids []string) (int, error)
	PageByDTO(queryDto interface{}, queryPage *QueryPage) (*[]T, int, error)
	TreePageByDTO(queryDto interface{}, queryPage *QueryPage) (*[]T, int, error)
	GetTreeById(id string) (*[]T, error)
	UpdateByMap(id string, dtoMap map[string]interface{}) error
}

IBaseService 定义了基础服务接口,任何具体服务都应该实现这些方法。

type ICreateDTO

type ICreateDTO interface {
}

type IModel

type IModel interface {
	TableName() string
}

type IQueryDTO

type IQueryDTO interface {
}

type IVO

type IVO interface {
	GetDetailVO() interface{}
	GetListVO() interface{}
}

type OperatorModel

type OperatorModel struct {
	CreateUser int64 `gorm:"column:create_user;type:bigint" json:"CreateUser,string"`
	CreateDept int64 `gorm:"column:create_dept;type:bigint" json:"CreateDept,string"`
	UpdateUser int64 `gorm:"column:update_user;type:bigint" json:"UpdateUser,string"`
}

type PaginationResult

type PaginationResult struct {
	Records []interface{} `json:"records"`
	Current int           `json:"current"`
	Size    int           `json:"size"`
	Total   int           `json:"total"`
	Pages   int           `json:"pages"`
}

type QueryPage

type QueryPage struct {
	Current int      `json:"current" form:"current"`
	Size    int      `json:"size" form:"size"`
	Ascs    []string `json:"ascs" form:"ascs"`
	Descs   []string `json:"descs" form:"descs"`
}

func NewQueryPageAndBind

func NewQueryPageAndBind(size int, c *gin.Context) *QueryPage

type SnowflakeIDModel

type SnowflakeIDModel struct {
	Id int64 `gorm:"primaryKey;autoIncrement:false" json:"Id,string"`
}

SnowflakeIDModel 是基于雪花算法生成唯一标识的基础模型

func (*SnowflakeIDModel) BeforeCreate

func (m *SnowflakeIDModel) BeforeCreate(tx *gorm.DB) error

BeforeCreate 在创建记录前为模型生成雪花算法ID

type SoftDeleteModel

type SoftDeleteModel struct {
	DeletedAt gorm.DeletedAt `gorm:"index"`
}

type TimeStampsModel

type TimeStampsModel struct {
	CreatedAt time.Time
	UpdatedAt time.Time
}

Directories

Path Synopsis
service/base_service.go
service/base_service.go

Jump to

Keyboard shortcuts

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