实现category service基础接口
This commit is contained in:
50
gateway/internal/handler/createcategoryhandler.go
Normal file
50
gateway/internal/handler/createcategoryhandler.go
Normal file
@ -0,0 +1,50 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func createCategoryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.CreateCategoryReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewCreateCategoryLogic(r.Context(), svcCtx)
|
||||
resp, err := l.CreateCategory(&req)
|
||||
if err != nil {
|
||||
if s, ok := status.FromError(err); ok {
|
||||
// 判断 gRPC 错误码,并自定义 HTTP 返回
|
||||
var code int
|
||||
switch s.Code() {
|
||||
case codes.NotFound:
|
||||
code = http.StatusNotFound
|
||||
case codes.InvalidArgument:
|
||||
code = http.StatusBadRequest
|
||||
default:
|
||||
code = http.StatusInternalServerError
|
||||
}
|
||||
|
||||
// 自定义 JSON 错误格式
|
||||
httpx.WriteJson(w, code, map[string]interface{}{
|
||||
"code": code,
|
||||
"message": s.Message(),
|
||||
})
|
||||
} else {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
}
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/deletecategoryhandler.go
Normal file
28
gateway/internal/handler/deletecategoryhandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func deleteCategoryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteCategoryReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewDeleteCategoryLogic(r.Context(), svcCtx)
|
||||
resp, err := l.DeleteCategory(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/getcategoryhandler.go
Normal file
28
gateway/internal/handler/getcategoryhandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func getCategoryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetCategoryReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewGetCategoryLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetCategory(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/getcategorytreehandler.go
Normal file
28
gateway/internal/handler/getcategorytreehandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func getCategoryTreeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetCategoryTreeReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewGetCategoryTreeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetCategoryTree(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/getsystemcategorieshandler.go
Normal file
28
gateway/internal/handler/getsystemcategorieshandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func getSystemCategoriesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetSystemCategoriesReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewGetSystemCategoriesLogic(r.Context(), svcCtx)
|
||||
resp, err := l.GetSystemCategories(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
gateway/internal/handler/listcategorieshandler.go
Normal file
28
gateway/internal/handler/listcategorieshandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func listCategoriesHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.ListCategoriesReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewListCategoriesLogic(r.Context(), svcCtx)
|
||||
resp, err := l.ListCategories(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,30 +16,67 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/login",
|
||||
Path: "/user/login",
|
||||
Handler: loginHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/register",
|
||||
Path: "/user/register",
|
||||
Handler: registerHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithPrefix("/api"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
[]rest.Route{
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/category/v1",
|
||||
Handler: createCategoryHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/user/:user_id",
|
||||
Path: "/category/v1",
|
||||
Handler: listCategoriesHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/category/v1/:id",
|
||||
Handler: getCategoryHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPut,
|
||||
Path: "/category/v1/:id",
|
||||
Handler: updateCategoryHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodDelete,
|
||||
Path: "/category/v1/:id",
|
||||
Handler: deleteCategoryHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/category/v1/:id/tree",
|
||||
Handler: getCategoryTreeHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/category/v1/system/:system_id",
|
||||
Handler: getSystemCategoriesHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/user/:user_id",
|
||||
Handler: getUserInfoHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/user/logout",
|
||||
Path: "/user/logout",
|
||||
Handler: logoutHandler(serverCtx),
|
||||
},
|
||||
},
|
||||
rest.WithJwt(serverCtx.Config.JwtAuth.AccessSecret),
|
||||
rest.WithPrefix("/api"),
|
||||
)
|
||||
}
|
||||
|
||||
28
gateway/internal/handler/updatecategoryhandler.go
Normal file
28
gateway/internal/handler/updatecategoryhandler.go
Normal file
@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"godemo/gateway/internal/logic"
|
||||
"godemo/gateway/internal/svc"
|
||||
"godemo/gateway/internal/types"
|
||||
)
|
||||
|
||||
func updateCategoryHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.UpdateCategoryReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewUpdateCategoryLogic(r.Context(), svcCtx)
|
||||
resp, err := l.UpdateCategory(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user