Documentation
¶
Overview ¶
Package crudh 提供基于 Hertz 的通用 CRUD HTTP Handler。
将 HTTP Handler 从 data/crud 数据层分离,避免数据层反向依赖 HTTP 中间件层。 Handler 依赖 crud.Service 接口和 middleware 包的响应函数。
用法:
repo := gormrepo.NewGormRepository[Project](db) svc := crud.NewBaseService[Project, CreateReq, UpdateReq](repo) h := crudh.NewHandler(svc) h.RegisterRoutes(group, "/projects")
Package crudh 提供基于 Hertz 的通用 CRUD HTTP Handler。
将 HTTP 层(Handler)与数据层(crud.Repository/crud.Service)分离, 避免 data/crud 包反向依赖 middleware 包。
Index ¶
- type Handler
- func (h *Handler[T, C, U]) Create(ctx context.Context, c *app.RequestContext)
- func (h *Handler[T, C, U]) Delete(ctx context.Context, c *app.RequestContext)
- func (h *Handler[T, C, U]) Get(ctx context.Context, c *app.RequestContext)
- func (h *Handler[T, C, U]) List(ctx context.Context, c *app.RequestContext)
- func (h *Handler[T, C, U]) RegisterRoutes(rg *route.RouterGroup, prefix string)
- func (h *Handler[T, C, U]) Update(ctx context.Context, c *app.RequestContext)
- type HandlerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
Handler 是通用 CRUD HTTP Handler。
func NewHandler ¶
func NewHandler[T any, CreateReq, UpdateReq any](svc crud.Service[T, CreateReq, UpdateReq], opts ...HandlerOption[T, CreateReq, UpdateReq]) *Handler[T, CreateReq, UpdateReq]
NewHandler 创建 CRUD Handler。
func (*Handler[T, C, U]) Create ¶
func (h *Handler[T, C, U]) Create(ctx context.Context, c *app.RequestContext)
Create 处理 POST 创建请求。
func (*Handler[T, C, U]) Delete ¶
func (h *Handler[T, C, U]) Delete(ctx context.Context, c *app.RequestContext)
Delete 处理 DELETE 删除请求。
func (*Handler[T, C, U]) Get ¶
func (h *Handler[T, C, U]) Get(ctx context.Context, c *app.RequestContext)
Get 处理 GET 单个获取请求。
func (*Handler[T, C, U]) List ¶
func (h *Handler[T, C, U]) List(ctx context.Context, c *app.RequestContext)
List 处理 GET 列表查询请求(带分页)。
func (*Handler[T, C, U]) RegisterRoutes ¶
func (h *Handler[T, C, U]) RegisterRoutes(rg *route.RouterGroup, prefix string)
RegisterRoutes 注册 CRUD 路由到 Hertz Group。
type HandlerOption ¶
HandlerOption 是 NewHandler 的配置选项。
func WithSortableColumns ¶
func WithSortableColumns[T any, CreateReq, UpdateReq any](cols ...string) HandlerOption[T, CreateReq, UpdateReq]
WithSortableColumns 设置 List 接口排序参数的白名单列名。 未设置时仅做标识符格式校验;设置后白名单外的列名返回 400。
Click to show internal directories.
Click to hide internal directories.