• Enter the service workspace
$ cd mall/service/order
Copy the code

6.1 Generating order Model

  • Create SQL file
$ vim model/order.sql
Copy the code
  • Writing SQL files
CREATE TABLE `order` (
	`id` bigint unsigned NOT NULL AUTO_INCREMENT,
	`uid` bigint unsigned NOT NULL DEFAULT '0' COMMENT 'user ID',
	`pid` bigint unsigned NOT NULL DEFAULT '0' COMMENT 'product ID',
	`amount` int(10) unsigned NOT NULL DEFAULT '0'  COMMENT 'Order Amount',
	`status` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'Order Status',
	`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
	`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP.PRIMARY KEY (`id`),
	KEY `idx_uid` (`uid`),
	KEY `idx_pid` (`pid`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;
Copy the code
  • Run the template generation command
$ goctl model mysql ddl -src ./model/order.sql -dir ./model -c
Copy the code

6.2 Generating the ORDER API Service

  • Creating an API file
$ vim api/order.api
Copy the code
  • Writing API files
type (
	// Order creation
	CreateRequest {
		Uid    int64 `json:"uid"`
		Pid    int64 `json:"pid"`
		Amount int64 `json:"amount"`
		Status int64 `json:"status"`
	}
	CreateResponse {
		Id int64 `json:"id"`
	}
	// Order creation

	// Order modification
	UpdateRequest {
		Id     int64 `json:"id"`
		Uid    int64 `json:"uid,optional"`
		Pid    int64 `json:"pid,optional"`
		Amount int64 `json:"amount,optional"`
		Status int64 `json:"status,optional"`
	}
	UpdateResponse {
	}
	// Order modification

	// Order deleted
	RemoveRequest {
		Id int64 `json:"id"`
	}
	RemoveResponse {
	}
	// Order deleted

	// Order details
	DetailRequest {
		Id int64 `json:"id"`
	}
	DetailResponse {
		Id     int64 `json:"id"`
		Uid    int64 `json:"uid"`
		Pid    int64 `json:"pid"`
		Amount int64 `json:"amount"`
		Status int64 `json:"status"`
	}
	// Order details

	// Order list
	ListRequest {
		Uid int64 `json:"uid"`
	}
	ListResponse {
		Id     int64 `json:"id"`
		Uid    int64 `json:"uid"`
		Pid    int64 `json:"pid"`
		Amount int64 `json:"amount"`
		Status int64 `json:"status"`
	}
	// Order list
)

@server(
	jwt: Auth
)
service Order {
	@handler Create
	post /api/order/create(CreateRequest) returns (CreateResponse)
	
	@handler Update
	post /api/order/update(UpdateRequest) returns (UpdateResponse)
	
	@handler Remove
	post /api/order/remove(RemoveRequest) returns (RemoveResponse)
	
	@handler Detail
	post /api/order/detail(DetailRequest) returns (DetailResponse)
	
	@handler List
	post /api/order/list(ListRequest) returns (ListResponse)
}
Copy the code
  • Run the template generation command
$ goctl api go -api ./api/order.api -dir ./api
Copy the code

6.3 Generating the ORDER RPC Service

  • Create the proto file
$ vim rpc/order.proto
Copy the code
  • Write proto files
syntax = "proto3";

package orderclient;

option go_package = "order";

// Order creation
message CreateRequest {
    int64 Uid = 1;
    int64 Pid = 2;
    int64 Amount = 3;
    int64 Status = 4;
}
message CreateResponse {
	int64 id = 1;
}
// Order creation

// Order modification
message UpdateRequest {
    int64 id = 1;
    int64 Uid = 2;
    int64 Pid = 3;
    int64 Amount = 4;
    int64 Status = 5;
}
message UpdateResponse {}// Order modification

// Order deleted
message RemoveRequest {
    int64 id = 1;
}
message RemoveResponse {}// Order deleted

// Order details
message DetailRequest {
    int64 id = 1;
}
message DetailResponse {
    int64 id = 1;
    int64 Uid = 2;
    int64 Pid = 3;
    int64 Amount = 4;
    int64 Status = 5;
}
// Order details

// Order list
message ListRequest {
    int64 uid = 1;
}
message ListResponse {
    repeated DetailResponse data = 1;
}
// Order list

// Order payment
message PaidRequest {
    int64 id = 1;
}
message PaidResponse {}// Order payment

service Order {
    rpc Create(CreateRequest) returns(CreateResponse);
    rpc Update(UpdateRequest) returns(UpdateResponse);
    rpc Remove(RemoveRequest) returns(RemoveResponse);
    rpc Detail(DetailRequest) returns(DetailResponse);
    rpc List(ListRequest) returns(ListResponse);
    rpc Paid(PaidRequest) returns(PaidResponse);
}
Copy the code
  • Run the template generation command
$ goctl rpc proto -src ./rpc/order.proto -dir ./rpc
Copy the code

6.4 Writing the ORDER RPC Service

6.4.1 Modifying a Configuration File

  • Modify the order.yaml configuration file
$ vim rpc/etc/order.yaml
Copy the code
  • Change the service listening address to 0.0.0.0:9002.EtcdService configuration,MysqlService configuration,CacheRedisService configuration
Name: order.rpc
ListenOn: 0.0. 0. 0: 9002

Etcd:
  Hosts:
  - etcd:2379
  Key: order.rpc

Mysql:
  DataSource: root:123456@tcp(mysql:3306)/mall? charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai

CacheRedis:
- Host: redis:6379
  Type: node
  Pass:
Copy the code

6.4.2 Adding order Model dependencies

  • addMysqlService configuration,CacheRedisInstantiation of the service configuration
$ vim rpc/internal/config/config.go
Copy the code
package config

import (
	"github.com/tal-tech/go-zero/core/stores/cache"
	"github.com/tal-tech/go-zero/zrpc"
)

type Config struct {
	zrpc.RpcServerConf

	Mysql struct {
		DataSource string
	}
    
	CacheRedis cache.CacheConf
}
Copy the code
  • Register the service Contextorder modelThe dependence of
$ vim rpc/internal/svc/servicecontext.go
Copy the code
package svc

import (
	"mall/service/order/model"
	"mall/service/order/rpc/internal/config"

	"github.com/tal-tech/go-zero/core/stores/sqlx"
)

type ServiceContext struct {
	Config config.Config

	OrderModel model.OrderModel
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.Mysql.DataSource)
	return &ServiceContext{
		Config:     c,
		OrderModel: model.NewOrderModel(conn, c.CacheRedis),
	}
}
Copy the code

6.4.3 Adding user RPC and Product RPC Dependencies

  • adduser rpc, product rpcService configuration
$ vim rpc/etc/order.yaml
Copy the code
Name: order.rpc
ListenOn: 0.0. 0. 0: 9002
Etcd:
  Hosts:
  - etcd:2379
  Key: order.rpc
  
.

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

ProductRpc:
  Etcd:
    Hosts:
    - etcd:2379
    Key: product.rpc
Copy the code
  • adduser rpc, product rpcInstantiation of the service configuration
$ vim rpc/internal/config/config.go
Copy the code
package config

import (
	"github.com/tal-tech/go-zero/core/stores/cache"
	"github.com/tal-tech/go-zero/zrpc"
)

type Config struct {
	zrpc.RpcServerConf

	Mysql struct {
		DataSource string
	}
    
	CacheRedis cache.CacheConf

	UserRpc    zrpc.RpcClientConf
	ProductRpc zrpc.RpcClientConf
}
Copy the code
  • Register the service Contextuser rpc, product rpcThe dependence of
$ vim rpc/internal/svc/servicecontext.go
Copy the code
package svc

import (
	"mall/service/order/model"
	"mall/service/order/rpc/internal/config"
	"mall/service/product/rpc/productclient"
	"mall/service/user/rpc/userclient"

	"github.com/tal-tech/go-zero/core/stores/sqlx"
	"github.com/tal-tech/go-zero/zrpc"
)

type ServiceContext struct {
	Config config.Config

	OrderModel model.OrderModel

	UserRpc    userclient.User
	ProductRpc productclient.Product
}

func NewServiceContext(c config.Config) *ServiceContext {
	conn := sqlx.NewMysql(c.Mysql.DataSource)
	return &ServiceContext{
		Config:     c,
		OrderModel: model.NewOrderModel(conn, c.CacheRedis),
		UserRpc:    userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
		ProductRpc: productclient.NewProduct(zrpc.MustNewClient(c.ProductRpc)),
	}
}
Copy the code

6.4.4 Adding an Order Create logic Create

$ vim rpc/internal/logic/createlogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/order"
	"mall/service/product/rpc/product"
	"mall/service/user/rpc/user"

	"github.com/tal-tech/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

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

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

func (l *CreateLogic) Create(in *order.CreateRequest) (*order.CreateResponse, error) {
	// Check whether the user exists
	_, err := l.svcCtx.UserRpc.UserInfo(l.ctx, &user.UserInfoRequest{
		Id: in.Uid,
	})
	iferr ! =nil {
		return nil, err
	}

	// Check whether the product exists
	productRes, err := l.svcCtx.ProductRpc.Detail(l.ctx, &product.DetailRequest{
		Id: in.Pid,
	})
	iferr ! =nil {
		return nil, err
	}
	// Determine whether the product inventory is sufficient
	if productRes.Stock <= 0 {
		return nil, status.Error(500."Insufficient product inventory")
	}

	newOrder := model.Order{
		Uid:    in.Uid,
		Pid:    in.Pid,
		Amount: in.Amount,
		Status: 0,}// Create an order
	res, err := l.svcCtx.OrderModel.Insert(&newOrder)
	iferr ! =nil {
		return nil, status.Error(500, err.Error())
	}

	newOrder.Id, err = res.LastInsertId()
	iferr ! =nil {
		return nil, status.Error(500, err.Error())
	}
        // Update product inventory
	_, err = l.svcCtx.ProductRpc.Update(l.ctx, &product.UpdateRequest{
		Id:     productRes.Id,
		Name:   productRes.Name,
		Desc:   productRes.Desc,
                Stock:  productRes.Stock - 1,
		Amount: productRes.Amount,
		Status: productRes.Status,
	})
	iferr ! =nil {
		return nil, err
	}

	return &order.CreateResponse{
		Id: newOrder.Id,
	}, nil
}
Copy the code

! Note: there is a data consistency problem in the product inventory update. In previous projects, we would use database transactions for this series of operations to ensure data consistency. But because we the “order” and “product” is divided into different services, in the actual projects they may have different database, so we want to consider the case of across service also can guarantee the consistency of the data, it involves the use of a distributed transaction, in later chapters we will introduce the use of a distributed transaction to modify the logic of this order.

6.4.5 Adding logical details of an order

$ vim rpc/internal/logic/detaillogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/order"

	"github.com/tal-tech/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

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

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

func (l *DetailLogic) Detail(in *order.DetailRequest) (*order.DetailResponse, error) {
	// Check whether the order exists
	res, err := l.svcCtx.OrderModel.FindOne(in.Id)
	iferr ! =nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100."Order does not exist")}return nil, status.Error(500, err.Error())
	}

	return &order.DetailResponse{
		Id:     res.Id,
		Uid:    res.Uid,
		Pid:    res.Pid,
		Amount: res.Amount,
		Status: res.Status,
	}, nil
}
Copy the code

