Skip to content

Commit

Permalink
Merge pull request #28 from feng99/main
Browse files Browse the repository at this point in the history
提交订单创建功能
  • Loading branch information
zhoushuguang committed Jul 7, 2022
2 parents fc1612d + d21fa2e commit faf0719
Show file tree
Hide file tree
Showing 52 changed files with 1,697 additions and 348 deletions.
6 changes: 6 additions & 0 deletions .idea/encodings.xml

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

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ A busy sum, when there is time to make up for it

[go-zero微服务实战系列(六、缓存一致性保证)](https://mp.weixin.qq.com/s/422ZHs81y7nN9Sgb_ESsgg)

[go-zero微服务实战系列(七、请求量这么高该如何优化)](https://mp.weixin.qq.com/s/pPPSPZJispmITY9Wsi7hUg)

[go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)](https://mp.weixin.qq.com/s/OAbuzj876SrrcB5WO_2FuA)

[go-zero微服务实战系列(九、极致优化秒杀性能)](https://mp.weixin.qq.com/s/8VSS9WNSy4jkOSSIA4BmLw)


## Basic Environment
Expand Down Expand Up @@ -55,4 +60,4 @@ A busy sum, when there is time to make up for it
| -------------- | ------|
| user rpc | 9001 |
| product rpc | 9002 |
| order rpc | 9003 |
| order rpc | 9003 |
21 changes: 19 additions & 2 deletions apps/app/api/api.api
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"apis/userReceiveAddress.api"
"apis/user.api"
"apis/usercollection.api"
"apis/order.api"
)

type (
Expand Down Expand Up @@ -175,7 +176,7 @@ service api-api {
get /v1/product/detail (ProductDetailRequest) returns (ProductDetailResponse)
}

//========================> user-user v1 <===================================
//========================> user v1 <===================================
//no need login
@server(
prefix: v1/user
Expand All @@ -191,7 +192,7 @@ service api-api {
@server(
prefix: v1/user
group: user
jwt: JwtAuth //使用jwt组件
jwt: JwtAuth //use jwt
)
service api-api {
@doc "get user info"
Expand Down Expand Up @@ -225,4 +226,20 @@ service api-api {
@doc "get user collection list"
@handler userCollectionList
get /getCollectionList (UserCollectionListReq) returns (UserCollectionListRes)
}


}

//========================> order v1 <===================================
//need login
@server(
prefix: v1/order
group: order
jwt: JwtAuth //use jwt
)
service api-api {
@doc "add order"
@handler addOrder
post /add (OrderAddReq) returns (OrderAddResp)
}
4 changes: 1 addition & 3 deletions apps/app/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ import (
var configFile = flag.String("f", "etc/api-api.yaml", "the etc file")

func init() {
//close statis log
logx.DisableStat()
}

func main() {
flag.Parse()

//close statis log
logx.DisableStat()

var c config.Config
conf.MustLoad(*configFile, &c)

Expand Down
46 changes: 46 additions & 0 deletions apps/app/api/apis/order.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
syntax = "v1"

info(
title: "订单管理"
desc: "订单管理"
author: "刘羽禅"
email: "18500606035@163.com"
version: "v1"
)

// 订单表
type Orders struct {
Id string `json:"id"` //订单id
Userid uint64 `json:"userid"` //用户id
Shoppingid int64 `json:"shoppingid"` //收货信息表id
Payment float64 `json:"payment"` //实际付款金额,单位是元,保留两位小数
Paymenttype int8 `json:"paymenttype"` //支付类型,1-在线支付
Postage int `json:"postage"` //运费,单位是元
Status int16 `json:"status"` //订单状态:0-已取消-10-未付款,20-已付款,30-待发货 40-待收货,50-交易成功,60-交易关闭
CreateTime int64 `json:"create_time"` //创建时间
UpdateTime int64 `json:"update_time"` //更新时间
}

//创建订单
type (
OrderAddReq {
receiveAddressId int64 `json:"receiveAddressId"` //用户收货地址表id
Postage int64 `json:"postage"` //运费,单位是元
Productid int64 `json:"productid"` //商品id
Quantity int64 `json:"quantity"` //商品数量
}
OrderAddResp {
Id string `json:"id"` //订单id
}
)
//创建订单

//订单详情
type (
OrderInfoReq {
}
OrderInfoResp {
OrderInfo Orders `json:"orderInfo"`
}
)
//订单详情
25 changes: 25 additions & 0 deletions apps/app/api/internal/handler/order/addorderhandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package order

import (
"net/http"

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

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

l := order.NewAddOrderLogic(r.Context(), svcCtx)
resp, err := l.AddOrder(&req)
result.HttpResult(r, w, resp, err)
}
}
13 changes: 13 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.

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

import (
"context"
"encoding/json"

"github.com/pkg/errors"
"github.com/zhoushuguang/lebron/apps/app/api/internal/svc"
"github.com/zhoushuguang/lebron/apps/app/api/internal/types"
"github.com/zhoushuguang/lebron/apps/order/rpc/order"

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

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

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

func (l *AddOrderLogic) AddOrder(req *types.OrderAddReq) (resp *types.OrderAddResp, err error) {
uid, err := l.ctx.Value("uid").(json.Number).Int64()
if err != nil {
return nil, err
}
addOrderReq := order.AddOrderReq{
Userid: uid,
Productid: req.Productid,
Quantity: req.Quantity,
Postage: req.Postage,
ReceiveAddressId: req.ReceiveAddressId,
}
addOrderResp, err := l.svcCtx.OrderRPC.CreateOrderDTM(l.ctx, &addOrderReq)
if err != nil {
return nil, errors.Wrapf(err, "req: %+v", req)
}
return &types.OrderAddResp{
Id: addOrderResp.Id,
}, nil
}
6 changes: 3 additions & 3 deletions apps/app/api/internal/logic/orderlistlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ func (l *OrderListLogic) OrderList(req *types.OrderListRequest) (resp *types.Ord
}
var pids []string
for _, o := range orderRet.Orders {
pids = append(pids, strconv.Itoa(int(o.ProductId)))
pids = append(pids, strconv.Itoa(int(o.Id)))
}
productRet, err := l.svcCtx.ProductRPC.Products(l.ctx, &product.ProductRequest{ProductIds: strings.Join(pids, ",")})
if err != nil {
return nil, err
}
var orders []*types.Order
for _, o := range orderRet.Orders {
if p, ok := productRet.Products[o.ProductId]; ok {
if p, ok := productRet.Products[o.Id]; ok {
orders = append(orders, &types.Order{
OrderID: o.OrderId,
OrderID: o.Orderid,
ProductName: p.Name,
})
}
Expand Down
8 changes: 4 additions & 4 deletions apps/app/api/internal/logic/user/loginlogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic
}

func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) {
res, err := l.svcCtx.UserRPC.Login(l.ctx, &user.LoginRequest{
Username: req.Username,
Password: req.Password,
})
var loginReq user.LoginRequest
loginReq.Username = req.Username
loginReq.Password = req.Password
res, err := l.svcCtx.UserRPC.Login(l.ctx, &loginReq)
if err != nil {
return nil, errors.Wrapf(err, "req: %+v", req)
}
Expand Down
31 changes: 31 additions & 0 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.

18 changes: 16 additions & 2 deletions apps/order/rpc/etc/order.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
Name: order.rpc
ListenOn: 127.0.0.1:9696
ListenOn: 127.0.0.1:9003
Mode: dev
Etcd:
Hosts:
- 127.0.0.1:2379
- 127.0.0.1:2379
Key: order.rpc

UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc

ProductRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: product.rpc

Telemetry:
Endpoint: http://127.0.0.1:14268/api/traces
DataSource: root:123456@tcp(127.0.0.1:3306)/orders?parseTime=true
Expand Down
2 changes: 2 additions & 0 deletions apps/order/rpc/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ type Config struct {
zrpc.RpcServerConf
DataSource string
CacheRedis cache.CacheConf
ProductRpc zrpc.RpcClientConf
UserRpc zrpc.RpcClientConf
}
Loading

0 comments on commit faf0719

Please sign in to comment.