添加course的业务层和实现层
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/logic/course_content"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func AddContentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.AddContentReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := course_content.NewAddContentLogic(r.Context(), svcCtx)
|
||||
resp, err := l.AddContent(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/logic/course_content"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func DeleteContentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteContentReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := course_content.NewDeleteContentLogic(r.Context(), svcCtx)
|
||||
resp, err := l.DeleteContent(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/logic/course_content"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func GetContentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetContentReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := course_content.NewGetContentLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetContent(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/logic/course_content"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func GetContentListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetContentListReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := course_content.NewGetContentListLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetContentList(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/logic/course_content"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func UpdateContentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateContentReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := course_content.NewUpdateContentLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UpdateContent(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddContentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAddContentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddContentLogic {
|
||||
return &AddContentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AddContentLogic) AddContent(req *types.AddContentReq) (resp *types.AddContentResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteContentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteContentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteContentLogic {
|
||||
return &DeleteContentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteContentLogic) DeleteContent(req *types.DeleteContentReq) (resp *types.DeleteContentResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetContentListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetContentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContentListLogic {
|
||||
return &GetContentListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetContentListLogic) GetContentList(req *types.GetContentListReq) (resp *types.GetContentListResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetContentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetContentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContentLogic {
|
||||
return &GetContentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetContentLogic) GetContent(req *types.GetContentReq) (resp *types.GetContentResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package course_content
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_content/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateContentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateContentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateContentLogic {
|
||||
return &UpdateContentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateContentLogic) UpdateContent(req *types.UpdateContentReq) (resp *types.UpdateContentResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ CourseContentModel = (*customCourseContentModel)(nil)
|
||||
|
||||
type (
|
||||
// CourseContentModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customCourseContentModel.
|
||||
CourseContentModel interface {
|
||||
courseContentModel
|
||||
withSession(session sqlx.Session) CourseContentModel
|
||||
}
|
||||
|
||||
customCourseContentModel struct {
|
||||
*defaultCourseContentModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewCourseContentModel returns a model for the database table.
|
||||
func NewCourseContentModel(conn sqlx.SqlConn) CourseContentModel {
|
||||
return &customCourseContentModel{
|
||||
defaultCourseContentModel: newCourseContentModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customCourseContentModel) withSession(session sqlx.Session) CourseContentModel {
|
||||
return NewCourseContentModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.9.2
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
courseContentFieldNames = builder.RawFieldNames(&CourseContent{})
|
||||
courseContentRows = strings.Join(courseContentFieldNames, ",")
|
||||
courseContentRowsExpectAutoSet = strings.Join(stringx.Remove(courseContentFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
|
||||
courseContentRowsWithPlaceHolder = strings.Join(stringx.Remove(courseContentFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
|
||||
)
|
||||
|
||||
type (
|
||||
courseContentModel interface {
|
||||
Insert(ctx context.Context, data *CourseContent) (sql.Result, error)
|
||||
FindOne(ctx context.Context, id int64) (*CourseContent, error)
|
||||
Update(ctx context.Context, data *CourseContent) error
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
|
||||
defaultCourseContentModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
CourseContent struct {
|
||||
Id int64 `db:"id"` // 内容ID(主键)
|
||||
CourseId int64 `db:"course_id"` // 关联课程ID
|
||||
ParentId int64 `db:"parent_id"` // 父级ID(0表示章节,>0表示小节,关联自身id)
|
||||
Title string `db:"title"` // 章节/小节标题
|
||||
Content sql.NullString `db:"content"` // 内容详情(如视频地址、图文内容等)
|
||||
Sort int64 `db:"sort"` // 排序(数字越小越靠前)
|
||||
}
|
||||
)
|
||||
|
||||
func newCourseContentModel(conn sqlx.SqlConn) *defaultCourseContentModel {
|
||||
return &defaultCourseContentModel{
|
||||
conn: conn,
|
||||
table: "`course_content`",
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultCourseContentModel) 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 *defaultCourseContentModel) FindOne(ctx context.Context, id int64) (*CourseContent, error) {
|
||||
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", courseContentRows, m.table)
|
||||
var resp CourseContent
|
||||
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 *defaultCourseContentModel) Insert(ctx context.Context, data *CourseContent) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?)", m.table, courseContentRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.CourseId, data.ParentId, data.Title, data.Content, data.Sort)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultCourseContentModel) Update(ctx context.Context, data *CourseContent) error {
|
||||
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, courseContentRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.CourseId, data.ParentId, data.Title, data.Content, data.Sort, data.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultCourseContentModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
server/internal/course_content/internal/model/vars.go
Normal file
5
server/internal/course_content/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
|
||||
70
server/internal/course_content/internal/types/types.go
Normal file
70
server/internal/course_content/internal/types/types.go
Normal file
@@ -0,0 +1,70 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
|
||||
package types
|
||||
|
||||
type AddContentReq struct {
|
||||
CourseId int `json:"course_id" form:"course_id"` // 课程ID,必填
|
||||
ParentId int `json:"parent_id" form:"parent_id"` // 父级ID,必填
|
||||
Title string `json:"title" form:"title"` // 标题,必填
|
||||
Content string `json:"content" form:"content"` // 内容详情,可选
|
||||
Sort int `json:"sort" form:"sort"` // 排序,可选
|
||||
}
|
||||
|
||||
type AddContentResp struct {
|
||||
BaseResp
|
||||
Data int `json:"data"` // 新增内容的ID
|
||||
}
|
||||
|
||||
type BaseResp struct {
|
||||
Code int `json:"code"` // 状态码,0表示成功
|
||||
Message string `json:"message"` // 提示信息
|
||||
Data interface{} `json:"data"` // 响应数据
|
||||
}
|
||||
|
||||
type CourseContent struct {
|
||||
Id int `json:"id"` // 内容ID(主键)
|
||||
CourseId int `json:"course_id"` // 关联课程ID
|
||||
ParentId int `json:"parent_id"` // 父级ID(0表示章节,>0表示小节)
|
||||
Title string `json:"title"` // 章节/小节标题
|
||||
Content string `json:"content"` // 内容详情
|
||||
Sort int `json:"sort"` // 排序
|
||||
}
|
||||
|
||||
type DeleteContentReq struct {
|
||||
Id int `json:"id" form:"id" uri:"id"` // 内容ID,必填
|
||||
}
|
||||
|
||||
type DeleteContentResp struct {
|
||||
BaseResp
|
||||
}
|
||||
|
||||
type GetContentListReq struct {
|
||||
CourseId int `json:"course_id" form:"course_id"` // 课程ID,必填
|
||||
ParentId int `json:"parent_id" form:"parent_id"` // 父级ID,可选,0表示获取章节
|
||||
}
|
||||
|
||||
type GetContentListResp struct {
|
||||
BaseResp
|
||||
Data []CourseContent `json:"data"` // 内容列表
|
||||
}
|
||||
|
||||
type GetContentReq struct {
|
||||
Id int `json:"id" form:"id" uri:"id"` // 内容ID,必填
|
||||
}
|
||||
|
||||
type GetContentResp struct {
|
||||
BaseResp
|
||||
Data CourseContent `json:"data"` // 内容详情
|
||||
}
|
||||
|
||||
type UpdateContentReq struct {
|
||||
Id int `json:"id" form:"id"` // 内容ID,必填
|
||||
Title string `json:"title" form:"title"` // 标题,可选
|
||||
Content string `json:"content" form:"content"` // 内容详情,可选
|
||||
Sort int `json:"sort" form:"sort"` // 排序,可选
|
||||
}
|
||||
|
||||
type UpdateContentResp struct {
|
||||
BaseResp
|
||||
}
|
||||
Reference in New Issue
Block a user