6.4.6 Adding order Update Logic Update

$ vim rpc/internal/logic/updatelogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/order"

	"github.com/tal-tech/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

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

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

func (l *UpdateLogic) Update(in *order.UpdateRequest) (*order.UpdateResponse, error) {
	// Check whether the order exists
	res, err := l.svcCtx.OrderModel.FindOne(in.Id)
	iferr ! =nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100."Order does not exist")}return nil, status.Error(500, err.Error())
	}

	ifin.Uid ! =0 {
		res.Uid = in.Uid
	}
	ifin.Pid ! =0 {
		res.Pid = in.Pid
	}
	ifin.Amount ! =0 {
		res.Amount = in.Amount
	}
	ifin.Status ! =0 {
		res.Status = in.Status
	}

	err = l.svcCtx.OrderModel.Update(res)
	iferr ! =nil {
		return nil, status.Error(500, err.Error())
	}

	return &order.UpdateResponse{}, nil
}
Copy the code

6.4.7 Adding the Logical Remove for Deleting an Order

$ vim rpc/internal/logic/removelogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/order"

	"github.com/tal-tech/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

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

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

func (l *RemoveLogic) Remove(in *order.RemoveRequest) (*order.RemoveResponse, error) {
	// Check whether the order exists
	res, err := l.svcCtx.OrderModel.FindOne(in.Id)
	iferr ! =nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100."Order does not exist")}return nil, status.Error(500, err.Error())
	}

	err = l.svcCtx.OrderModel.Delete(res.Id)
	iferr ! =nil {
		return nil, status.Error(500, err.Error())
	}

	return &order.RemoveResponse{}, nil
}
Copy the code

