完成所有接口的实现层
This commit is contained in:
@@ -5,10 +5,12 @@ package course_teacher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_teacher/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_teacher/internal/types"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/util"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -30,7 +32,59 @@ func NewCreateCourseTeacherLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *CreateCourseTeacherLogic) CreateCourseTeacher(req *types.CreateCourseTeacherReq) (resp *types.CreateCourseTeacherResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 校验必填参数
|
||||
if req.CourseId <= 0 {
|
||||
return nil, fmt.Errorf("参数错误:课程ID必须为正整数")
|
||||
}
|
||||
if req.TeacherId <= 0 {
|
||||
return nil, fmt.Errorf("参数错误:教师ID必须为正整数")
|
||||
}
|
||||
if len(req.Name) == 0 || len(req.Name) > 100 {
|
||||
return nil, fmt.Errorf("参数错误:教师姓名不能为空且长度不超过100字符")
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 校验可选参数
|
||||
if len(req.Title) > 200 {
|
||||
return nil, fmt.Errorf("参数错误:教师头衔长度不能超过200字符")
|
||||
}
|
||||
if len(req.Avatar) > 512 {
|
||||
return nil, fmt.Errorf("参数错误:头像URL长度不能超过512字符")
|
||||
}
|
||||
|
||||
// 3. 处理可选参数默认值(Sort默认0)
|
||||
sort := int64(0)
|
||||
if req.Sort >= 0 {
|
||||
sort = int64(req.Sort) // 适配int→int64(Model层字段类型)
|
||||
}
|
||||
|
||||
// 4. 转换请求参数为Model层结构体
|
||||
courseTeacher := &model.CourseTeacher{
|
||||
CourseId: int64(req.CourseId), // 课程ID:int→int64
|
||||
TeacherId: int64(req.TeacherId), // 教师ID:int→int64
|
||||
Name: req.Name,
|
||||
Title: req.Title,
|
||||
Avatar: req.Avatar,
|
||||
Intro: util.StringToNullString(req.Intro),
|
||||
Sort: sort, // 排序:int→int64
|
||||
}
|
||||
|
||||
// 5. 调用Model层执行插入
|
||||
result, err := l.model.Insert(l.ctx, courseTeacher)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建课程-教师关联失败:%w", err)
|
||||
}
|
||||
|
||||
// 6. 获取新增关联记录的ID
|
||||
newId, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取新增关联ID失败:%w", err)
|
||||
}
|
||||
|
||||
// 7. 构造响应
|
||||
resp = &types.CreateCourseTeacherResp{
|
||||
Id: int(newId), // int64→int(适配响应结构体)
|
||||
Message: "课程-教师关联创建成功",
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package course_teacher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_teacher/internal/model"
|
||||
@@ -30,7 +31,18 @@ func NewDeleteCourseTeacherLogic(ctx context.Context, cfg *config.Config, model
|
||||
}
|
||||
|
||||
func (l *DeleteCourseTeacherLogic) DeleteCourseTeacher(req *types.DeleteCourseTeacherReq) (resp *types.DeleteCourseTeacherResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 将请求中的int类型ID转换为数据层所需的int64类型
|
||||
teacherID := int64(req.Id)
|
||||
|
||||
return
|
||||
// 2. 调用数据层的删除方法执行删除操作
|
||||
err = l.model.Delete(l.ctx, teacherID)
|
||||
if err != nil {
|
||||
// 若删除过程中出现数据库错误(如连接异常等),返回错误信息
|
||||
return nil, fmt.Errorf("删除教学团队关联失败: %v", err)
|
||||
}
|
||||
|
||||
// 3. 删除成功,返回操作结果
|
||||
return &types.DeleteCourseTeacherResp{
|
||||
Message: fmt.Sprintf("教学团队关联(ID: %d)已成功删除", req.Id),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,9 @@ package course_teacher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_teacher/internal/model"
|
||||
@@ -30,7 +33,38 @@ func NewGetCourseTeacherLogic(ctx context.Context, cfg *config.Config, model mod
|
||||
}
|
||||
|
||||
func (l *GetCourseTeacherLogic) GetCourseTeacher(req *types.GetCourseTeacherReq) (resp *types.GetCourseTeacherResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 转换请求ID类型(int -> int64,适配数据层方法)
|
||||
teacherID := int64(req.Id)
|
||||
|
||||
return
|
||||
// 2. 调用数据层查询单个关联信息
|
||||
courseTeacher, err := l.model.FindOne(l.ctx, teacherID)
|
||||
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. 转换数据层模型到响应结构体(处理类型转换)
|
||||
resp = &types.GetCourseTeacherResp{
|
||||
Id: int(courseTeacher.Id), // int64 -> int
|
||||
CourseId: int(courseTeacher.CourseId), // int64 -> int
|
||||
TeacherId: int(courseTeacher.TeacherId), // int64 -> int
|
||||
Name: courseTeacher.Name, // string直接赋值
|
||||
Title: courseTeacher.Title, // string直接赋值
|
||||
Avatar: courseTeacher.Avatar, // string直接赋值
|
||||
Intro: NullStringToString(courseTeacher.Intro), // string直接赋值
|
||||
Sort: int(courseTeacher.Sort), // int64 -> int
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func NullStringToString(ns sql.NullString) string {
|
||||
if ns.Valid {
|
||||
return ns.String
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package course_teacher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_teacher/internal/model"
|
||||
@@ -30,7 +32,73 @@ func NewListCourseTeacherLogic(ctx context.Context, cfg *config.Config, model mo
|
||||
}
|
||||
|
||||
func (l *ListCourseTeacherLogic) ListCourseTeacher(req *types.ListCourseTeacherReq) (resp *types.ListCourseTeacherResp, 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. 转换筛选参数为int64(适配Model层字段类型)
|
||||
courseId := int64(req.CourseId)
|
||||
teacherId := int64(req.TeacherId)
|
||||
|
||||
// 3. 计算分页参数(offset = (page-1)*pageSize)
|
||||
offset := (req.Page - 1) * req.PageSize
|
||||
|
||||
// 4. 调用Model层查询总条数(按筛选条件)
|
||||
total, err := l.model.CountByConditions(l.ctx, courseId, teacherId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询教师总条数失败:%w", err)
|
||||
}
|
||||
|
||||
// 5. 调用Model层查询当前页数据(按筛选条件+分页+排序)
|
||||
teachers, err := l.model.FindByConditionsWithPage(
|
||||
l.ctx,
|
||||
courseId, // 课程ID筛选(0表示不筛选)
|
||||
teacherId, // 教师ID筛选(0表示不筛选)
|
||||
req.PageSize, // 每页条数
|
||||
offset, // 偏移量
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询教师列表失败:%w", err)
|
||||
}
|
||||
|
||||
// 6. 转换Model数据为响应列表(处理sql.NullString和int64→int)
|
||||
var list []types.GetCourseTeacherResp
|
||||
for _, t := range teachers {
|
||||
list = append(list, types.GetCourseTeacherResp{
|
||||
Id: int(t.Id), // int64→int
|
||||
CourseId: int(t.CourseId), // int64→int
|
||||
TeacherId: int(t.TeacherId), // int64→int
|
||||
Name: t.Name,
|
||||
Title: t.Title, // sql.NullString→string
|
||||
Avatar: t.Avatar, // sql.NullString→string
|
||||
Intro: NullStringToString(t.Intro), // sql.NullString→string
|
||||
Sort: int(t.Sort), // int64→int
|
||||
})
|
||||
}
|
||||
|
||||
// 7. 构造响应
|
||||
resp = &types.ListCourseTeacherResp{
|
||||
Total: int(total), // 总条数(int64→int)
|
||||
List: list,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func StringToNullString(s string) sql.NullString {
|
||||
if s != "" {
|
||||
return sql.NullString{
|
||||
String: s,
|
||||
Valid: true,
|
||||
}
|
||||
}
|
||||
return sql.NullString{
|
||||
Valid: false, // 空字符串时标记为无效,数据库存 NULL
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ type (
|
||||
FindOne(ctx context.Context, id int64) (*CourseTeacher, error)
|
||||
Update(ctx context.Context, data *CourseTeacher) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
CountByConditions(ctx context.Context, courseId, teacherId int64) (int64, error)
|
||||
FindByConditionsWithPage(
|
||||
ctx context.Context,
|
||||
courseId, teacherId int64,
|
||||
pageSize, offset int,
|
||||
) ([]*CourseTeacher, error)
|
||||
}
|
||||
|
||||
defaultCourseTeacherModel struct {
|
||||
@@ -86,6 +92,74 @@ func (m *defaultCourseTeacherModel) Update(ctx context.Context, data *CourseTeac
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCourseTeacherModel) CountByConditions(ctx context.Context, courseId, teacherId int64) (int64, error) {
|
||||
var count int64
|
||||
baseQuery := fmt.Sprintf("select count(*) from %s", m.table)
|
||||
|
||||
// 构造筛选条件(支持单条件/多条件/无筛选)
|
||||
var whereClauses []string
|
||||
var args []interface{}
|
||||
|
||||
if courseId > 0 {
|
||||
whereClauses = append(whereClauses, "`course_id` = ?")
|
||||
args = append(args, courseId)
|
||||
}
|
||||
if teacherId > 0 {
|
||||
whereClauses = append(whereClauses, "`teacher_id` = ?")
|
||||
args = append(args, teacherId)
|
||||
}
|
||||
|
||||
// 拼接完整查询语句
|
||||
if len(whereClauses) > 0 {
|
||||
baseQuery += " where " + strings.Join(whereClauses, " and ")
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
err := m.conn.QueryRowCtx(ctx, &count, baseQuery, args...)
|
||||
return count, err
|
||||
}
|
||||
|
||||
// FindByConditionsWithPage 实现按条件分页查询
|
||||
func (m *defaultCourseTeacherModel) FindByConditionsWithPage(
|
||||
ctx context.Context,
|
||||
courseId, teacherId int64,
|
||||
pageSize, offset int,
|
||||
) ([]*CourseTeacher, error) {
|
||||
var teachers []*CourseTeacher
|
||||
baseQuery := fmt.Sprintf("select %s from %s", courseTeacherRows, m.table)
|
||||
|
||||
// 构造筛选条件(同CountByConditions)
|
||||
var whereClauses []string
|
||||
var args []interface{}
|
||||
|
||||
if courseId > 0 {
|
||||
whereClauses = append(whereClauses, "`course_id` = ?")
|
||||
args = append(args, courseId)
|
||||
}
|
||||
if teacherId > 0 {
|
||||
whereClauses = append(whereClauses, "`teacher_id` = ?")
|
||||
args = append(args, teacherId)
|
||||
}
|
||||
|
||||
// 拼接分页和排序(按sort升序,确保展示顺序一致)
|
||||
pageQuery := " order by `sort` asc limit ?, ?"
|
||||
args = append(args, offset, pageSize) // 补充分页参数
|
||||
|
||||
// 拼接完整查询语句
|
||||
if len(whereClauses) > 0 {
|
||||
baseQuery += " where " + strings.Join(whereClauses, " and ") + pageQuery
|
||||
} else {
|
||||
baseQuery += pageQuery
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
err := m.conn.QueryRowsCtx(ctx, &teachers, baseQuery, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return teachers, nil
|
||||
}
|
||||
|
||||
func (m *defaultCourseTeacherModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user