添加视频案例实现层

This commit is contained in:
2025-10-31 14:10:26 +08:00
parent f5b4b3fce9
commit 55b2e201f8
3 changed files with 127 additions and 0 deletions

View File

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

View File

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

View File

@@ -0,0 +1,93 @@
// 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 (
videoCaseFieldNames = builder.RawFieldNames(&VideoCase{})
videoCaseRows = strings.Join(videoCaseFieldNames, ",")
videoCaseRowsExpectAutoSet = strings.Join(stringx.Remove(videoCaseFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
videoCaseRowsWithPlaceHolder = strings.Join(stringx.Remove(videoCaseFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
)
type (
videoCaseModel interface {
Insert(ctx context.Context, data *VideoCase) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*VideoCase, error)
Update(ctx context.Context, data *VideoCase) error
Delete(ctx context.Context, id int64) error
}
defaultVideoCaseModel struct {
conn sqlx.SqlConn
table string
}
VideoCase struct {
Id int64 `db:"id"` // 视频案例ID主键
Title string `db:"title"` // 视频案例标题
Intro sql.NullString `db:"intro"` // 视频简介(描述案例背景、内容等)
VideoUrl string `db:"video_url"` // 视频播放地址如MP4 URL、视频平台嵌入链接
DesignerNames string `db:"designer_names"` // 设计人员名单(多人用逗号分隔)
TutorNames string `db:"tutor_names"` // 指导老师名单(多人用逗号分隔)
Sort int64 `db:"sort"` // 排序(数字越小越靠前)
CreateTime time.Time `db:"create_time"` // 创建时间
UpdateTime time.Time `db:"update_time"` // 更新时间
}
)
func newVideoCaseModel(conn sqlx.SqlConn) *defaultVideoCaseModel {
return &defaultVideoCaseModel{
conn: conn,
table: "`video_case`",
}
}
func (m *defaultVideoCaseModel) 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 *defaultVideoCaseModel) FindOne(ctx context.Context, id int64) (*VideoCase, error) {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", videoCaseRows, m.table)
var resp VideoCase
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 *defaultVideoCaseModel) Insert(ctx context.Context, data *VideoCase) (sql.Result, error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?)", m.table, videoCaseRowsExpectAutoSet)
ret, err := m.conn.ExecCtx(ctx, query, data.Title, data.Intro, data.VideoUrl, data.DesignerNames, data.TutorNames, data.Sort)
return ret, err
}
func (m *defaultVideoCaseModel) Update(ctx context.Context, data *VideoCase) error {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, videoCaseRowsWithPlaceHolder)
_, err := m.conn.ExecCtx(ctx, query, data.Title, data.Intro, data.VideoUrl, data.DesignerNames, data.TutorNames, data.Sort, data.Id)
return err
}
func (m *defaultVideoCaseModel) tableName() string {
return m.table
}