添加course的业务层和实现层
This commit is contained in:
29
server/internal/course/internal/model/coursemodel.go
Normal file
29
server/internal/course/internal/model/coursemodel.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ CourseModel = (*customCourseModel)(nil)
|
||||
|
||||
type (
|
||||
// CourseModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customCourseModel.
|
||||
CourseModel interface {
|
||||
courseModel
|
||||
withSession(session sqlx.Session) CourseModel
|
||||
}
|
||||
|
||||
customCourseModel struct {
|
||||
*defaultCourseModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewCourseModel returns a model for the database table.
|
||||
func NewCourseModel(conn sqlx.SqlConn) CourseModel {
|
||||
return &customCourseModel{
|
||||
defaultCourseModel: newCourseModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customCourseModel) withSession(session sqlx.Session) CourseModel {
|
||||
return NewCourseModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
157
server/internal/course/internal/model/coursemodel_gen.go
Normal file
157
server/internal/course/internal/model/coursemodel_gen.go
Normal file
@@ -0,0 +1,157 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.9.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
courseFieldNames = builder.RawFieldNames(&Course{})
|
||||
courseRows = strings.Join(courseFieldNames, ",")
|
||||
courseRowsExpectAutoSet = strings.Join(stringx.Remove(courseFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
courseRowsWithPlaceHolder = strings.Join(stringx.Remove(courseFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
courseModel interface {
|
||||
Insert(ctx context.Context, data *Course) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*Course, error)
|
||||
Update(ctx context.Context, data *Course) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
List(ctx context.Context, filter CourseFilter) ([]*Course, error)
|
||||
Count(ctx context.Context, filter CourseFilter) (int64, error)
|
||||
}
|
||||
|
||||
defaultCourseModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
Course struct {
|
||||
Id int64 `db:"id"` // 课程ID(主键)
|
||||
Title string `db:"title"` // 课程标题
|
||||
Subtitle string `db:"subtitle"` // 课程副标题
|
||||
CoverUrl string `db:"cover_url"` // 课程封面图URL
|
||||
Intro sql.NullString `db:"intro"` // 课程简介(对应“课程简介”模块)
|
||||
Status int64 `db:"status"` // 课程状态(0删除,1-已发布)
|
||||
CreateTime time.Time `db:"create_time"` // 创建时间
|
||||
UpdateTime time.Time `db:"update_time"` // 更新时间
|
||||
}
|
||||
|
||||
CourseFilter struct {
|
||||
Status int8 // 状态筛选
|
||||
Keyword string // 关键词搜索
|
||||
Offset int // 分页偏移量
|
||||
Limit int // 分页数量
|
||||
}
|
||||
)
|
||||
|
||||
func newCourseModel(conn sqlx.SqlConn) *defaultCourseModel {
|
||||
return &defaultCourseModel{
|
||||
conn: conn,
|
||||
table: "`course`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCourseModel) Delete(ctx context.Context, id int64) error {
|
||||
query := fmt.Sprintf("update %s set `status` = 0, `update_time` = CURRENT_TIMESTAMP where `id` = ?", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCourseModel) FindOne(ctx context.Context, id int64) (*Course, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", courseRows, m.table)
|
||||
var resp Course
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, id)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCourseModel) Insert(ctx context.Context, data *Course) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, courseRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.Subtitle, data.CoverUrl, data.Intro, data.Status)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultCourseModel) Update(ctx context.Context, data *Course) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, courseRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.Subtitle, data.CoverUrl, data.Intro, data.Status, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCourseModel) List(ctx context.Context, filter CourseFilter) ([]*Course, error) {
|
||||
query := fmt.Sprintf("select %s from %s where 1=1", courseRows, m.table)
|
||||
args := []interface{}{}
|
||||
|
||||
// 状态筛选逻辑:
|
||||
// - 如果未指定filter.Status(默认值),默认只查询"已发布"(status=1)
|
||||
// - 如果指定了filter.Status(0或1),则按指定值筛选(支持查询已删除的课程)
|
||||
if filter.Status == -1 {
|
||||
// 默认筛选:只查已发布(status=1)
|
||||
query += " and `status` = 1"
|
||||
} else {
|
||||
// 按指定status筛选(0=删除,1=发布)
|
||||
query += " and `status` = ?"
|
||||
args = append(args, filter.Status)
|
||||
}
|
||||
|
||||
// 关键词搜索(标题和副标题模糊匹配)
|
||||
if filter.Keyword != "" {
|
||||
query += " and (`title` like ? or `subtitle` like ?)"
|
||||
args = append(args, "%"+filter.Keyword+"%", "%"+filter.Keyword+"%")
|
||||
}
|
||||
|
||||
// 分页处理
|
||||
query += " limit ?, ?"
|
||||
args = append(args, filter.Offset, filter.Limit)
|
||||
|
||||
// 执行查询
|
||||
var courses []*Course
|
||||
err := m.conn.QueryRowsCtx(ctx, &courses, query, args...)
|
||||
return courses, err
|
||||
}
|
||||
|
||||
// 配套的Count方法也需要同步修改(确保总数计算正确)
|
||||
func (m *defaultCourseModel) Count(ctx context.Context, filter CourseFilter) (int64, error) {
|
||||
query := fmt.Sprintf("select count(1) from %s where 1=1", m.table)
|
||||
args := []interface{}{}
|
||||
|
||||
// 与List方法保持一致的status筛选逻辑
|
||||
if filter.Status == -1 {
|
||||
query += " and `status` = 1"
|
||||
} else {
|
||||
query += " and `status` = ?"
|
||||
args = append(args, filter.Status)
|
||||
}
|
||||
|
||||
// 关键词搜索条件
|
||||
if filter.Keyword != "" {
|
||||
query += " and (`title` like ? or `subtitle` like ?)"
|
||||
args = append(args, "%"+filter.Keyword+"%", "%"+filter.Keyword+"%")
|
||||
}
|
||||
|
||||
var total int64
|
||||
err := m.conn.QueryRowCtx(ctx, &total, query, args...)
|
||||
return total, err
|
||||
}
|
||||
|
||||
func (m *defaultCourseModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
server/internal/course/internal/model/vars.go
Normal file
5
server/internal/course/internal/model/vars.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
Reference in New Issue
Block a user