6.4.8 Adding a Logical Order List

  • Add according touidQuery all orders of the userOrderModelmethodsFindAllByUid
$ vim model/ordermodel.go
Copy the code
package model

......

type (
	OrderModel interface {
		Insert(data *Order) (sql.Result, error)
		FindOne(id int64) (*Order, error)
		FindAllByUid(uid int64) ([]*Order, error)
		Update(data *Order) error
		Delete(id int64) error } ...... ) .func (m *defaultOrderModel) FindAllByUid(uid int64) ([]*Order, error) {
	var resp []*Order

	query := fmt.Sprintf("select %s from %s where `uid` = ?", orderRows, m.table)
	err := m.QueryRowsNoCache(&resp, query, uid)

	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

......
Copy the code
  • Add order list logic
$ vim rpc/internal/logic/listlogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/order"
	"mall/service/user/rpc/user"

	"github.com/tal-tech/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

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

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

func (l *ListLogic) List(in *order.ListRequest) (*order.ListResponse, error) {
	// Check whether the user exists
	_, err := l.svcCtx.UserRpc.UserInfo(l.ctx, &user.UserInfoRequest{
		Id: in.Uid,
	})
	iferr ! =nil {
		return nil, err
	}

	// Check whether the order exists
	list, err := l.svcCtx.OrderModel.FindAllByUid(in.Uid)
	iferr ! =nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100."Order does not exist")}return nil, status.Error(500, err.Error())
	}

	orderList := make([]*order.DetailResponse, 0)
	for _, item := range list {
		orderList = append(orderList, &order.DetailResponse{
			Id:     item.Id,
			Uid:    item.Uid,
			Pid:    item.Pid,
			Amount: item.Amount,
			Status: item.Status,
		})
	}

	return &order.ListResponse{
		Data: orderList,
	}, nil
}
Copy the code

