go17/vblog/apps/blog/interface.go

114 lines
2.5 KiB
Go
Raw Normal View History

2024-12-01 11:06:09 +08:00
package blog
2024-12-01 16:05:07 +08:00
import (
"context"
2025-01-12 17:22:23 +08:00
"strconv"
2024-12-08 16:20:18 +08:00
"strings"
2024-12-01 16:05:07 +08:00
2024-12-15 12:10:11 +08:00
"github.com/infraboard/mcube/v2/ioc"
2024-12-01 16:05:07 +08:00
"gitlab.com/go-course-project/go17/vblog/utils"
)
2024-12-15 12:10:11 +08:00
const (
AppName = "blog"
)
func GetService() Service {
return ioc.Controller().Get(AppName).(Service)
}
2024-12-01 11:06:09 +08:00
type Service interface {
// 创建博客
2024-12-01 16:05:07 +08:00
CreateBlog(context.Context, *CreateBlogRequest) (*Blog, error)
2024-12-01 11:06:09 +08:00
// 博客列表查询
2024-12-01 16:05:07 +08:00
QueryBlog(context.Context, *QueryBlogRequest) (*BlogSet, error)
2024-12-01 11:06:09 +08:00
// 博客详情查询
2024-12-01 17:17:51 +08:00
DescribeBlog(context.Context, *DescribeBlogRequest) (*Blog, error)
2024-12-01 11:06:09 +08:00
// 博客编辑
2024-12-01 17:17:51 +08:00
UpdateBlog(context.Context, *UpdateBlogRequest) (*Blog, error)
2024-12-01 11:06:09 +08:00
// 发布
2024-12-01 17:17:51 +08:00
PublishBlog(context.Context, *PublishBlogRequest) (*Blog, error)
2024-12-01 11:06:09 +08:00
// 删除
2024-12-01 17:17:51 +08:00
DeleteBlog(context.Context, *DeleteBlogRequest) error
}
2025-01-12 17:22:23 +08:00
func NewDeleteBlogRequest[T int | string](id T) *DeleteBlogRequest {
var idValue int
switch v := any(id).(type) {
case int:
idValue = v
case string:
parsedID, _ := strconv.ParseUint(v, 10, 0)
idValue = int(parsedID)
}
return &DeleteBlogRequest{
GetRequest: utils.GetRequest{
Id: uint(idValue),
},
}
}
2024-12-01 17:17:51 +08:00
type DeleteBlogRequest struct {
utils.GetRequest
}
type PublishBlogRequest struct {
utils.GetRequest
StatusSpec
}
type UpdateBlogRequest struct {
utils.GetRequest
CreateBlogRequest
}
2025-01-12 18:31:26 +08:00
func NewDescribeBlogRequest(id uint) *DescribeBlogRequest {
return &DescribeBlogRequest{
GetRequest: *utils.NewGetRequest(id),
}
}
2024-12-01 17:17:51 +08:00
type DescribeBlogRequest struct {
utils.GetRequest
2024-12-01 11:06:09 +08:00
}
2024-12-01 16:05:07 +08:00
2024-12-08 14:46:15 +08:00
func NewQueryBlogRequest() *QueryBlogRequest {
return &QueryBlogRequest{
PageRequest: *utils.NewPageRequest(),
Tags: map[string]string{},
}
}
2024-12-01 16:05:07 +08:00
type QueryBlogRequest struct {
// 分页参数
utils.PageRequest
// 文章标题模糊匹配, Golang
2024-12-08 16:20:18 +08:00
Keywords string `json:"keywords" form:"keywords"`
2024-12-01 16:05:07 +08:00
// 状态过滤参数, 作者nil, 访客: STAGE_PUBLISHED
2024-12-08 16:20:18 +08:00
Stage *STAGE `json:"stage" form:"stage"`
2024-12-01 16:05:07 +08:00
// 查询某个用户具体的文章: 给作者用的
2024-12-08 16:20:18 +08:00
CreateBy string `json:"create_by" form:"create_by"`
2024-12-01 16:05:07 +08:00
// 分类
2024-12-08 16:20:18 +08:00
Category string `json:"category" form:"category"`
2024-12-01 16:05:07 +08:00
// 查询Tag相关的文章
2024-12-08 14:46:15 +08:00
// SELECT *
// FROM my_table
// WHERE data->>'$.name' = '某个名字';
2024-12-08 16:20:18 +08:00
Tags map[string]string `json:"tag" form:"-"`
}
// tags=key=value,key=value
// key=value,key=value
// key=value
func (r *QueryBlogRequest) SetTag(tag string) {
kvItem := strings.Split(tag, ",")
for i := range kvItem {
kvString := kvItem[i]
kv := strings.Split(kvString, "=")
if len(kv) > 1 {
r.Tags[kv[0]] = strings.Join(kv[1:], "=")
}
}
2024-12-01 16:05:07 +08:00
}