Files
hldrCenter/server/internal/course_file/internal/logic/course_file/createcoursefilelogic.go
2025-11-02 09:37:52 +08:00

85 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1
package course_file
import (
"context"
"fmt"
"github.com/JACKYMYPERSON/hldrCenter/config"
"github.com/JACKYMYPERSON/hldrCenter/internal/course_file/internal/model"
"github.com/JACKYMYPERSON/hldrCenter/internal/course_file/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateCourseFileLogic struct {
logx.Logger
ctx context.Context
cfg *config.Config
model model.CourseFileModel
}
func NewCreateCourseFileLogic(ctx context.Context, cfg *config.Config, model model.CourseFileModel) *CreateCourseFileLogic {
return &CreateCourseFileLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
cfg: cfg,
model: model,
}
}
func (l *CreateCourseFileLogic) CreateCourseFile(req *types.CreateCourseFileReq) (resp *types.CreateCourseFileResp, err error) {
// 1. 参数校验补充validate标签之外的业务校验
if req.ContentId <= 0 {
return nil, fmt.Errorf("参数错误关联的内容ID必须为正整数")
}
if len(req.Title) == 0 {
return nil, fmt.Errorf("参数错误:文件标题不能为空")
}
if len(req.Title) > 255 {
return nil, fmt.Errorf("参数错误文件标题长度不能超过255字符")
}
if len(req.FileType) == 0 {
return nil, fmt.Errorf("参数错误:文件类型不能为空")
}
if len(req.FileType) > 30 {
return nil, fmt.Errorf("参数错误文件类型长度不能超过30字符")
}
if len(req.FileUrl) == 0 {
return nil, fmt.Errorf("参数错误文件URL不能为空")
}
if len(req.FileUrl) > 255 {
return nil, fmt.Errorf("参数错误文件URL长度不能超过255字符")
}
// 2. 转换请求参数为Model层结构体适配数据库字段类型
courseFile := &model.CourseFile{
ContentId: int64(req.ContentId), // 假设Model层ContentId为int64类型
Title: req.Title,
FileType: req.FileType,
FileUrl: req.FileUrl,
}
// 3. 调用Model层插入数据
result, err := l.model.Insert(l.ctx, courseFile)
if err != nil {
return nil, fmt.Errorf("创建课程文件失败:%w", err)
}
// 4. 获取新增记录的ID从插入结果中提取
newId, err := result.LastInsertId()
if err != nil {
return nil, fmt.Errorf("获取新增文件ID失败%w", err)
}
// 5. 构造响应
resp = &types.CreateCourseFileResp{
Id: int(newId), // 转换int64为int适配响应结构体
Message: "课程文件创建成功",
}
return resp, nil
}