Skip to content

Commit

Permalink
fix(gomall): category page
Browse files Browse the repository at this point in the history
Signed-off-by: rogerogers <rogers@rogerogers.com>
  • Loading branch information
rogerogers committed May 12, 2024
1 parent b93889a commit 4725b1b
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ func Category(ctx context.Context, c *app.RequestContext) {
return
}

utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
c.HTML(consts.StatusOK, "category", resp)
}
19 changes: 19 additions & 0 deletions gomall/app/frontend/biz/handler/product/product_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,22 @@ func GetProduct(ctx context.Context, c *app.RequestContext) {
}
c.HTML(consts.StatusOK, "product", utils.WarpResponse(ctx, c, resp))
}

// SearchProducs .
// @router /search [GET]
func SearchProducs(ctx context.Context, c *app.RequestContext) {
var err error
var req product.SearchProductsReq
err = c.BindAndValidate(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}

resp, err := service.NewSearchProducsService(ctx, c).Run(&req)
if err != nil {
utils.SendErrResponse(ctx, c, consts.StatusOK, err)
return
}
c.HTML(consts.StatusOK, "search", utils.WarpResponse(ctx, c, resp))
}
15 changes: 15 additions & 0 deletions gomall/app/frontend/biz/handler/product/product_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ func TestGetProduct(t *testing.T) {
// assert.DeepEqual(t, 200, resp.StatusCode())
// assert.DeepEqual(t, "null", string(resp.Body()))
}

func TestSearchProducs(t *testing.T) {
h := server.Default()
h.GET("/search", SearchProducs)
path := "/search" // todo: you can customize query
body := &ut.Body{Body: bytes.NewBufferString(""), Len: 1} // todo: you can customize body
header := ut.Header{} // todo: you can customize header
w := ut.PerformRequest(h.Engine, "GET", path, body, header)
resp := w.Result()
t.Log(string(resp.Body()))

// todo edit your unit test.
// assert.DeepEqual(t, 200, resp.StatusCode())
// assert.DeepEqual(t, "null", string(resp.Body()))
}
5 changes: 5 additions & 0 deletions gomall/app/frontend/biz/router/product/middleware.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 1 addition & 14 deletions gomall/app/frontend/biz/router/product/product_page.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions gomall/app/frontend/biz/service/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"context"

category "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/category"
common "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/common"
"github.com/cloudwego/biz-demo/gomall/app/frontend/infra/rpc"
"github.com/cloudwego/biz-demo/gomall/rpc_gen/kitex_gen/product"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
)

type CategoryService struct {
Expand All @@ -31,6 +33,10 @@ func NewCategoryService(Context context.Context, RequestContext *app.RequestCont
return &CategoryService{RequestContext: RequestContext, Context: Context}
}

func (h *CategoryService) Run(req *category.CategoryReq) (resp *common.Empty, err error) {
return
func (h *CategoryService) Run(req *category.CategoryReq) (resp map[string]any, err error) {
p, _ := rpc.ProductClient.ListProducts(h.Context, &product.ListProductsReq{CategoryName: req.Category})
return utils.H{
"title": "Category",
"items": p.Products,
}, nil
}
45 changes: 45 additions & 0 deletions gomall/app/frontend/biz/service/search_producs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package service

import (
"context"

product "github.com/cloudwego/biz-demo/gomall/app/frontend/hertz_gen/frontend/product"
"github.com/cloudwego/biz-demo/gomall/app/frontend/infra/rpc"
rpcproduct "github.com/cloudwego/biz-demo/gomall/rpc_gen/kitex_gen/product"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/utils"
)

type SearchProducsService struct {
RequestContext *app.RequestContext
Context context.Context
}

func NewSearchProducsService(Context context.Context, RequestContext *app.RequestContext) *SearchProducsService {
return &SearchProducsService{RequestContext: RequestContext, Context: Context}
}

func (h *SearchProducsService) Run(req *product.SearchProductsReq) (resp map[string]any, err error) {
p, err := rpc.ProductClient.SearchProducts(h.Context, &rpcproduct.SearchProductsReq{Query: req.Q})
if err != nil {
return nil, err
}
return utils.H{
"items": p.Results,
"q": req.Q,
}, nil
}
13 changes: 7 additions & 6 deletions gomall/app/frontend/hertz_gen/api/api.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 21 additions & 20 deletions gomall/app/frontend/hertz_gen/frontend/category/category_page.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions gomall/app/frontend/hertz_gen/frontend/common/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4725b1b

Please sign in to comment.