完成所有接口的实现层
This commit is contained in:
@@ -5,6 +5,7 @@ package course_resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
@@ -30,7 +31,54 @@ func NewCreateCourseResourceLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *CreateCourseResourceLogic) CreateCourseResource(req *types.CreateCourseResourceReq) (resp *types.CreateCourseResourceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 校验必填参数
|
||||
if req.CourseId <= 0 {
|
||||
return nil, fmt.Errorf("参数错误:课程ID必须为正整数")
|
||||
}
|
||||
if len(req.Title) == 0 || len(req.Title) > 255 {
|
||||
return nil, fmt.Errorf("参数错误:资源标题不能为空且长度不超过255字符")
|
||||
}
|
||||
if len(req.ResourceUrl) == 0 || len(req.ResourceUrl) > 512 {
|
||||
return nil, fmt.Errorf("参数错误:资源URL不能为空且长度不超过512字符")
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 处理可选参数默认值
|
||||
size := int64(0)
|
||||
if req.Size >= 0 {
|
||||
size = int64(req.Size) // 适配int→int64(数据库字段常规类型)
|
||||
}
|
||||
|
||||
sort := int64(0)
|
||||
if req.Sort >= 0 {
|
||||
sort = int64(req.Sort) // 适配int→int64
|
||||
}
|
||||
|
||||
// 3. 转换请求参数为Model层结构体
|
||||
resourceModel := &model.CourseResource{
|
||||
CourseId: int64(req.CourseId), // 课程ID:int→int64
|
||||
Title: req.Title,
|
||||
ResourceUrl: req.ResourceUrl,
|
||||
Size: size, // 资源大小(默认0)
|
||||
Sort: sort, // 排序(默认0)
|
||||
}
|
||||
|
||||
// 4. 调用Model层执行插入
|
||||
result, err := l.model.Insert(l.ctx, resourceModel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建课程资源失败:%w", err)
|
||||
}
|
||||
|
||||
// 5. 获取新增资源的主键ID
|
||||
newId, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取新增资源ID失败:%w", err)
|
||||
}
|
||||
|
||||
// 6. 构造响应结果
|
||||
resp = &types.CreateCourseResourceResp{
|
||||
Id: int(newId), // int64→int(适配响应结构体)
|
||||
Message: "课程资源创建成功",
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ package course_resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -30,7 +32,32 @@ func NewDeleteCourseResourceLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *DeleteCourseResourceLogic) DeleteCourseResource(req *types.DeleteCourseResourceReq) (resp *types.DeleteCourseResourceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验:确保资源ID为正整数
|
||||
if req.Id <= 0 {
|
||||
return nil, fmt.Errorf("参数错误:资源ID必须为正整数")
|
||||
}
|
||||
resourceId := int64(req.Id) // 转换为int64(适配Model层字段类型)
|
||||
|
||||
return
|
||||
// 2. 检查资源是否存在(避免删除不存在的记录)
|
||||
_, err = l.model.FindOne(l.ctx, resourceId)
|
||||
if err != nil {
|
||||
if err == model.ErrNotFound || err == sqlx.ErrNotFound {
|
||||
return nil, fmt.Errorf("删除失败:该课程资源不存在或已被删除")
|
||||
}
|
||||
// 其他查询错误(如数据库连接异常)
|
||||
return nil, fmt.Errorf("查询资源信息失败:%w", err)
|
||||
}
|
||||
|
||||
// 3. 调用Model层执行删除操作
|
||||
err = l.model.Delete(l.ctx, resourceId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("删除课程资源失败:%w", err)
|
||||
}
|
||||
|
||||
// 4. 构造成功响应
|
||||
resp = &types.DeleteCourseResourceResp{
|
||||
Message: "课程资源删除成功",
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,10 +5,12 @@ package course_resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -30,7 +32,31 @@ func NewGetCourseResourceLogic(ctx context.Context, cfg *config.Config, model mo
|
||||
}
|
||||
|
||||
func (l *GetCourseResourceLogic) GetCourseResource(req *types.GetCourseResourceReq) (resp *types.GetCourseResourceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验:确保资源ID为正整数
|
||||
if req.Id <= 0 {
|
||||
return nil, fmt.Errorf("参数错误:资源ID必须为正整数")
|
||||
}
|
||||
resourceId := int64(req.Id) // 转换为int64(适配Model层字段类型)
|
||||
|
||||
return
|
||||
// 2. 调用Model层查询资源详情
|
||||
resourceModel, err := l.model.FindOne(l.ctx, resourceId)
|
||||
if err != nil {
|
||||
if err == model.ErrNotFound || err == sqlx.ErrNotFound {
|
||||
return nil, fmt.Errorf("查询失败:该课程资源不存在或已被删除")
|
||||
}
|
||||
// 其他查询错误(如数据库连接异常)
|
||||
return nil, fmt.Errorf("查询课程资源失败:%w", err)
|
||||
}
|
||||
|
||||
// 3. 转换Model数据为响应结构体(处理类型适配)
|
||||
resp = &types.GetCourseResourceResp{
|
||||
Id: int(resourceModel.Id), // Model层Id为int64,转换为int
|
||||
CourseId: int(resourceModel.CourseId), // 关联课程ID:int64→int
|
||||
Title: resourceModel.Title, // 标题直接复用
|
||||
ResourceUrl: resourceModel.ResourceUrl, // 资源URL直接复用
|
||||
Size: int(resourceModel.Size), // 资源大小:int64→int
|
||||
Sort: int(resourceModel.Sort), // 排序:int64→int
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package course_resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
@@ -30,7 +31,54 @@ func NewListCourseResourceLogic(ctx context.Context, cfg *config.Config, model m
|
||||
}
|
||||
|
||||
func (l *ListCourseResourceLogic) ListCourseResource(req *types.ListCourseResourceReq) (resp *types.ListCourseResourceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验(补充validate标签外的业务校验)
|
||||
if req.Page < 1 {
|
||||
return nil, fmt.Errorf("参数错误:页码必须≥1")
|
||||
}
|
||||
if req.PageSize < 1 || req.PageSize > 100 {
|
||||
return nil, fmt.Errorf("参数错误:每页条数必须在1-100之间")
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 计算分页参数(offset = (page-1)*pageSize)
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
|
||||
// 3. 调用Model层查询总条数(按CourseId筛选,CourseId=0时查全部)
|
||||
total, err := l.model.CountByCourseId(l.ctx, int64(req.CourseId))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询资源总条数失败:%w", err)
|
||||
}
|
||||
|
||||
// 4. 调用Model层查询当前页数据
|
||||
resources, err := l.model.FindByCourseIdWithPage(
|
||||
l.ctx,
|
||||
int64(req.CourseId), // 课程ID(0表示查全部)
|
||||
req.PageSize, // 每页条数
|
||||
offset, // 偏移量
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询资源列表失败:%w", err)
|
||||
}
|
||||
|
||||
// 5. 转换Model数据为响应列表(适配GetCourseResourceResp)
|
||||
var list []types.GetCourseResourceResp
|
||||
for _, res := range resources {
|
||||
list = append(list, types.GetCourseResourceResp{
|
||||
Id: int(res.Id), // int64→int
|
||||
CourseId: int(res.CourseId), // int64→int
|
||||
Title: res.Title,
|
||||
ResourceUrl: res.ResourceUrl,
|
||||
Size: int(res.Size), // int64→int
|
||||
Sort: int(res.Sort), // int64→int
|
||||
})
|
||||
}
|
||||
|
||||
// 6. 构造响应
|
||||
resp = &types.ListCourseResourceResp{
|
||||
Total: int(total), // 总条数(int64→int)
|
||||
List: list, // 资源列表
|
||||
Page: req.Page, // 当前页码
|
||||
PageSize: req.PageSize, // 每页条数
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package course_resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
@@ -30,7 +32,53 @@ func NewUpdateCourseResourceLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *UpdateCourseResourceLogic) UpdateCourseResource(req *types.UpdateCourseResourceReq) (resp *types.UpdateCourseResourceResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 转换资源ID为数据层所需的int64类型
|
||||
resourceID := int64(req.Id)
|
||||
|
||||
return
|
||||
// 2. 查询原始资源信息(确保资源存在,保留未更新字段原始值)
|
||||
original, err := l.model.FindOne(l.ctx, resourceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, errors.New("resource not found")) {
|
||||
return nil, fmt.Errorf("课程资源不存在(ID: %d)", req.Id)
|
||||
}
|
||||
return nil, fmt.Errorf("查询课程资源失败: %v", err)
|
||||
}
|
||||
|
||||
// 3. 构建更新数据(基于原始数据,覆盖请求中有效字段)
|
||||
updateData := &model.CourseResource{
|
||||
Id: resourceID, // 主键ID(WHERE条件,必须保留)
|
||||
CourseId: original.CourseId, // 默认沿用原始课程ID
|
||||
Title: original.Title, // 默认沿用原始标题
|
||||
ResourceUrl: original.ResourceUrl, // 默认沿用原始资源链接
|
||||
Size: original.Size, // 默认沿用原始资源大小
|
||||
Sort: original.Sort, // 默认沿用原始排序
|
||||
}
|
||||
|
||||
// 4. 覆盖需要更新的字段(仅处理请求中提供的有效值)
|
||||
if req.CourseId != 0 {
|
||||
updateData.CourseId = int64(req.CourseId) // int转int64
|
||||
}
|
||||
if req.Title != "" {
|
||||
updateData.Title = req.Title // 字符串直接赋值
|
||||
}
|
||||
if req.ResourceUrl != "" {
|
||||
updateData.ResourceUrl = req.ResourceUrl // 资源链接直接赋值
|
||||
}
|
||||
if req.Size >= 0 {
|
||||
updateData.Size = int64(req.Size) // int转int64(资源大小非负)
|
||||
}
|
||||
if req.Sort >= 0 {
|
||||
updateData.Sort = int64(req.Sort) // int转int64(排序非负)
|
||||
}
|
||||
|
||||
// 5. 调用数据层执行更新
|
||||
err = l.model.Update(l.ctx, updateData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("更新课程资源失败: %v", err)
|
||||
}
|
||||
|
||||
// 6. 返回成功响应
|
||||
return &types.UpdateCourseResourceResp{
|
||||
Message: fmt.Sprintf("课程资源(ID: %d)更新成功", req.Id),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ type (
|
||||
FindOne(ctx context.Context, id int64) (*CourseResource, error)
|
||||
Update(ctx context.Context, data *CourseResource) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
CountByCourseId(ctx context.Context, courseId int64) (int64, error)
|
||||
FindByCourseIdWithPage(
|
||||
ctx context.Context,
|
||||
courseId int64,
|
||||
pageSize, offset int,
|
||||
) ([]*CourseResource, error)
|
||||
}
|
||||
|
||||
defaultCourseResourceModel struct {
|
||||
@@ -84,6 +90,52 @@ func (m *defaultCourseResourceModel) Update(ctx context.Context, data *CourseRes
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCourseResourceModel) CountByCourseId(ctx context.Context, courseId int64) (int64, error) {
|
||||
var count int64
|
||||
// 构造查询条件:courseId=0时查全部,否则按course_id筛选
|
||||
if courseId == 0 {
|
||||
query := fmt.Sprintf("select count(*) from %s", m.table)
|
||||
err := m.conn.QueryRowCtx(ctx, &count, query)
|
||||
return count, err
|
||||
} else {
|
||||
query := fmt.Sprintf("select count(*) from %s where `course_id` = ?", m.table)
|
||||
err := m.conn.QueryRowCtx(ctx, &count, query, courseId)
|
||||
return count, err
|
||||
}
|
||||
}
|
||||
|
||||
// FindByCourseIdWithPage 实现按课程ID分页查询
|
||||
func (m *defaultCourseResourceModel) FindByCourseIdWithPage(
|
||||
ctx context.Context,
|
||||
courseId int64,
|
||||
pageSize, offset int,
|
||||
) ([]*CourseResource, error) {
|
||||
var resources []*CourseResource
|
||||
// 构造查询SQL:支持筛选+排序+分页(按sort升序,确保资源有序)
|
||||
baseQuery := fmt.Sprintf("select %s from %s", courseResourceRows, m.table)
|
||||
sortQuery := " order by `sort` asc limit ?, ?" // 按sort升序,限制条数和偏移量
|
||||
|
||||
var query string
|
||||
var args []interface{}
|
||||
|
||||
if courseId == 0 {
|
||||
// 查全部资源:无where条件
|
||||
query = baseQuery + sortQuery
|
||||
args = []interface{}{offset, pageSize}
|
||||
} else {
|
||||
// 按课程ID筛选
|
||||
query = baseQuery + " where `course_id` = ?" + sortQuery
|
||||
args = []interface{}{courseId, offset, pageSize}
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
err := m.conn.QueryRowsCtx(ctx, &resources, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return resources, nil
|
||||
}
|
||||
|
||||
func (m *defaultCourseResourceModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user