blog

package
v0.0.0-...-f4276bb Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2023 License: MIT Imports: 5 Imported by: 0

README

文章管理

  • 定义接口: 保护数据结构, 叫做领域接口
  • 接口的具体实现:
  • 每个模块管理着自己的数据, 如果别的业务模块想要查询 这个领域的数据, 必须通过领域接口, 不能绕过接口接口访问数据

分层2层:

  • 业务的定义, 只是声明: blog(这个包就是负责定义业务)
  • 业务的实现
    • 实现接口 (impl)
    • 接口约束 var _ blog.Service = &impl{}
    • 实现后能不能正常工作: 需要我们写单元测试, 采用TDD(Test Drive Develop)来方式来编写业务代码
    • 业务功能测试OK, 可以进入接口的开发阶段
  • 业务的接口(api包, 通过网络对外提供使用能力, protocal), 其他程序开发者, 通过你暴露的接口来与你的服务进行交互
    • http: 只实现这个
      • 定义一个对象(可以提供HTTP业务能力处理的对象), 面向对象, 对象核心能力, 有项目责任
    • grpc
    • thirft
    • tcp
  • 接入http server 启动服务

面向对象里的核心, 区分控制器对象和数据对象

Documentation

Index

Constants

View Source
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 使用构造好按时保证兼容性: 把需要初始化的 在这个函数进行初始化, 有需要默认执行,补充默认值

func (*Blog) String

func (s *Blog) String() string

Stringer

func (*Blog) TableName

func (i *Blog) TableName() string
type Tabler interface {
	TableName() string
}

定义gorm 存入数据时表的名称

type BlogSet

type BlogSet struct {
	// 总共多个篇文章
	Total int64   `json:"total"`
	Items []*Blog `json:"items"`
}

func NewBlogSet

func NewBlogSet() *BlogSet

func (*BlogSet) String

func (s *BlogSet) String() string

Stringer

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 + 创建时间 + 修改时间 + 发布时间

func NewMeta

func NewMeta() *Meta

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

自定义类型

const (
	// 使用0表示默认值, 默认就是草稿状态,
	STATUS_DRAFT STATUS = iota
	// 已发布状态
	STATUS_PUBLISHED
)

这个类型由哪些值

func (STATUS) MarshalJSON

func (s STATUS) MarshalJSON() ([]byte, error)

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

func (s *STATUS) UnmarshalJSON(b []byte) error

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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