feat: import services and standardize Go 1.26.5
This commit is contained in:
74
apps/base/cms/internal/logic/post/create.go
Normal file
74
apps/base/cms/internal/logic/post/create.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Package post 提供文章相关的业务逻辑处理
|
||||
// 包括文章的创建、修改、删除、查询等功能
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-apps/cms/internal/models"
|
||||
"git.apinb.com/bsm-apps/cms/internal/utils"
|
||||
pb "git.apinb.com/bsm-apps/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
sdkUtils "git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// Create 创建新文章
|
||||
// 验证用户身份和文章数据,创建文章记录并关联分类、标签、附件等信息
|
||||
// 参数:
|
||||
// - ctx: 请求上下文,包含用户身份信息
|
||||
// - in: 文章数据,包含标题、内容、分类、标签等
|
||||
//
|
||||
// 返回:
|
||||
// - reply: 操作结果,包含文章ID和时间戳
|
||||
// - err: 错误信息
|
||||
func Create(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
|
||||
// 解析请求上下文,获取用户身份信息
|
||||
authCtx, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证必填字段
|
||||
if in.GetSiteIdentity() == "" || in.GetTitle() == "" || in.GetContent() == "" || in.GetKey() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 构建文章数据模型
|
||||
var data = &models.CmsPost{
|
||||
SiteIdentity: in.SiteIdentity,
|
||||
Title: in.Title,
|
||||
Hash: utils.FormatKey(in.Key), // 格式化文章键值,确保唯一性
|
||||
Description: in.Description,
|
||||
CoverPath: in.CoverPath,
|
||||
Author: in.Author,
|
||||
AuthorIdentity: authCtx.Identity, // 设置作者身份标识
|
||||
Content: in.Content,
|
||||
TargetUrl: in.TargetUrl,
|
||||
SourceUrl: in.SourceUrl,
|
||||
HasAccessory: in.HasAccessory,
|
||||
Types: in.PostType,
|
||||
AccessoryIdentityArray: in.AccessoryIdentityArray,
|
||||
CategoryIdentityArray: in.CategoryIdentityArray,
|
||||
TagsIdentityArray: in.TagsIdentityArray,
|
||||
}
|
||||
|
||||
// 生成文章唯一标识
|
||||
data.Identity = sdkUtils.UUID()
|
||||
|
||||
// 保存文章数据,包括关联的分类、标签、附件信息
|
||||
err = models.AddPost(data, in.AccessoryData, in.CategoryIdentityArray, in.TagsIdentityArray)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: vars.OK,
|
||||
Details: data.Identity,
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user