6.4.9 Adding the Order Payment logic Paid

$ vim rpc/internal/logic/paidlogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/model"
	"mall/service/order/rpc/internal/svc"
	"mall/service/order/rpc/order"

	"github.com/tal-tech/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

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

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

func (l *PaidLogic) Paid(in *order.PaidRequest) (*order.PaidResponse, error) {
	// Check whether the order exists
	res, err := l.svcCtx.OrderModel.FindOne(in.Id)
	iferr ! =nil {
		if err == model.ErrNotFound {
			return nil, status.Error(100."Order does not exist")}return nil, status.Error(500, err.Error())
	}

	res.Status = 1

	err = l.svcCtx.OrderModel.Update(res)
	iferr ! =nil {
		return nil, status.Error(500, err.Error())
	}

	return &order.PaidResponse{}, nil
}
Copy the code

6.5 Writing the ORDER API Service

6.5.1 Modifying the Configuration File

  • Modify the order.yaml configuration file
$ vim api/etc/order.yaml
Copy the code
  • Change the service address to 0.0.0.0:8002.MysqlService configuration,CacheRedisService configuration,AuthVerify the configuration
Name: Order
Host: 0.0. 0. 0
Port: 8002

Mysql:
  DataSource: root:123456@tcp(mysql:3306)/mall? charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai

CacheRedis:
- Host: redis:6379
  Type: node
  Pass:

Auth:
  AccessSecret: uOvKLmVfztaXGpNYd4Z0I1SiT7MweJhl
  AccessExpire: 86400
Copy the code

6.5.2 Adding an ORDER RPC Dependency

  • addorder rpcService configuration
$ vim api/etc/order.yaml
Copy the code
Name: Order
Host: 0.0. 0. 0
Port: 8002

.

OrderRpc:
  Etcd:
    Hosts:
    - etcd:2379
    Key: order.rpc
Copy the code
  • addorder rpcInstantiation of the service configuration
$ vim api/internal/config/config.go
Copy the code
package config

import (
	"github.com/tal-tech/go-zero/rest"
	"github.com/tal-tech/go-zero/zrpc"
)

type Config struct {
	rest.RestConf

	Auth struct {
		AccessSecret string
		AccessExpire int64
	}

	OrderRpc zrpc.RpcClientConf
}
Copy the code
  • Register the service Contextuser rpcThe dependence of
$ vim api/internal/svc/servicecontext.go
Copy the code
package svc

import (
	"mall/service/order/api/internal/config"
	"mall/service/order/rpc/orderclient"

	"github.com/tal-tech/go-zero/zrpc"
)

type ServiceContext struct {
	Config config.Config

	OrderRpc orderclient.Order
}

func NewServiceContext(c config.Config) *ServiceContext {
	return &ServiceContext{
		Config:   c,
		OrderRpc: orderclient.NewOrder(zrpc.MustNewClient(c.OrderRpc)),
	}
}
Copy the code

6.5.3 Adding an Order Create logic Create

