refactor: reorganize modules and add Linux build tooling
This commit is contained in:
66
module/base/cms/internal/logic/category/create.go
Normal file
66
module/base/cms/internal/logic/category/create.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Package category 提供分类相关的业务逻辑处理
|
||||
// 包括分类的创建、修改、删除、查询等功能
|
||||
package category
|
||||
|
||||
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/utils"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
// Create 创建新分类
|
||||
// 验证用户身份和分类数据,创建分类记录
|
||||
// 参数:
|
||||
// - ctx: 请求上下文,包含用户身份信息
|
||||
// - in: 分类数据,包含标题、父级ID、介绍等
|
||||
//
|
||||
// 返回:
|
||||
// - reply: 操作结果,包含分类ID和时间戳
|
||||
// - err: 错误信息
|
||||
func Create(ctx context.Context, in *pb.CategoryItem) (reply *pb.StatusReply, err error) {
|
||||
// 解析请求上下文,验证用户身份
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 验证分类标题不能为空且长度不超过255字符
|
||||
if in.GetSiteIdentity() == "" || in.GetTitle() == "" || len(in.GetTitle()) > 255 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
// 构建分类数据模型
|
||||
data := models.CmsCategory{
|
||||
SiteIdentity: in.GetSiteIdentity(),
|
||||
ParentId: uint(in.GetParentId()), // 父级分类ID,0表示顶级分类
|
||||
CategoryKey: in.GetCategoryKey(), // 分类键值,用于URL等
|
||||
Title: in.GetTitle(), // 分类标题
|
||||
CoverPath: in.GetCoverPath(), // 分类封面图片路径
|
||||
Intro: in.GetIntro(), // 分类介绍
|
||||
}
|
||||
|
||||
// 生成分类唯一标识
|
||||
data.Identity = utils.UUID()
|
||||
|
||||
// 保存分类数据到数据库
|
||||
if err := impl.DBService.Model(&models.CmsCategory{}).Create(&data).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: vars.OK,
|
||||
Details: data.Identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
}
|
||||
33
module/base/cms/internal/logic/category/delete.go
Normal file
33
module/base/cms/internal/logic/category/delete.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package category
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// 删除分类
|
||||
func Delete(ctx context.Context, in *pb.DeleteCategoryRequest) (reply *pb.StatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if in.GetIdentity() == "" || len(in.GetIdentity()) > 255 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if err := models.DeleteCategory(in.GetIdentity()); err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
59
module/base/cms/internal/logic/category/fetch.go
Normal file
59
module/base/cms/internal/logic/category/fetch.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package category
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 分类列表
|
||||
func Fetch(ctx context.Context, in *pb.IdentRequest) (reply *pb.CategoryListReply, err error) {
|
||||
// list, cnt, err := logic.CategoryList()
|
||||
var (
|
||||
cnt int64 = 0
|
||||
list []models.CmsCategory
|
||||
)
|
||||
tx := impl.DBService.Debug().Model(&models.CmsCategory{})
|
||||
fmt.Println("in = ", in)
|
||||
if in.GetId() != 0 {
|
||||
tx = tx.Where("parent_id = ?", in.GetId())
|
||||
} else {
|
||||
tx = tx.Where("parent_id = 0")
|
||||
}
|
||||
err = tx.Order("created_at desc").Preload("Children").Count(&cnt).Find(&list).Error
|
||||
if err != nil {
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
out := &pb.CategoryListReply{
|
||||
Count: cnt,
|
||||
Data: fetchCategory(list),
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func fetchCategory(list []models.CmsCategory) (in []*pb.CategoryItem) {
|
||||
for _, v := range list {
|
||||
item := &pb.CategoryItem{
|
||||
Id: int64(v.ID),
|
||||
Identity: v.Identity,
|
||||
ParentId: int64(v.ParentId),
|
||||
CategoryKey: v.CategoryKey,
|
||||
Title: v.Title,
|
||||
CoverPath: v.CoverPath,
|
||||
Intro: v.Intro,
|
||||
CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
UpdatedAt: v.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||||
Child: make([]*pb.CategoryItem, 0, len(v.Children)),
|
||||
}
|
||||
if len(v.Children) > 0 {
|
||||
item.Child = fetchCategory(v.Children)
|
||||
}
|
||||
in = append(in, item)
|
||||
}
|
||||
return in
|
||||
}
|
||||
44
module/base/cms/internal/logic/category/modify.go
Normal file
44
module/base/cms/internal/logic/category/modify.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package category
|
||||
|
||||
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/types"
|
||||
)
|
||||
|
||||
// 修改分类
|
||||
func Modify(ctx context.Context, in *pb.ModifyCategoryRequest) (reply *pb.StatusReply, err error) {
|
||||
if in.GetSiteIdentity() == "" || in.GetIdentity() == "" || len(in.GetIdentity()) > 255 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
if in.GetTitle() == "" || len(in.GetTitle()) > 255 {
|
||||
return nil, errcode.ErrInvalidArgument
|
||||
}
|
||||
|
||||
data := &models.CmsCategory{
|
||||
SiteIdentity: in.SiteIdentity,
|
||||
ParentId: uint(in.GetParentId()),
|
||||
CategoryKey: in.GetCategoryKey(),
|
||||
Title: in.GetTitle(),
|
||||
CoverPath: in.GetCoverPath(),
|
||||
Intro: in.GetIntro(),
|
||||
Std_Identity: types.Std_Identity{Identity: in.Identity},
|
||||
}
|
||||
if err := impl.DBService.Where("identity = ?", data.Identity).Updates(data).Error; err != nil {
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
Code: 0,
|
||||
Message: "OK",
|
||||
Timeseq: time.Now().Unix(),
|
||||
}, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user