完成课程资源Handler
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/handler"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/courseresourceapi.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
Name: course_resource_api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
@@ -4,15 +4,18 @@
|
||||
package course_resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/logic/course_resource"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func CreateCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
func CreateCourseResourceHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.CreateCourseResourceReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
@@ -20,7 +23,22 @@ func CreateCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
l := course_resource.NewCreateCourseResourceLogic(r.Context(), svcCtx)
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
mysqlCfg.Username,
|
||||
mysqlCfg.Password,
|
||||
mysqlCfg.Host,
|
||||
mysqlCfg.Port,
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
CourseResourceModel := model.NewCourseResourceModel(conn)
|
||||
|
||||
l := course_resource.NewCreateCourseResourceLogic(r.Context(), cfg, CourseResourceModel)
|
||||
resp, err := l.CreateCourseResource(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
@@ -4,15 +4,18 @@
|
||||
package course_resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/logic/course_resource"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func DeleteCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
func DeleteCourseResourceHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteCourseResourceReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
@@ -20,7 +23,22 @@ func DeleteCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
l := course_resource.NewDeleteCourseResourceLogic(r.Context(), svcCtx)
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
mysqlCfg.Username,
|
||||
mysqlCfg.Password,
|
||||
mysqlCfg.Host,
|
||||
mysqlCfg.Port,
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
CourseResourceModel := model.NewCourseResourceModel(conn)
|
||||
|
||||
l := course_resource.NewDeleteCourseResourceLogic(r.Context(), cfg, CourseResourceModel)
|
||||
resp, err := l.DeleteCourseResource(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
@@ -4,15 +4,18 @@
|
||||
package course_resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/logic/course_resource"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func GetCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
func GetCourseResourceHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetCourseResourceReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
@@ -20,7 +23,22 @@ func GetCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
l := course_resource.NewGetCourseResourceLogic(r.Context(), svcCtx)
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
mysqlCfg.Username,
|
||||
mysqlCfg.Password,
|
||||
mysqlCfg.Host,
|
||||
mysqlCfg.Port,
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
CourseResourceModel := model.NewCourseResourceModel(conn)
|
||||
|
||||
l := course_resource.NewGetCourseResourceLogic(r.Context(), cfg, CourseResourceModel)
|
||||
resp, err := l.GetCourseResource(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
@@ -4,23 +4,40 @@
|
||||
package course_resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/logic/course_resource"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func ListCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
func ListCourseResourceHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ListCourseResourceReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
mysqlCfg.Username,
|
||||
mysqlCfg.Password,
|
||||
mysqlCfg.Host,
|
||||
mysqlCfg.Port,
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
|
||||
l := course_resource.NewListCourseResourceLogic(r.Context(), svcCtx)
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
CourseResourceModel := model.NewCourseResourceModel(conn)
|
||||
|
||||
l := course_resource.NewListCourseResourceLogic(r.Context(), cfg, CourseResourceModel)
|
||||
resp, err := l.ListCourseResource(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
@@ -4,15 +4,18 @@
|
||||
package course_resource
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/logic/course_resource"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
func UpdateCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
func UpdateCourseResourceHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateCourseResourceReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
@@ -20,7 +23,22 @@ func UpdateCourseResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
l := course_resource.NewUpdateCourseResourceLogic(r.Context(), svcCtx)
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
mysqlCfg.Username,
|
||||
mysqlCfg.Password,
|
||||
mysqlCfg.Host,
|
||||
mysqlCfg.Port,
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
CourseResourceModel := model.NewCourseResourceModel(conn)
|
||||
|
||||
l := course_resource.NewUpdateCourseResourceLogic(r.Context(), cfg, CourseResourceModel)
|
||||
resp, err := l.UpdateCourseResource(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
@@ -1,10 +0,0 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/rest"
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.1
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
course_resource "github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/handler/course_resource"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/",
|
||||
Handler: course_resource.CreateCourseResourceHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPut,
|
||||
Path: "/",
|
||||
Handler: course_resource.UpdateCourseResourceHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/:id",
|
||||
Handler: course_resource.GetCourseResourceHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodDelete,
|
||||
Path: "/:id",
|
||||
Handler: course_resource.DeleteCourseResourceHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/list",
|
||||
Handler: course_resource.ListCourseResourceHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api/course-resource"),
|
||||
)
|
||||
}
|
||||
@@ -6,7 +6,8 @@ package course_resource
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -15,14 +16,16 @@ import (
|
||||
type CreateCourseResourceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
cfg *config.Config
|
||||
model model.CourseResourceModel
|
||||
}
|
||||
|
||||
func NewCreateCourseResourceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCourseResourceLogic {
|
||||
func NewCreateCourseResourceLogic(ctx context.Context, cfg *config.Config, model model.CourseResourceModel) *CreateCourseResourceLogic {
|
||||
return &CreateCourseResourceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
cfg: cfg,
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ package course_resource
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -15,14 +16,16 @@ import (
|
||||
type DeleteCourseResourceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
cfg *config.Config
|
||||
model model.CourseResourceModel
|
||||
}
|
||||
|
||||
func NewDeleteCourseResourceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteCourseResourceLogic {
|
||||
func NewDeleteCourseResourceLogic(ctx context.Context, cfg *config.Config, model model.CourseResourceModel) *DeleteCourseResourceLogic {
|
||||
return &DeleteCourseResourceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
cfg: cfg,
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ package course_resource
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -15,14 +16,16 @@ import (
|
||||
type GetCourseResourceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
cfg *config.Config
|
||||
model model.CourseResourceModel
|
||||
}
|
||||
|
||||
func NewGetCourseResourceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCourseResourceLogic {
|
||||
func NewGetCourseResourceLogic(ctx context.Context, cfg *config.Config, model model.CourseResourceModel) *GetCourseResourceLogic {
|
||||
return &GetCourseResourceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
cfg: cfg,
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ package course_resource
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -15,14 +16,16 @@ import (
|
||||
type ListCourseResourceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
cfg *config.Config
|
||||
model model.CourseResourceModel
|
||||
}
|
||||
|
||||
func NewListCourseResourceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCourseResourceLogic {
|
||||
func NewListCourseResourceLogic(ctx context.Context, cfg *config.Config, model model.CourseResourceModel) *ListCourseResourceLogic {
|
||||
return &ListCourseResourceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
cfg: cfg,
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@ package course_resource
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/svc"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -15,14 +16,16 @@ import (
|
||||
type UpdateCourseResourceLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
cfg *config.Config
|
||||
model model.CourseResourceModel
|
||||
}
|
||||
|
||||
func NewUpdateCourseResourceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCourseResourceLogic {
|
||||
func NewUpdateCourseResourceLogic(ctx context.Context, cfg *config.Config, model model.CourseResourceModel) *UpdateCourseResourceLogic {
|
||||
return &UpdateCourseResourceLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
cfg: cfg,
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.1
|
||||
|
||||
package svc
|
||||
|
||||
import (
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_resource/internal/config"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user