实现category service基础接口

This commit is contained in:
2025-05-31 16:27:01 +08:00
parent faa6a35475
commit e5446bf836
33 changed files with 1420 additions and 67 deletions

View File

@ -0,0 +1,103 @@
package logic
import (
"context"
"strings"
"godemo/category/category"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type CreateCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCreateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCategoryLogic {
return &CreateCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateCategoryLogic) CreateCategory(req *types.CreateCategoryReq) (resp *types.CreateCategoryResp, err error) {
// 1. 参数验证
if err := validateCreateRequest(req); err != nil {
return nil, err
}
// 2. 准备 RPC 请求
rpcReq := &category.CreateCategoryRequest{
SystemId: req.SystemID,
Name: req.Name,
Description: req.Description,
}
// 可选参数处理
if req.Alias != "" {
rpcReq.Alias = req.Alias
}
if req.ParentID != "" {
rpcReq.ParentId = req.ParentID
}
// 3. 调用 RPC 服务
rpcResp, rpcErr := l.svcCtx.CategoryRpc.CreateCategory(l.ctx, rpcReq)
if rpcErr != nil {
return nil, rpcErr
}
// 4. 构建响应
resp = &types.CreateCategoryResp{
ID: rpcResp.Category.Id,
}
l.Logger.Infof("Category created successfully: ID=%s, Name=%s", resp.ID, req.Name)
return resp, nil
}
// validateCreateRequest 验证创建分类请求
func validateCreateRequest(req *types.CreateCategoryReq) error {
// 必需字段检查
if strings.TrimSpace(req.SystemID) == "" {
return status.Error(codes.InvalidArgument, "systemId不能为空")
}
if strings.TrimSpace(req.Name) == "" {
return status.Error(codes.InvalidArgument, "name不能为空")
}
// 名称长度限制
if len(req.Name) > 50 {
return status.Error(codes.InvalidArgument, "alias cannot exceed 50 characters")
}
// 别名格式验证 (只允许字母、数字、连字符和下划线)
// if req.Alias != "" {
// if len(req.Alias) > 50 {
// return errors.New("alias cannot exceed 50 characters")
// }
// for _, ch := range req.Alias {
// if !(ch >= 'a' && ch <= 'z') &&
// !(ch >= 'A' && ch <= 'Z') &&
// !(ch >= '0' && ch <= '9') &&
// ch != '-' && ch != '_' {
// return errors.New("alias can only contain letters, numbers, hyphens and underscores")
// }
// }
// }
// 描述长度限制
if len(req.Description) > 500 {
return status.Error(codes.InvalidArgument, "description cannot exceed 500 characters")
}
return nil
}

View File

@ -0,0 +1,30 @@
package logic
import (
"context"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDeleteCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteCategoryLogic {
return &DeleteCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DeleteCategoryLogic) DeleteCategory(req *types.DeleteCategoryReq) (resp *types.BaseResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,59 @@
package logic
import (
"context"
"time"
"godemo/category/category"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCategoryLogic {
return &GetCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCategoryLogic) GetCategory(req *types.GetCategoryReq) (resp *types.CategoryDetailResp, err error) {
rpcResp, err := l.svcCtx.CategoryRpc.GetCategory(l.ctx, &category.GetCategoryRequest{
Id: req.ID,
})
if err != nil {
return nil, err
}
// 转换主分类的时间
createdAtStr := time.Unix(rpcResp.Category.CreatedAt, 0).Format(time.RFC3339)
updatedAtStr := ""
if rpcResp.Category.UpdatedAt != 0 {
updatedAtStr = time.Unix(rpcResp.Category.UpdatedAt, 0).Format(time.RFC3339)
}
resp = &types.CategoryDetailResp{
CategoryDetail: types.CategoryDetail{
BaseCategory: types.BaseCategory{
ID: rpcResp.Category.Id,
Name: rpcResp.Category.Name,
Alias: rpcResp.Category.Alias,
Description: rpcResp.Category.Description,
ParentID: rpcResp.Category.ParentId,
SystemID: rpcResp.Category.SystemId,
CreatedAt: createdAtStr,
UpdatedAt: updatedAtStr,
},
Parent: nil,
},
}
return
}

View File

@ -0,0 +1,30 @@
package logic
import (
"context"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCategoryTreeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCategoryTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCategoryTreeLogic {
return &GetCategoryTreeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetCategoryTreeLogic) GetCategoryTree(req *types.GetCategoryTreeReq) (resp *types.CategoryTreeResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,97 @@
package logic
import (
"context"
"strings"
"time"
"godemo/category/category"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type GetSystemCategoriesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetSystemCategoriesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSystemCategoriesLogic {
return &GetSystemCategoriesLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetSystemCategoriesLogic) GetSystemCategories(req *types.GetSystemCategoriesReq) (resp *types.ListCategoriesResp, err error) {
// 1. 验证系统ID
if strings.TrimSpace(req.SystemID) == "" {
return nil, status.Error(codes.InvalidArgument, "system_id is required")
}
// 2. 准备 RPC 请求
rpcReq := &category.GetSystemCategoriesRequest{
SystemId: req.SystemID,
IncludeDescendants: req.IncludeDescendants,
}
// 添加父分类ID过滤如果提供
if req.ParentID != "" {
rpcReq.ParentId = req.ParentID
}
// 3. 调用 RPC 服务
rpcResp, rpcErr := l.svcCtx.CategoryRpc.GetSystemCategories(l.ctx, rpcReq)
if rpcErr != nil {
st, ok := status.FromError(rpcErr)
if ok && st.Code() == codes.NotFound {
// 返回空列表而不是错误
return &types.ListCategoriesResp{
Total: 0,
List: []types.BaseCategory{},
}, nil
}
l.Logger.Errorf("RPC error: %v", rpcErr)
return nil, rpcErr
}
// 4. 转换响应数据
categories := make([]types.BaseCategory, 0, len(rpcResp.Categories))
for _, cat := range rpcResp.Categories {
// 转换时间格式
createdAt := ""
if cat.CreatedAt > 0 {
createdAt = time.Unix(cat.CreatedAt, 0).Format(time.RFC3339)
}
updatedAt := ""
if cat.UpdatedAt > 0 {
updatedAt = time.Unix(cat.UpdatedAt, 0).Format(time.RFC3339)
}
categories = append(categories, types.BaseCategory{
ID: cat.Id,
SystemID: cat.SystemId,
Name: cat.Name,
Alias: cat.Alias,
ParentID: cat.ParentId,
Description: cat.Description,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
})
}
// 5. 构建响应
resp = &types.ListCategoriesResp{
Total: rpcResp.Total,
List: categories,
}
l.Logger.Infof("Retrieved %d categories for system %s (parent: %s)", len(categories), req.SystemID, req.ParentID)
return resp, nil
}

View File

@ -0,0 +1,30 @@
package logic
import (
"context"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type ListCategoriesLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewListCategoriesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCategoriesLogic {
return &ListCategoriesLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListCategoriesLogic) ListCategories(req *types.ListCategoriesReq) (resp *types.ListCategoriesResp, err error) {
// todo: add your logic here and delete this line
return
}

View File

@ -0,0 +1,30 @@
package logic
import (
"context"
"godemo/gateway/internal/svc"
"godemo/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateCategoryLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUpdateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCategoryLogic {
return &UpdateCategoryLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UpdateCategoryLogic) UpdateCategory(req *types.UpdateCategoryReq) (resp *types.BaseResp, err error) {
// todo: add your logic here and delete this line
return
}