添加课程文件Model

This commit is contained in:
2025-11-01 23:06:54 +08:00
parent b4968af226
commit 7b93dd366d
3 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
var _ CourseFileModel = (*customCourseFileModel)(nil)
type (
// CourseFileModel is an interface to be customized, add more methods here,
// and implement the added methods in customCourseFileModel.
CourseFileModel interface {
courseFileModel
withSession(session sqlx.Session) CourseFileModel
}
customCourseFileModel struct {
*defaultCourseFileModel
}
)
// NewCourseFileModel returns a model for the database table.
func NewCourseFileModel(conn sqlx.SqlConn) CourseFileModel {
return &customCourseFileModel{
defaultCourseFileModel: newCourseFileModel(conn),
}
}
func (m *customCourseFileModel) withSession(session sqlx.Session) CourseFileModel {
return NewCourseFileModel(sqlx.NewSqlConnFromSession(session))
}

View File

@@ -0,0 +1,91 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: 1.9.1
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 (
courseFileFieldNames = builder.RawFieldNames(&CourseFile{})
courseFileRows = strings.Join(courseFileFieldNames, ",")
courseFileRowsExpectAutoSet = strings.Join(stringx.Remove(courseFileFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
courseFileRowsWithPlaceHolder = strings.Join(stringx.Remove(courseFileFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)
type (
courseFileModel interface {
Insert(ctx context.Context, data *CourseFile) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*CourseFile, error)
Update(ctx context.Context, data *CourseFile) error
Delete(ctx context.Context, id int64) error
}
defaultCourseFileModel struct {
conn sqlx.SqlConn
table string
}
CourseFile struct {
Id int64 `db:"id"` // 主键ID
ContentId int64 `db:"content_id"` // 关联的内容ID如课程章节ID等
Title string `db:"title"` // 文件标题
FileType string `db:"file_type"` // 文件类型如pdf、video、doc等
FileUrl string `db:"file_url"` // 文件存储URL
CreateTime time.Time `db:"create_time"` // 创建时间
UpdateTime time.Time `db:"update_time"` // 更新时间(自动更新)
}
)
func newCourseFileModel(conn sqlx.SqlConn) *defaultCourseFileModel {
return &defaultCourseFileModel{
conn: conn,
table: "`course_file`",
}
}
func (m *defaultCourseFileModel) Delete(ctx context.Context, id int64) error {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
_, err := m.conn.ExecCtx(ctx, query, id)
return err
}
func (m *defaultCourseFileModel) FindOne(ctx context.Context, id int64) (*CourseFile, error) {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", courseFileRows, m.table)
var resp CourseFile
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 *defaultCourseFileModel) Insert(ctx context.Context, data *CourseFile) (sql.Result, error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, courseFileRowsExpectAutoSet)
ret, err := m.conn.ExecCtx(ctx, query, data.ContentId, data.Title, data.FileType, data.FileUrl)
return ret, err
}
func (m *defaultCourseFileModel) Update(ctx context.Context, data *CourseFile) error {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, courseFileRowsWithPlaceHolder)
_, err := m.conn.ExecCtx(ctx, query, data.ContentId, data.Title, data.FileType, data.FileUrl, data.Id)
return err
}
func (m *defaultCourseFileModel) tableName() string {
return m.table
}

View File

@@ -0,0 +1,5 @@
package model
import "github.com/zeromicro/go-zero/core/stores/sqlx"
var ErrNotFound = sqlx.ErrNotFound