Skip to content

Commit

Permalink
Merge pull request #5 from zhoushuguang/article-four
Browse files Browse the repository at this point in the history
Article four
  • Loading branch information
zhoushuguang authored Jun 11, 2022
2 parents f437b0e + 9b4b569 commit c9bc6f9
Show file tree
Hide file tree
Showing 50 changed files with 1,427 additions and 543 deletions.
32 changes: 23 additions & 9 deletions apps/app/api/api.api
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ type (
}

Product {
ID int64 `json:"id"` // 商品ID
Name string `json:"name"` // 产品名称
Description string `json:"description"` // 商品描述
Price float64 `json:"price"` // 商品价格
Stock int64 `json:"stock"` // 库存
Category string `json:"category"` // 分类
Status int64 `json:"status"` // 状态:1-正常,2-下架
CreateTime int64 `json:"create_time"` // 创建时间
UpdateTime int64 `json:"update_time"` // 更新时间
ID int64 `json:"id"` // 商品ID
Name string `json:"name"` // 产品名称
Images []string `json:"images"` // 图片
Description string `json:"description"` // 商品描述
Price float64 `json:"price"` // 商品价格
Stock int64 `json:"stock"` // 库存
Category string `json:"category"` // 分类
Status int64 `json:"status"` // 状态:1-正常,2-下架
CreateTime int64 `json:"create_time"` // 创建时间
UpdateTime int64 `json:"update_time"` // 更新时间
}

RecommendRequest {
Expand Down Expand Up @@ -123,6 +124,15 @@ type (
ProductImage string `json:"product_image"`
ProductDescription string `json:"product_description"`
}

ProductDetailRequest {
ProductID int64 `form:"product_id"`
}

ProductDetailResponse {
Product *Product `json:"product"`
Comments []*Comment `json:"comments"`
}
)

service api-api {
Expand Down Expand Up @@ -153,4 +163,8 @@ service api-api {
@doc "订单列表"
@handler OrderListHandler
get /v1/order/list (OrderListRequest) returns (OrderListResponse)

@doc "商品详情"
@handler ProductDetailHandler
get /v1/product/detail (ProductDetailRequest) returns (ProductDetailResponse)
}
5 changes: 5 additions & 0 deletions apps/app/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import (
"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"

"github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/api-api.yaml", "the config file")

func init() {
logx.DisableStat()
}

func main() {
flag.Parse()

Expand Down
6 changes: 6 additions & 0 deletions apps/app/api/etc/api-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ ProductRPC:
Hosts:
- 127.0.0.1:2379
Key: product.rpc
ReplyRPC:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: reply.rpc
NonBlock: true
1 change: 1 addition & 0 deletions apps/app/api/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ type Config struct {
rest.RestConf
OrderRPC zrpc.RpcClientConf
ProductRPC zrpc.RpcClientConf
ReplyRPC zrpc.RpcClientConf
}
28 changes: 28 additions & 0 deletions apps/app/api/internal/handler/productdetailhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package handler

import (
"net/http"

"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zhoushuguang/lebron/apps/app/api/internal/logic"
"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"
"github.com/zhoushuguang/lebron/apps/app/api/internal/types"
)

func ProductDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ProductDetailRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}

l := logic.NewProductDetailLogic(r.Context(), svcCtx)
resp, err := l.ProductDetail(&req)
if err != nil {
httpx.Error(w, err)
} else {
httpx.OkJson(w, resp)
}
}
}
5 changes: 5 additions & 0 deletions apps/app/api/internal/handler/routes.go

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

63 changes: 63 additions & 0 deletions apps/app/api/internal/logic/productdetaillogic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package logic

import (
"context"

"github.com/zeromicro/go-zero/core/mr"
"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"
"github.com/zhoushuguang/lebron/apps/app/api/internal/types"
"github.com/zhoushuguang/lebron/apps/product/rpc/product"
"github.com/zhoushuguang/lebron/apps/reply/rpc/reply"

"github.com/zeromicro/go-zero/core/logx"
)

type ProductDetailLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}

func NewProductDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductDetailLogic {
return &ProductDetailLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}

