完成教学案例的后端
This commit is contained in:
@@ -5,6 +5,7 @@ package teaching_case
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/teaching_case/internal/model"
|
||||
@@ -30,7 +31,36 @@ func NewCreateTeachingCaseLogic(ctx context.Context, cfg *config.Config, model m
|
||||
}
|
||||
|
||||
func (l *CreateTeachingCaseLogic) CreateTeachingCase(req *types.CreateTeachingCaseReq) (resp *types.BaseResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 二次参数校验(确保必填字段非空,补充validate标签之外的逻辑)
|
||||
if req.Title == "" || req.TutorName == "" || req.StudentNames == "" || req.Content == "" {
|
||||
return &types.BaseResp{Code: 400, Msg: "标题、导师姓名、学生姓名、案例内容为必填项"}, nil
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 映射请求参数到model结构体
|
||||
data := &model.TeachingCase{
|
||||
Title: req.Title,
|
||||
TutorName: req.TutorName,
|
||||
TutorTitle: req.TutorTitle, // 允许为空,使用默认值""
|
||||
StudentNames: req.StudentNames,
|
||||
Content: req.Content,
|
||||
CoverUrl: req.CoverUrl, // 允许为空,使用默认值""
|
||||
Sort: int64(req.Sort), // 默认为0,无需额外处理
|
||||
// create_time和update_time由数据库自动维护,无需手动赋值
|
||||
}
|
||||
|
||||
// 3. 调用model插入数据
|
||||
_, err = l.model.Insert(l.ctx, data)
|
||||
if err != nil {
|
||||
// 4. 错误处理:返回具体错误信息,便于排查
|
||||
return &types.BaseResp{
|
||||
Code: 500,
|
||||
Msg: fmt.Sprintf("创建教学案例失败:%s", err.Error()),
|
||||
}, err
|
||||
}
|
||||
|
||||
// 5. 成功响应
|
||||
return &types.BaseResp{
|
||||
Code: 0,
|
||||
Msg: "教学案例创建成功",
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package teaching_case
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/teaching_case/internal/model"
|
||||
@@ -30,7 +31,28 @@ func NewDeleteTeachingCaseLogic(ctx context.Context, cfg *config.Config, model m
|
||||
}
|
||||
|
||||
func (l *DeleteTeachingCaseLogic) DeleteTeachingCase(req *types.DeleteTeachingCaseReq) (resp *types.BaseResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验:确保 ID 合法
|
||||
if req.Id <= 0 {
|
||||
return &types.BaseResp{Code: 400, Msg: "案例ID不合法"}, nil
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 先查询案例是否存在且未被删除(避免重复删除或删除不存在的数据)
|
||||
_, err = l.model.FindOne(ctx, int64(req.Id))
|
||||
if err != nil {
|
||||
// 若查询不到(已删除或不存在),直接返回成功提示(避免前端困惑)
|
||||
if err == model.ErrNotFound {
|
||||
return &types.BaseResp{Code: 0, Msg: "教学案例已删除或不存在"}, nil
|
||||
}
|
||||
// 其他查询错误返回500
|
||||
return &types.BaseResp{Code: 500, Msg: fmt.Sprintf("查询案例失败:%s", err.Error())}, err
|
||||
}
|
||||
|
||||
// 3. 执行软删除:更新 is_delete 为 1
|
||||
err = l.model.Delete(l.ctx, int64(req.Id))
|
||||
if err != nil {
|
||||
return &types.BaseResp{Code: 500, Msg: fmt.Sprintf("删除教学案例失败:%s", err.Error())}, err
|
||||
}
|
||||
|
||||
// 4. 成功响应
|
||||
return &types.BaseResp{Code: 0, Msg: "教学案例删除成功"}, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ package teaching_case
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/teaching_case/internal/model"
|
||||
@@ -30,7 +32,34 @@ func NewGetTeachingCaseLogic(ctx context.Context, cfg *config.Config, model mode
|
||||
}
|
||||
|
||||
func (l *GetTeachingCaseLogic) GetTeachingCase(req *types.GetTeachingCaseReq) (resp *types.TeachingCaseResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验:确保案例ID合法
|
||||
if req.Id <= 0 {
|
||||
return nil, errors.New("案例ID不合法")
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 调用Model查询(已过滤已删除数据,需先同步修改Model的FindOne方法)
|
||||
modelCase, err := l.model.FindOne(l.ctx, int64(req.Id))
|
||||
if err != nil {
|
||||
// 区分"未找到"和"查询异常"
|
||||
if err == model.ErrNotFound {
|
||||
return nil, errors.New("教学案例不存在或已删除")
|
||||
}
|
||||
return nil, fmt.Errorf("查询教学案例失败:%w", err)
|
||||
}
|
||||
|
||||
// 3. 映射Model数据到响应结构体
|
||||
resp = &types.TeachingCaseResp{
|
||||
Id: int(modelCase.Id),
|
||||
Title: modelCase.Title,
|
||||
TutorName: modelCase.TutorName,
|
||||
TutorTitle: modelCase.TutorTitle,
|
||||
StudentNames: modelCase.StudentNames,
|
||||
Content: modelCase.Content,
|
||||
CoverUrl: modelCase.CoverUrl,
|
||||
Sort: int(modelCase.Sort),
|
||||
CreateTime: modelCase.CreateTime.Format("2006-01-02 15:04:05"), // 数据库自动生成,直接返回
|
||||
UpdateTime: modelCase.UpdateTime.Format("2006-01-02 15:04:05"), // 数据库自动更新,直接返回
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package teaching_case
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/teaching_case/internal/model"
|
||||
@@ -30,7 +31,60 @@ func NewListTeachingCaseLogic(ctx context.Context, cfg *config.Config, model mod
|
||||
}
|
||||
|
||||
func (l *ListTeachingCaseLogic) ListTeachingCase(req *types.ListTeachingCaseReq) (resp *types.ListTeachingCaseResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验与默认值处理
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1 // 默认第一页
|
||||
}
|
||||
if req.Size <= 0 || req.Size > 100 {
|
||||
req.Size = 10 // 默认10条/页,最大限制100条
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 分页计算:offset = (页码-1) * 每页条数
|
||||
offset := (req.Page - 1) * req.Size
|
||||
|
||||
// 3. 调用Model层查询:先查总条数,再查分页列表
|
||||
total, err := l.model.Count(l.ctx, req.Keyword)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询教学案例总条数失败:%w", err)
|
||||
}
|
||||
|
||||
list, err := l.model.ListByPage(l.ctx, req.Keyword, req.Sort, offset, req.Size)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询教学案例列表失败:%w", err)
|
||||
}
|
||||
|
||||
// 4. 数据映射:Model → 响应结构体(含时间类型转换)
|
||||
respList := make([]types.TeachingCaseResp, 0, len(list))
|
||||
for _, item := range list {
|
||||
// 时间格式转换:time.Time → string
|
||||
createTime := item.CreateTime.Format("2006-01-02 15:04:05")
|
||||
updateTime := item.UpdateTime.Format("2006-01-02 15:04:05")
|
||||
if item.CreateTime.IsZero() {
|
||||
createTime = ""
|
||||
}
|
||||
if item.UpdateTime.IsZero() {
|
||||
updateTime = ""
|
||||
}
|
||||
|
||||
respList = append(respList, types.TeachingCaseResp{
|
||||
Id: int(item.Id),
|
||||
Title: item.Title,
|
||||
TutorName: item.TutorName,
|
||||
TutorTitle: item.TutorTitle,
|
||||
StudentNames: item.StudentNames,
|
||||
Content: item.Content,
|
||||
CoverUrl: item.CoverUrl,
|
||||
Sort: int(item.Sort),
|
||||
CreateTime: createTime,
|
||||
UpdateTime: updateTime,
|
||||
})
|
||||
}
|
||||
|
||||
// 5. 组装响应结果
|
||||
resp = &types.ListTeachingCaseResp{
|
||||
Total: total,
|
||||
List: respList,
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ package teaching_case
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/teaching_case/internal/model"
|
||||
@@ -29,7 +30,62 @@ func NewUpdateTeachingCaseLogic(ctx context.Context, cfg *config.Config, model m
|
||||
}
|
||||
|
||||
func (l *UpdateTeachingCaseLogic) UpdateTeachingCase(req *types.UpdateTeachingCaseReq) (resp *types.BaseResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
// 1. 参数校验:ID为必填且合法
|
||||
if req.Id <= 0 {
|
||||
return &types.BaseResp{Code: 400, Msg: "案例ID不合法"}, nil
|
||||
}
|
||||
|
||||
return
|
||||
// 2. 校验案例是否存在且未被删除
|
||||
existCase, err := l.model.FindOne(l.ctx, int64(req.Id))
|
||||
if err != nil {
|
||||
if err == model.ErrNotFound {
|
||||
return &types.BaseResp{Code: 404, Msg: "教学案例不存在或已删除"}, nil
|
||||
}
|
||||
return &types.BaseResp{Code: 500, Msg: fmt.Sprintf("查询案例失败:%s", err.Error())}, err
|
||||
}
|
||||
|
||||
// 3. 部分字段更新:只更新请求中不为空/有效的值(保留原有数据)
|
||||
updateData := &model.TeachingCase{
|
||||
Id: existCase.Id, // 必须保留ID(更新条件)
|
||||
IsDelete: existCase.IsDelete, // 保留软删除状态(不允许通过更新修改)
|
||||
Title: existCase.Title,
|
||||
TutorName: existCase.TutorName,
|
||||
TutorTitle: existCase.TutorTitle,
|
||||
StudentNames: existCase.StudentNames,
|
||||
Content: existCase.Content,
|
||||
CoverUrl: existCase.CoverUrl,
|
||||
Sort: existCase.Sort,
|
||||
}
|
||||
|
||||
// 覆盖请求中传递的非空/有效字段
|
||||
if req.Title != "" {
|
||||
updateData.Title = req.Title
|
||||
}
|
||||
if req.TutorName != "" {
|
||||
updateData.TutorName = req.TutorName
|
||||
}
|
||||
if req.TutorTitle != "" {
|
||||
updateData.TutorTitle = req.TutorTitle
|
||||
}
|
||||
if req.StudentNames != "" {
|
||||
updateData.StudentNames = req.StudentNames
|
||||
}
|
||||
if req.Content != "" {
|
||||
updateData.Content = req.Content
|
||||
}
|
||||
if req.CoverUrl != "" {
|
||||
updateData.CoverUrl = req.CoverUrl
|
||||
}
|
||||
if req.Sort != 0 { // 允许排序为0(0是合法值),仅当传递非默认值时更新
|
||||
updateData.Sort = int64(req.Sort)
|
||||
}
|
||||
|
||||
// 4. 调用Model更新数据
|
||||
err = l.model.Update(l.ctx, updateData)
|
||||
if err != nil {
|
||||
return &types.BaseResp{Code: 500, Msg: fmt.Sprintf("更新教学案例失败:%s", err.Error())}, err
|
||||
}
|
||||
|
||||
// 5. 成功响应
|
||||
return &types.BaseResp{Code: 0, Msg: "教学案例更新成功"}, nil
|
||||
}
|
||||
|
||||
@@ -29,6 +29,8 @@ type (
|
||||
FindOne(ctx context.Context, id int64) (*TeachingCase, error)
|
||||
Update(ctx context.Context, data *TeachingCase) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
Count(ctx context.Context, keyword string) (int64, error)
|
||||
ListByPage(ctx context.Context, keyword string, sort int, offset, size int) ([]*TeachingCase, error)
|
||||
}
|
||||
|
||||
defaultTeachingCaseModel struct {
|
||||
@@ -47,6 +49,7 @@ type (
|
||||
Sort int64 `db:"sort"` // 排序(数字越小越靠前)
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
IsDelete int64 `db:"is_delete"` // 软删除标识(0-未删除,1-已删除)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -58,13 +61,14 @@ func newTeachingCaseModel(conn sqlx.SqlConn) *defaultTeachingCaseModel {
|
||||
}
|
||||
|
||||
func (m *defaultTeachingCaseModel) Delete(ctx context.Context, id int64) error {
|
||||
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
|
||||
// 软删除:更新 is_delete 为 1,保留数据记录
|
||||
query := fmt.Sprintf("update %s set `is_delete` = 1, `update_time` = CURRENT_TIMESTAMP where `id` = ? and `is_delete` = 0", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultTeachingCaseModel) FindOne(ctx context.Context, id int64) (*TeachingCase, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", teachingCaseRows, m.table)
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? and `is_delete` = 0 limit 1", teachingCaseRows, m.table)
|
||||
var resp TeachingCase
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
@@ -78,17 +82,69 @@ func (m *defaultTeachingCaseModel) FindOne(ctx context.Context, id int64) (*Teac
|
||||
}
|
||||
|
||||
func (m *defaultTeachingCaseModel) Insert(ctx context.Context, data *TeachingCase) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, teachingCaseRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.TutorName, data.TutorTitle, data.StudentNames, data.Content, data.CoverUrl, data.Sort)
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, teachingCaseRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.TutorName, data.TutorTitle, data.StudentNames, data.Content, data.CoverUrl, data.Sort, data.IsDelete)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultTeachingCaseModel) Update(ctx context.Context, data *TeachingCase) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, teachingCaseRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.TutorName, data.TutorTitle, data.StudentNames, data.Content, data.CoverUrl, data.Sort, data.Id)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.TutorName, data.TutorTitle, data.StudentNames, data.Content, data.CoverUrl, data.Sort, data.IsDelete, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultTeachingCaseModel) Count(ctx context.Context, keyword string) (int64, error) {
|
||||
query := fmt.Sprintf("select count(id) from %s where is_delete = 0", m.table)
|
||||
args := make([]interface{}, 0)
|
||||
|
||||
// 关键词搜索:模糊匹配标题、导师姓名
|
||||
if keyword != "" {
|
||||
query += " and (title like ? or tutor_name like ?)"
|
||||
likeVal := "%" + strings.TrimSpace(keyword) + "%"
|
||||
args = append(args, likeVal, likeVal)
|
||||
}
|
||||
|
||||
var total int64
|
||||
// 修正点:sqlx.QueryRowCtx 已自动扫描结果到 total,无需额外调用 Scan
|
||||
err := m.conn.QueryRowCtx(ctx, &total, query, args...)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// ListByPage:分页查询未删除的案例列表(支持关键词、排序)
|
||||
func (m *defaultTeachingCaseModel) ListByPage(ctx context.Context, keyword string, sort int, offset, size int) ([]*TeachingCase, error) {
|
||||
query := fmt.Sprintf("select %s from %s where is_delete = 0", teachingCaseRows, m.table)
|
||||
args := make([]interface{}, 0)
|
||||
|
||||
// 关键词搜索条件
|
||||
if keyword != "" {
|
||||
query += " and (title like ? or tutor_name like ?)"
|
||||
likeVal := "%" + strings.TrimSpace(keyword) + "%"
|
||||
args = append(args, likeVal, likeVal)
|
||||
}
|
||||
|
||||
// 排序逻辑:优先按sort升序(数字越小越靠前),无sort则按创建时间降序
|
||||
query += " order by "
|
||||
if sort != 0 {
|
||||
query += "sort asc, create_time desc"
|
||||
} else {
|
||||
query += "create_time desc"
|
||||
}
|
||||
|
||||
// 分页条件
|
||||
query += " limit ?, ?"
|
||||
args = append(args, offset, size)
|
||||
|
||||
var list []*TeachingCase
|
||||
err := m.conn.QueryRowsCtx(ctx, &list, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (m *defaultTeachingCaseModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user