miniblog

module
v0.0.0-...-dcf75d9 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2025 License: MIT

README

miniblog

如何新增一个新资源

因为整个 miniblog 项目非常规范,所以可以快速添加一个新的 REST 资源。新增 REST 资源时,需要先给 REST 资源起以下几个名字:

类型:资源的类型名称,例如 Post,使用大写驼峰格式; 单数:资源的单数形式,例如 post,使用小写驼峰格式,首字母小写; 复数:资源的复数形式,例如 posts,使用小写驼峰格式,首字母小写。

这里假设需要新增一个 Comment 资源,用来记录博客的评论,并将这些记录保存在数据库中。可以按以下顺序来实现 Comment 资源相关的功能代码:

定义 API 接口; 编译 Protobuf 文件; 在数据库中创建 comment 表,并修改 cmd/gen-gorm-model/gen_gorm_model.go 文件,添加 comment 表的 GORM Model 生成代码; 运行 go run cmd/gen-gorm-model/gen_gorm_model.go 命令,生成 GORM Model; 完善 API 接口请求参数的默认值设置方法(修改 pkg/api/apiserver/v1/comment.pb.defaults.go 文件); 实现 API 接口的请求参数校验方法(在文件 internal/apiserver/pkg/validation/comment.go 中实现); 实现 Comment 资源的 Store 层代码(在文件 internal/apiserver/store/comment.go 中实现); 实现 Comment 资源的 Model 和 Proto 的转换函数(在 internal/apiserver/pkg/conversion/comment.go 文件中实现); 实现 Comment 资源的 Biz 层代码(在文件 internal/apiserver/biz/v1/comment/comment.go 中实现); 实现 Comment 资源的 Handler 层代码(在文件 internal/apiserver/handler/comment.go 中实现)。

在具体实现时,可参考已有资源的实现。例如复制已有资源的实现文件,通过替换字符串等方式,快速实现 Comment 资源的代码。

Jump to

Keyboard shortcuts

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