func (l *ProductDetailLogic) ProductDetail(req *types.ProductDetailRequest) (resp *types.ProductDetailResponse, err error) {
var (
p *product.ProductItem
cs *reply.CommentsResponse
)
if err := mr.Finish(func() error {
var err error
if p, err = l.svcCtx.ProductRPC.Product(l.ctx, &product.ProductItemRequest{ProductId: req.ProductID}); err != nil {
return err
}
return nil
}, func() error {
var err error
if cs, err = l.svcCtx.ReplyRPC.Comments(l.ctx, &reply.CommentsRequest{TargetId: req.ProductID}); err != nil {
logx.Errorf("get comments error: %v", err)
}
return nil
}); err != nil {
return nil, err
}
var comments []*types.Comment
for _, c := range cs.Comments {
comments = append(comments, &types.Comment{
ID: c.Id,
Content: c.Content,
})
}
return &types.ProductDetailResponse{
Product: &types.Product{
ID: p.ProductId,
Name: p.Name,
},
Comments: comments,
}, nil
}
3 changes: 3 additions & 0 deletions apps/app/api/internal/svc/servicecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/zhoushuguang/lebron/apps/app/api/internal/config"
"github.com/zhoushuguang/lebron/apps/order/rpc/order"
"github.com/zhoushuguang/lebron/apps/product/rpc/product"
"github.com/zhoushuguang/lebron/apps/reply/rpc/reply"

"github.com/zeromicro/go-zero/zrpc"
)
Expand All @@ -12,12 +13,14 @@ type ServiceContext struct {
Config config.Config
OrderRPC order.Order
ProductRPC product.Product
ReplyRPC reply.Reply
}

func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
OrderRPC: order.NewOrder(zrpc.MustNewClient(c.OrderRPC)),
ProductRPC: product.NewProduct(zrpc.MustNewClient(c.ProductRPC)),
ReplyRPC: reply.NewReply(zrpc.MustNewClient(c.ReplyRPC)),
}
}
28 changes: 19 additions & 9 deletions apps/app/api/internal/types/types.go

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

16 changes: 8 additions & 8 deletions apps/product/admin/admin.api
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
type Request {
Name string `path:"name,options=you|me"`
}
syntax = "v1"

type Response {
Message string `json:"message"`
}
type (
UploadImageResponse {
Success bool `json:"success"`
}
)

service admin-api {
@handler AdminHandler
get /from/:name(Request) returns (Response)
@handler UploadImageHandler
post /v1/upload/image() returns (UploadImageResponse)
}
3 changes: 3 additions & 0 deletions apps/product/admin/etc/admin-api.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Name: admin-api
Host: 0.0.0.0
Port: 8888
OSSEndpoint: https://oss-cn-hangzhou.aliyuncs.com
AccessKeyID: xxxxxxxxxxxx
AccessKeySecret: xxxxxxxxxxxx
3 changes: 3 additions & 0 deletions apps/product/admin/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import "github.com/zeromicro/go-zero/rest"

type Config struct {
rest.RestConf
OSSEndpoint string
AccessKeyID string
AccessKeySecret string
}
6 changes: 3 additions & 3 deletions apps/product/admin/internal/handler/routes.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,12 @@ import (
"github.com/zeromicro/go-zero/rest/httpx"
"github.com/zhoushuguang/lebron/apps/product/admin/internal/logic"
"github.com/zhoushuguang/lebron/apps/product/admin/internal/svc"
"github.com/zhoushuguang/lebron/apps/product/admin/internal/types"
)

func AdminHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
func UploadImageHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
httpx.Error(w, err)
return
}

l := logic.NewAdminLogic(r.Context(), svcCtx)
resp, err := l.Admin(&req)
l := logic.NewUploadImageLogic(r.Context(), svcCtx, r)
resp, err := l.UploadImage()
if err != nil {
httpx.Error(w, err)
} else {
Expand Down
30 changes: 0 additions & 30 deletions apps/product/admin/internal/logic/adminlogic.go

This file was deleted.

Loading

0 comments on commit c9bc6f9

Please sign in to comment.