2024-12-01 16:05:07 +08:00
|
|
|
package blog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-12-08 14:46:15 +08:00
|
|
|
"github.com/infraboard/mcube/v2/exception"
|
|
|
|
"github.com/infraboard/mcube/v2/ioc/config/validator"
|
|
|
|
"github.com/infraboard/mcube/v2/tools/pretty"
|
2024-12-01 16:05:07 +08:00
|
|
|
"gitlab.com/go-course-project/go17/vblog/utils"
|
|
|
|
)
|
|
|
|
|
2024-12-08 14:46:15 +08:00
|
|
|
func NewBlogSet() *BlogSet {
|
|
|
|
return &BlogSet{
|
|
|
|
Items: []*Blog{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 16:05:07 +08:00
|
|
|
type BlogSet struct {
|
2024-12-08 14:46:15 +08:00
|
|
|
Total int64 `json:"total"`
|
2024-12-01 16:05:07 +08:00
|
|
|
Items []*Blog `json:"items"`
|
|
|
|
}
|
|
|
|
|
2025-01-19 09:33:31 +08:00
|
|
|
func (b *BlogSet) String() string {
|
|
|
|
return pretty.ToJSON(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlogSet) Add(item *Blog) {
|
|
|
|
b.Items = append(b.Items, item)
|
|
|
|
b.Total++
|
|
|
|
}
|
|
|
|
|
2025-01-12 18:31:26 +08:00
|
|
|
func NewBlog(in *CreateBlogRequest) (*Blog, error) {
|
2024-12-08 14:46:15 +08:00
|
|
|
if err := in.Validate(); err != nil {
|
|
|
|
return nil, exception.NewBadRequest("参数异常: %s", err)
|
|
|
|
}
|
|
|
|
return &Blog{
|
|
|
|
ResourceMeta: *utils.NewResourceMeta(),
|
|
|
|
CreateBlogRequest: *in,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-12-01 16:05:07 +08:00
|
|
|
type Blog struct {
|
|
|
|
// 存放到数据里的对象的远数据信息
|
|
|
|
utils.ResourceMeta
|
|
|
|
// 创建是的具体信息
|
|
|
|
CreateBlogRequest
|
|
|
|
// 文章状态
|
|
|
|
Status
|
|
|
|
}
|
|
|
|
|
2024-12-08 14:46:15 +08:00
|
|
|
func (t *Blog) String() string {
|
|
|
|
return pretty.ToJSON(t)
|
|
|
|
}
|
|
|
|
|
2024-12-01 16:05:07 +08:00
|
|
|
func (t *Blog) TableName() string {
|
|
|
|
return "blogs"
|
|
|
|
}
|
|
|
|
|
|
|
|
type CreateBlogRequest struct {
|
|
|
|
// 标题
|
2024-12-08 14:46:15 +08:00
|
|
|
Title string `json:"title" gorm:"column:title;type:varchar(200)" validate:"required"`
|
2024-12-01 16:05:07 +08:00
|
|
|
// 摘要
|
2024-12-08 14:46:15 +08:00
|
|
|
Summary string `json:"summary" gorm:"column:summary;type:text" validate:"required"`
|
2024-12-01 16:05:07 +08:00
|
|
|
// 内容
|
2024-12-08 14:46:15 +08:00
|
|
|
Content string `json:"content" gorm:"column:content;type:text" validate:"required"`
|
2024-12-01 16:05:07 +08:00
|
|
|
// 分类
|
2024-12-08 14:46:15 +08:00
|
|
|
Category string `json:"category" gorm:"column:category;type:varchar(200);index" validate:"required"`
|
2024-12-01 16:05:07 +08:00
|
|
|
// 标签
|
|
|
|
Tags map[string]string `json:"tags" gorm:"column:tags;serializer:json"`
|
|
|
|
}
|
|
|
|
|
2024-12-08 14:46:15 +08:00
|
|
|
func (r *CreateBlogRequest) Validate() error {
|
|
|
|
return validator.Validate(r)
|
|
|
|
}
|
|
|
|
|
2024-12-01 16:05:07 +08:00
|
|
|
type Status struct {
|
2024-12-01 17:17:51 +08:00
|
|
|
StatusSpec
|
2024-12-01 16:05:07 +08:00
|
|
|
// 状态变化的时间, 拿发布时间
|
|
|
|
ChangeAt *time.Time `json:"change_at" gorm:"column:change_at"`
|
|
|
|
}
|
2024-12-01 17:17:51 +08:00
|
|
|
|
|
|
|
type StatusSpec struct {
|
|
|
|
// 0: 草稿, 1: 已发布, 2: 审核 ...
|
|
|
|
Stage STAGE `json:"stage" gorm:"column:stage;type:tinyint(1);index"`
|
|
|
|
}
|