refactor: reorganize modules and add Linux build tooling
This commit is contained in:
51
module/base/cms/internal/logic/post/add_comment.go
Normal file
51
module/base/cms/internal/logic/post/add_comment.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// 发布评论
|
||||
func AddComment(ctx context.Context, in *pb.CommentItem) (*pb.StatusReply, error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var data = models.CmsComment{
|
||||
Std_Identity: types.Std_Identity{Identity: utils.ULID()},
|
||||
PostIdentity: in.PostIdentity,
|
||||
OwnerIdentity: auth.Identity,
|
||||
Role: auth.Role,
|
||||
ParentId: uint(in.ParentId),
|
||||
ReplyIdentity: in.ReplyIdentity,
|
||||
Cms: in.Cms,
|
||||
}
|
||||
authName := map[string]any{}
|
||||
err = impl.DBService.Table("mall_staff").Take(&authName, "identity=?", auth.Identity).Error
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
data.OwnerName = authName["name"].(string)
|
||||
err = models.AddComment(&data, auth.Identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: vars.OK,
|
||||
Details: data.Identity,
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
80
module/base/cms/internal/logic/post/comment_list.go
Normal file
80
module/base/cms/internal/logic/post/comment_list.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 评论列表
|
||||
func CommentList(ctx context.Context, in *pb.CommentListRequest) (*pb.CommentListResponse, error) {
|
||||
var (
|
||||
page = in.GetPage()
|
||||
size = in.GetSize()
|
||||
postIdentity = in.GetPostIdentity()
|
||||
list = make([]models.CmsComment, 0)
|
||||
cnt int64 = 0
|
||||
)
|
||||
// _, err := service.ParseMetaCtx(ctx, nil)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
if in.GetPostIdentity() == "" {
|
||||
fmt.Println("err = ", in.GetPostIdentity())
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
if in.GetPage() < 1 {
|
||||
in.Page = 1
|
||||
}
|
||||
if in.GetSize() < 10 {
|
||||
in.Size = 10
|
||||
}
|
||||
if in.GetSize() > 50 {
|
||||
in.Size = 50
|
||||
}
|
||||
// list, cnt, err := logic.CommentList(in.Page, in.Size, in.PostIdentity)
|
||||
if err := impl.DBService.Model(&models.CmsComment{}).Debug().Where("post_identity = ?", postIdentity).
|
||||
Order("created_at desc").Preload("Children").Count(&cnt).
|
||||
Limit(int(size)).Offset(int((page - 1) * size)).Find(&list).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.CommentListResponse{
|
||||
List: GetCommentList(list),
|
||||
Count: cnt,
|
||||
}, nil
|
||||
}
|
||||
func GetCommentList(list []models.CmsComment) (res []*pb.CommentItem) {
|
||||
for _, val := range list {
|
||||
var comment = &pb.CommentItem{
|
||||
Identity: val.Identity,
|
||||
PostIdentity: val.PostIdentity,
|
||||
OwnerName: val.OwnerName,
|
||||
OwnerIdentity: val.OwnerIdentity,
|
||||
Role: val.Role,
|
||||
ParentId: int64(val.ParentId),
|
||||
Cms: val.Cms,
|
||||
ReplyIdentity: val.ReplyIdentity,
|
||||
CreatedAt: val.CreatedAt.Format(time.DateTime),
|
||||
UpdatedAt: val.UpdatedAt.Format(time.DateTime),
|
||||
LikeHits: val.LikeHits,
|
||||
UnlikeHits: val.UnlikeHits,
|
||||
CommentHits: val.CommentHits,
|
||||
}
|
||||
|
||||
if len(val.Children) != 0 {
|
||||
comment.List = GetCommentList(val.Children)
|
||||
}
|
||||
res = append(res, comment)
|
||||
|
||||
}
|
||||
return res
|
||||
}
|
||||
74
module/base/cms/internal/logic/post/create.go
Normal file
74
module/base/cms/internal/logic/post/create.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Package post 提供文章相关的业务逻辑处理
|
||||
// 包括文章的创建、修改、删除、查询等功能
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
"bsm/full/module/base/cms/internal/utils"
|
||||
pb "bsm/full/module/base/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
|
||||
}
|
||||
40
module/base/cms/internal/logic/post/delete.go
Normal file
40
module/base/cms/internal/logic/post/delete.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 删除文章
|
||||
func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// valildate request id,identity.
|
||||
if in.Id == 0 && in.Identity == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.DeletePost(in.Identity, "") // Todo:
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
30
module/base/cms/internal/logic/post/delete_comment.go
Normal file
30
module/base/cms/internal/logic/post/delete_comment.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
)
|
||||
|
||||
// DeleteComment 删除评论
|
||||
func DeleteComment(ctx context.Context, in *pb.DeleteCommentRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.DeleteComment(in.Identity)
|
||||
if err != nil {
|
||||
fmt.Println()
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
30
module/base/cms/internal/logic/post/desc_comment_like.go
Normal file
30
module/base/cms/internal/logic/post/desc_comment_like.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
)
|
||||
|
||||
// DescCommentLike 减少评论点赞数
|
||||
func DescCommentLike(ctx context.Context, in *pb.CommentOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetCommentIdentity() == "" || in.GetOpIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.IncrOrDescCommentField(in.CommentIdentity, "like_hits", true)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
30
module/base/cms/internal/logic/post/desc_comment_unlike.go
Normal file
30
module/base/cms/internal/logic/post/desc_comment_unlike.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
)
|
||||
|
||||
// DescCommentUnlike 减少评论踩
|
||||
func DescCommentUnlike(ctx context.Context, in *pb.CommentOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetCommentIdentity() == "" || in.GetOpIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.IncrOrDescCommentField(in.CommentIdentity, "unlike_hits", true)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
31
module/base/cms/internal/logic/post/desc_post_like.go
Normal file
31
module/base/cms/internal/logic/post/desc_post_like.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// DescPostLike 处理帖子的点赞减少操作
|
||||
func DescPostLike(ctx context.Context, in *pb.PostOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetOpIdentity() == "" || in.GetPostIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.IncrOrDescPostField(in.PostIdentity, "like_hits", true)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
31
module/base/cms/internal/logic/post/desc_post_unlike.go
Normal file
31
module/base/cms/internal/logic/post/desc_post_unlike.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// DescPostUnlike 处理用户取消点赞帖子的操作
|
||||
func DescPostUnlike(ctx context.Context, in *pb.PostOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetOpIdentity() == "" || in.GetPostIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.IncrOrDescPostField(in.PostIdentity, "unlike_hits", true)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
17
module/base/cms/internal/logic/post/ext.go
Normal file
17
module/base/cms/internal/logic/post/ext.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func fmtKey(in string) string {
|
||||
in = strings.ToLower(in)
|
||||
in = strings.TrimSpace(in)
|
||||
|
||||
re := regexp.MustCompile(`\s+`)
|
||||
in = re.ReplaceAllString(in, " ")
|
||||
in = strings.ReplaceAll(in, " ", "-")
|
||||
|
||||
return in
|
||||
}
|
||||
107
module/base/cms/internal/logic/post/fetch.go
Normal file
107
module/base/cms/internal/logic/post/fetch.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 文章列表
|
||||
func Fetch(ctx context.Context, in *pb.PostListRequest) (reply *pb.PostListReply, err error) {
|
||||
if in.GetPage() <= 0 {
|
||||
in.Page = 1
|
||||
}
|
||||
if in.GetSize() <= 0 {
|
||||
in.Size = 10
|
||||
}
|
||||
if in.GetSize() > 50 {
|
||||
in.Size = 50
|
||||
}
|
||||
list, cnt, err := models.PostList(in.Page, in.Size, in.CategoryIdentity, in.Keyword, int64(in.Type))
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
var res = pb.PostListReply{
|
||||
Count: cnt,
|
||||
Data: make([]*pb.PostItem, 0),
|
||||
}
|
||||
for _, val := range list {
|
||||
var (
|
||||
accessoryData = make([]*pb.AccessoryItem, 0)
|
||||
accessories = make([]string, 0)
|
||||
|
||||
categoryData = make([]*pb.CategoryItem, 0)
|
||||
categories = make([]string, 0)
|
||||
|
||||
TagsData = make([]*pb.TagsItem, 0)
|
||||
tagsIdentityArray = make([]string, 0)
|
||||
)
|
||||
for _, accessory := range val.Accessories {
|
||||
accessoryData = append(accessoryData, &pb.AccessoryItem{
|
||||
Identity: accessory.Identity,
|
||||
Title: accessory.Title,
|
||||
FilePath: accessory.FilePath,
|
||||
})
|
||||
accessories = append(accessories, accessory.Identity)
|
||||
}
|
||||
for _, category := range val.Categories {
|
||||
categoryData = append(categoryData, &pb.CategoryItem{
|
||||
Id: int64(category.Category.ID),
|
||||
Identity: category.Category.Identity,
|
||||
ParentId: int64(category.Category.ParentId),
|
||||
Title: category.Category.Title,
|
||||
CoverPath: category.Category.CoverPath,
|
||||
Intro: category.Category.Intro,
|
||||
})
|
||||
categories = append(categories, category.CategoryIdentity)
|
||||
}
|
||||
for _, tags := range val.Tags {
|
||||
TagsData = append(TagsData, &pb.TagsItem{
|
||||
Id: int64(tags.Tags.ID),
|
||||
Identity: tags.TagsIdentity,
|
||||
Title: tags.Tags.Title,
|
||||
Intro: tags.Tags.Intro,
|
||||
CoverPath: tags.Tags.CoverPath,
|
||||
})
|
||||
tagsIdentityArray = append(tagsIdentityArray, tags.TagsIdentity)
|
||||
}
|
||||
|
||||
res.Data = append(res.Data, &pb.PostItem{
|
||||
Identity: val.Identity,
|
||||
OwnerId: int64(val.OwnerID),
|
||||
OwnerIdentity: val.OwnerIdentity,
|
||||
Title: val.Title,
|
||||
CoverPath: val.CoverPath,
|
||||
Author: val.Author,
|
||||
AuthorIdentity: val.AuthorIdentity,
|
||||
Content: val.Content,
|
||||
TargetUrl: val.TargetUrl,
|
||||
SourceUrl: val.SourceUrl,
|
||||
Hits: val.Hits,
|
||||
HasAccessory: val.HasAccessory,
|
||||
CreatedAt: val.CreatedAt.Format(time.DateTime),
|
||||
UpdatedAt: val.UpdatedAt.Format(time.DateTime),
|
||||
Description: val.Description,
|
||||
LikeHits: val.LikeHits,
|
||||
UnlikeHits: val.UnlikeHits,
|
||||
CommentHits: val.CommentHits,
|
||||
PostType: val.Types,
|
||||
Key: val.Hash,
|
||||
Hash: val.Hash,
|
||||
AccessoryData: accessoryData,
|
||||
AccessoryIdentityArray: accessories,
|
||||
CategoryData: categoryData,
|
||||
CategoryIdentityArray: categories,
|
||||
TagsData: TagsData,
|
||||
TagsIdentityArray: tagsIdentityArray,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
return &res, nil
|
||||
}
|
||||
82
module/base/cms/internal/logic/post/get_by_identity.go
Normal file
82
module/base/cms/internal/logic/post/get_by_identity.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 获取文章详情 By Identity
|
||||
func GetByIdentity(ctx context.Context, in *pb.GetPostRequest) (reply *pb.PostItem, err error) {
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
data, err := models.GetPost("identity", in.Identity)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
var res = &pb.PostItem{}
|
||||
if data != nil {
|
||||
res.Identity = data.Identity
|
||||
res.OwnerId = int64(data.OwnerID)
|
||||
res.OwnerIdentity = data.OwnerIdentity
|
||||
res.Title = data.Title
|
||||
res.CoverPath = data.CoverPath
|
||||
res.Author = data.Author
|
||||
res.AuthorIdentity = data.AuthorIdentity
|
||||
res.Content = data.Content
|
||||
res.TargetUrl = data.TargetUrl
|
||||
res.SourceUrl = data.SourceUrl
|
||||
res.Hits = data.Hits
|
||||
res.HasAccessory = data.HasAccessory
|
||||
res.CreatedAt = data.CreatedAt.String()
|
||||
res.UpdatedAt = data.UpdatedAt.String()
|
||||
res.Description = data.Description
|
||||
res.LikeHits = data.LikeHits
|
||||
res.UnlikeHits = data.UnlikeHits
|
||||
res.CommentHits = data.CommentHits
|
||||
res.PostType = data.Types
|
||||
res.Key = data.Hash
|
||||
res.Hash = data.Hash
|
||||
res.Lang = data.Lang
|
||||
res.SourceOrigin = data.SourceOrigin
|
||||
res.Rights = data.Rights
|
||||
res.ExtendUrl = data.ExtendUrl
|
||||
res.ExtendData = data.ExtendData
|
||||
res.ExtendImg = data.ExtendImg
|
||||
res.ExtendDesc = data.ExtendDesc
|
||||
res.Published = data.Published.Format(time.DateTime)
|
||||
}
|
||||
for _, val := range data.Accessories {
|
||||
res.AccessoryData = append(res.AccessoryData, &pb.AccessoryItem{
|
||||
Identity: val.Identity,
|
||||
Title: val.Title,
|
||||
FilePath: val.FilePath,
|
||||
CreatedAt: val.CreatedAt.Format(time.DateTime),
|
||||
})
|
||||
}
|
||||
for _, val := range data.Accessories {
|
||||
res.AccessoryIdentityArray = append(res.AccessoryIdentityArray, val.Identity)
|
||||
}
|
||||
for _, val := range data.Tags {
|
||||
res.TagsData = append(res.TagsData, &pb.TagsItem{
|
||||
Id: int64(val.ID),
|
||||
Identity: val.Tags.Identity,
|
||||
Title: val.Tags.Title,
|
||||
Intro: val.Tags.Intro,
|
||||
CoverPath: val.Tags.CoverPath,
|
||||
CreatedAt: val.Tags.CreatedAt.Format(time.DateTime),
|
||||
})
|
||||
}
|
||||
for _, val := range data.Tags {
|
||||
res.TagsIdentityArray = append(res.TagsIdentityArray, val.Tags.Identity)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
50
module/base/cms/internal/logic/post/get_by_key.go
Normal file
50
module/base/cms/internal/logic/post/get_by_key.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
"bsm/full/module/base/cms/internal/utils"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 获取文章详情 By Key
|
||||
func GetByKey(ctx context.Context, in *pb.GetPostByKeyRequest) (reply *pb.PostItem, err error) {
|
||||
if in.GetKey() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
data, err := models.GetPost("hash", utils.FormatKey(in.Key))
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
var res = &pb.PostItem{}
|
||||
if data != nil {
|
||||
res.Identity = data.Identity
|
||||
res.OwnerId = int64(data.OwnerID)
|
||||
res.OwnerIdentity = data.OwnerIdentity
|
||||
res.Title = data.Title
|
||||
res.CoverPath = data.CoverPath
|
||||
res.Author = data.Author
|
||||
res.AuthorIdentity = data.AuthorIdentity
|
||||
res.Content = data.Content
|
||||
res.TargetUrl = data.TargetUrl
|
||||
res.SourceUrl = data.SourceUrl
|
||||
res.Hits = data.Hits
|
||||
res.HasAccessory = data.HasAccessory
|
||||
res.CreatedAt = data.CreatedAt.String()
|
||||
res.UpdatedAt = data.UpdatedAt.String()
|
||||
res.Description = data.Description
|
||||
res.LikeHits = data.LikeHits
|
||||
res.UnlikeHits = data.UnlikeHits
|
||||
res.CommentHits = data.CommentHits
|
||||
res.PostType = data.Types
|
||||
res.Key = data.Hash
|
||||
res.Hash = data.Hash
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
41
module/base/cms/internal/logic/post/incr_comment_like.go
Normal file
41
module/base/cms/internal/logic/post/incr_comment_like.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
)
|
||||
|
||||
// IncrCommentLike 评论点赞处理
|
||||
func IncrCommentLike(ctx context.Context, in *pb.CommentOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var (
|
||||
cnt int64 = 0
|
||||
)
|
||||
|
||||
if in.GetCommentIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
err = models.IncrOrDescCommentField(in.CommentIdentity, "like_hits", false)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if err := impl.DBService.Model(&models.CmsComment{}).Where("identity = ?", in.CommentIdentity).
|
||||
Pluck("like_hits", &cnt).Error; err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Details: utils.Int642String(cnt),
|
||||
}, nil
|
||||
}
|
||||
36
module/base/cms/internal/logic/post/incr_comment_unlike.go
Normal file
36
module/base/cms/internal/logic/post/incr_comment_unlike.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/utils"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
)
|
||||
|
||||
// 评论点踩处理
|
||||
func IncrCommentUnlike(ctx context.Context, in *pb.CommentOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var cnt int64
|
||||
if in.GetCommentIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.IncrOrDescCommentField(in.CommentIdentity, "unlike_hits", false)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
if err := impl.DBService.Model(&models.CmsComment{}).Where("identity = ?", in.CommentIdentity).
|
||||
Pluck("unlike_hits", &cnt).Error; err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Details: utils.Int642String(cnt),
|
||||
}, nil
|
||||
}
|
||||
50
module/base/cms/internal/logic/post/incr_post_like.go
Normal file
50
module/base/cms/internal/logic/post/incr_post_like.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Package post 提供文章相关的业务逻辑处理
|
||||
// 包括文章的创建、修改、删除、查询、点赞等功能
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// IncrPostLike 增加文章点赞数
|
||||
// 验证用户身份后,增加指定文章的点赞计数
|
||||
// 参数:
|
||||
// - ctx: 请求上下文,包含用户身份信息
|
||||
// - in: 操作请求,包含操作ID和文章ID
|
||||
//
|
||||
// 返回:
|
||||
// - *pb.StatusReply: 操作结果
|
||||
// - error: 错误信息
|
||||
func IncrPostLike(ctx context.Context, in *pb.PostOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
// 解析请求上下文,验证用户身份
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证必填参数
|
||||
if in.GetOpIdentity() == "" || in.GetPostIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 增加文章点赞数
|
||||
err = models.IncrOrDescPostField(in.PostIdentity, "like_hits", false)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
29
module/base/cms/internal/logic/post/incr_post_unlike.go
Normal file
29
module/base/cms/internal/logic/post/incr_post_unlike.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 文章点踩处理
|
||||
func IncrPostUnlike(ctx context.Context, in *pb.PostOpIdentityRequest) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if in.GetOpIdentity() == "" || in.GetPostIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
err = models.IncrOrDescPostField(in.PostIdentity, "unlike_hits", false)
|
||||
if err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
54
module/base/cms/internal/logic/post/modify.go
Normal file
54
module/base/cms/internal/logic/post/modify.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
"bsm/full/module/base/cms/internal/utils"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
)
|
||||
|
||||
// 修改文章
|
||||
func Modify(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.GetSiteIdentity() == "" || in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
var data = &models.CmsPost{
|
||||
SiteIdentity: in.SiteIdentity,
|
||||
Title: in.Title,
|
||||
Description: in.Description,
|
||||
CoverPath: in.CoverPath,
|
||||
Author: in.Author,
|
||||
Content: in.Content,
|
||||
TargetUrl: in.TargetUrl,
|
||||
SourceUrl: in.SourceUrl,
|
||||
HasAccessory: in.HasAccessory,
|
||||
Types: in.PostType,
|
||||
Hash: utils.FormatKey(in.Key),
|
||||
Lang: in.Lang,
|
||||
SourceOrigin: in.SourceOrigin,
|
||||
Rights: in.Rights,
|
||||
ExtendUrl: in.ExtendUrl,
|
||||
ExtendData: in.ExtendData,
|
||||
ExtendImg: in.ExtendImg,
|
||||
ExtendDesc: in.ExtendDesc,
|
||||
}
|
||||
err = models.ModifyPost(in.Identity, data, in.AccessoryIdentityArray, in.CategoryIdentityArray, in.TagsIdentityArray)
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
37
module/base/cms/internal/logic/post/modify_comment.go
Normal file
37
module/base/cms/internal/logic/post/modify_comment.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/service"
|
||||
"git.apinb.com/bsm-sdk/core/types"
|
||||
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
)
|
||||
|
||||
// 修改评论
|
||||
func ModifyComment(ctx context.Context, in *pb.CommentItem) (*pb.StatusReply, error) {
|
||||
_, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.GetIdentity() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
var data = models.CmsComment{
|
||||
Std_Identity: types.Std_Identity{Identity: in.Identity},
|
||||
PostIdentity: in.PostIdentity,
|
||||
ParentId: uint(in.ParentId),
|
||||
ReplyIdentity: in.ReplyIdentity,
|
||||
Cms: in.Cms,
|
||||
}
|
||||
// err = logic.ModifyComment(&data)
|
||||
if err := impl.DBService.Where("identity = ?", data.Identity).Updates(data).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.StatusReply{}, nil
|
||||
}
|
||||
74
module/base/cms/internal/logic/post/search.go
Normal file
74
module/base/cms/internal/logic/post/search.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bsm/full/module/base/cms/internal/impl"
|
||||
"bsm/full/module/base/cms/internal/models"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/errcode"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
)
|
||||
|
||||
// 搜索文章
|
||||
func Search(ctx context.Context, in *pb.SearchRequest) (reply *pb.PostListReply, err error) {
|
||||
if in.GetKeyword() == "" {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
var (
|
||||
cnt int64 = 0
|
||||
pageNo int64 = in.GetPageNo()
|
||||
pageSize int64 = in.GetPageSize()
|
||||
offset int64 = (pageNo - 1) * pageSize
|
||||
)
|
||||
|
||||
// vlidate request page_no,page_size.
|
||||
if pageNo < 1 {
|
||||
pageNo = 1
|
||||
offset = 0
|
||||
}
|
||||
if pageSize < 50 {
|
||||
pageSize = 50
|
||||
offset = (pageNo - 1) * pageSize
|
||||
}
|
||||
|
||||
var data []*models.CmsPost
|
||||
tx := impl.DBService.Model(&models.CmsPost{}).Where("title like ? or Cms like ?", "%"+in.Keyword+"%", "%"+in.Keyword+"%")
|
||||
if err := tx.Count(&cnt).Offset(int(offset)).Limit(int(pageSize)).Find(&data).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
replyData := make([]*pb.PostItem, len(data))
|
||||
for _, val := range data {
|
||||
replyData = append(replyData, &pb.PostItem{
|
||||
Identity: val.Identity,
|
||||
OwnerId: int64(val.OwnerID),
|
||||
OwnerIdentity: val.OwnerIdentity,
|
||||
Title: val.Title,
|
||||
CoverPath: val.CoverPath,
|
||||
Author: val.Author,
|
||||
AuthorIdentity: val.AuthorIdentity,
|
||||
Content: val.Content,
|
||||
TargetUrl: val.TargetUrl,
|
||||
SourceUrl: val.SourceUrl,
|
||||
Hits: val.Hits,
|
||||
HasAccessory: val.HasAccessory,
|
||||
CreatedAt: val.CreatedAt.String(),
|
||||
UpdatedAt: val.UpdatedAt.String(),
|
||||
Description: val.Description,
|
||||
LikeHits: val.LikeHits,
|
||||
UnlikeHits: val.UnlikeHits,
|
||||
CommentHits: val.CommentHits,
|
||||
PostType: val.Types,
|
||||
Key: val.Hash,
|
||||
Hash: val.Hash,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.PostListReply{
|
||||
Count: cnt,
|
||||
Data: replyData,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user