$ vim api/internal/logic/createlogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/api/internal/svc"
	"mall/service/order/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/tal-tech/go-zero/core/logx"
)

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

func NewCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) CreateLogic {
	return CreateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateLogic) Create(req types.CreateRequest) (resp *types.CreateResponse, err error) {
	res, err := l.svcCtx.OrderRpc.Create(l.ctx, &orderclient.CreateRequest{
		Uid:    req.Uid,
		Pid:    req.Pid,
		Amount: req.Amount,
		Status: req.Status,
	})
	iferr ! =nil {
		return nil, err
	}

	return &types.CreateResponse{
		Id: res.Id,
	}, nil
}
Copy the code

6.5.4 Adding logical details of an order

$ vim api/internal/logic/detaillogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/api/internal/svc"
	"mall/service/order/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/tal-tech/go-zero/core/logx"
)

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

func NewDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) DetailLogic {
	return DetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DetailLogic) Detail(req types.DetailRequest) (resp *types.DetailResponse, err error) {
	res, err := l.svcCtx.OrderRpc.Detail(l.ctx, &orderclient.DetailRequest{
		Id: req.Id,
	})
	iferr ! =nil {
		return nil, err
	}

	return &types.DetailResponse{
		Id:     res.Id,
		Uid:    res.Uid,
		Pid:    res.Pid,
		Amount: res.Amount,
		Status: res.Status,
	}, nil
}
Copy the code

6.5.5 Adding order Update Logic Update

$ vim api/internal/logic/updatelogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/api/internal/svc"
	"mall/service/order/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/tal-tech/go-zero/core/logx"
)

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

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) UpdateLogic {
	return UpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateLogic) Update(req types.UpdateRequest) (resp *types.UpdateResponse, err error) {
	_, err = l.svcCtx.OrderRpc.Update(l.ctx, &orderclient.UpdateRequest{
		Id:     req.Id,
		Uid:    req.Uid,
		Pid:    req.Pid,
		Amount: req.Amount,
		Status: req.Status,
	})
	iferr ! =nil {
		return nil, err
	}

	return &types.UpdateResponse{}, nil
}
Copy the code

6.5.6 Adding the Logical Remove for Deleting an Order

$ vim api/internal/logic/removelogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/api/internal/svc"
	"mall/service/order/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/tal-tech/go-zero/core/logx"
)

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

func NewRemoveLogic(ctx context.Context, svcCtx *svc.ServiceContext) RemoveLogic {
	return RemoveLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RemoveLogic) Remove(req types.RemoveRequest) (resp *types.RemoveResponse, err error) {
	_, err = l.svcCtx.OrderRpc.Remove(l.ctx, &orderclient.RemoveRequest{
		Id: req.Id,
	})
	iferr ! =nil {
		return nil, err
	}

	return &types.RemoveResponse{}, nil
}
Copy the code

6.5.7 Adding a Logical Order List

$ vim api/internal/logic/listlogic.go
Copy the code
package logic

import (
	"context"

	"mall/service/order/api/internal/svc"
	"mall/service/order/api/internal/types"
	"mall/service/order/rpc/orderclient"

	"github.com/tal-tech/go-zero/core/logx"
)

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

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) ListLogic {
	return ListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListLogic) List(req types.ListRequest) (resp []*types.ListResponse, err error) {
	res, err := l.svcCtx.OrderRpc.List(l.ctx, &orderclient.ListRequest{
		Uid: req.Uid,
	})
	iferr ! =nil {
		return nil, err
	}

	orderList := make([]*types.ListResponse, 0)
	for _, item := range res.Data {
		orderList = append(orderList, &types.ListResponse{
			Id:     item.Id,
			Uid:    item.Uid,
			Pid:    item.Pid,
			Amount: item.Amount,
			Status: item.Status,
		})
	}

	return orderList, nil
}
Copy the code

6.6 Starting the ORDER RPC Service

! Tip: To start the service, you need to start it in the Golang container

$ cdMall /service/order/ RPC $go run order.go -f etc/order.yaml Starting RPC server at 127.0.0.1:9002...Copy the code

6.7 Starting the Order API Service

! Tip: To start the service, you need to start it in the Golang container

$ cdMall /service/order/ API $go run order.go -f etc/order.yaml Starting server at 0.0.0.00:8002...Copy the code

Project address: Github

Previous article “Go-Zero Combat: Let micro Services Go — 5 Products”

The next article is “Go-Zero: Make Micro services Go — 7 Pay”.