Documentation
¶
Index ¶
Constants ¶
const ( // App指的一个业务单元 // ioc.GetController(AppName) 获取到 这个业务模块的具体实现 // 简单的一中抽象, (常量/变量) 都是一种引用, 不用硬编码: blogs, 出行了100次 AppName = "blogs" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Blog ¶
type Blog struct {
*Meta
*CreateBlogRequest
}
数据 这个对象如何保存数据库里面 ? 通过匿名嵌套来组合产生新的结构体
func NewBlog ¶
func NewBlog(req *CreateBlogRequest) *Blog
通过一个构建函数来构建Blog 为什么要使用构建函数?为什么不直接使用struct 使用构造好按时保证兼容性: 把需要初始化的 在这个函数进行初始化, 有需要默认执行,补充默认值
type BlogSet ¶
func NewBlogSet ¶
func NewBlogSet() *BlogSet
type CreateBlogRequest ¶
type CreateBlogRequest struct {
// CreateAt --> createAt
// CreateAt --> create_at(通常我们json的tag也是采用蛇形缩写)
// json, gorm, text
// 文章标题
Title string `json:"title" gorm:"column:title" validate:"required"`
Author string `json:"author" gorm:"column:author" validate:"required"`
Content string `json:"content" validate:"required"`
// map[string]string orm是不知道如何入库的
// 直接存成json
Tags map[string]string `json:"tags" gorm:"serializer:json"`
// 文章是由状态
Status STATUS `json:"status"`
}
用户传入的数据 + 标题 + 作者 + 内容(Markdown) + 标签(map) GORM Object ---> Table Row Object struct Tag: gorm:"column:title" 你不定义tag,默认使用你json的tag Insert (title) VALUE (?)
func NewCreateBlogRequest ¶
func NewCreateBlogRequest() *CreateBlogRequest
func (*CreateBlogRequest) Validate ¶
func (req *CreateBlogRequest) Validate() error
检查用户提交的参数是否合法 使用这个来做校验: https://github.com/go-playground/validator
type DeleteBlogRequest ¶
type DeleteBlogRequest struct {
*DescribeBlogRequest
}
func NewDeleteBlogRequest ¶
func NewDeleteBlogRequest(id string) *DeleteBlogRequest
type DescribeBlogRequest ¶
type DescribeBlogRequest struct {
Id string
}
func NewDescribeBlogRequest ¶
func NewDescribeBlogRequest(id string) *DescribeBlogRequest
type Meta ¶
type Meta struct {
Id int `json:"id"`
// 直接用时间戳, 我选择用时间戳
// 我们是做后端, 一般数据库的时间对象是又时区约束
// 后端直接存储时间戳, 当需要展示的时候,由前端(Web,APP,...)负责带上用户的当前时区做展示
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
PublishedAt int64 `json:"pulished_at"`
}
文章的元数据: + 文章的Id + 创建时间 + 修改时间 + 发布时间
type QueryBlogRequest ¶
type QueryBlogRequest struct {
// 一页多少个
PageSize int
// 当前是那页
PageNumber int
// 模糊搜索, 搜索文章内容
Keywords string
// 条件过滤
Author string
}
1. 列表查询请求的参数 服务端分页, 默认执: 1页 20个 关键字查询, 模糊搜索 条件过滤, 比如过滤作者是谁的文章
func NewQueryBlogRequest ¶
func NewQueryBlogRequest() *QueryBlogRequest
func (*QueryBlogRequest) Offset ¶
func (r *QueryBlogRequest) Offset() int
type STATUS ¶
type STATUS int
自定义类型
func (STATUS) MarshalJSON ¶
Marshaler is the interface implemented by types that // can marshal themselves into valid JSON.
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
你自己定义当前类型的JSON输出, 一定要是一个合法的JSON "status": "xxx", "xxx"
func (*STATUS) UnmarshalJSON ¶
json.Unmarshaler 完成 STATUS类型的自定义反序列化
type Service ¶
type Service interface {
// 查询文章列表
QueryBlog(context.Context, *QueryBlogRequest) (*BlogSet, error)
// 查询单个文章
DescribeBlog(context.Context, *DescribeBlogRequest) (*Blog, error)
// 接口一定要保证很强一个兼容性
CreateBlog(context.Context, *CreateBlogRequest) (*Blog, error)
// 更新文章
UpdateBlog(context.Context, *UpdateBlogRequest) (*Blog, error)
// 删除文章, 返回删除的对象, 用前端提升, 用于对象最终
DeleteBlog(context.Context, *DeleteBlogRequest) (*Blog, error)
}
博客管理业务接口(CRUD) Blog 是CreateBlog这个接口的参数,是用户传递的数据 定义接口的时候, 你站在顶层来进行设计, 站在使用者的角度, 代码调用方
type UpdateBlogRequest ¶
type UpdateBlogRequest struct {
*DescribeBlogRequest
*CreateBlogRequest
}
func NewUpdateBlogRequest ¶
func NewUpdateBlogRequest(id string) *UpdateBlogRequest