作者 陈志颖

fix:调整错误

package command
import (
"fmt"
"time"
"github.com/beego/beego/v2/core/validation"
)
type CancelCommand struct {
// 订单编号
OrderNo string `json:"orderNo" valid:"Required"`
// 公司id
CompanyId int64 `json:"companyId" valid:"Required"`
// 是否是公司负责人
IsPrincipal bool `json:"isPrincipal" valid:"Required"`
// 统一用户id
Uid int64 `json:"uid" valid:"Required"`
// 用户账号
UserAccount string `json:"userAccount" valid:"Required"`
// 用户头像URL
UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
// 用户名称
UserName string `json:"userName" valid:"Required"`
// 邮件地址
Email string `json:"email" valid:"Required"`
// 性别
Gender int `json:"gender" valid:"Required"`
// 入职时间
EntryTime time.Time `json:"entryTime" valid:"Required"`
// 分机
Extension string `json:"extension" valid:"Required"`
// 工作地
Workplace string `json:"workplace" valid:"Required"`
// 私人电话
PrivateNumber string `json:"privateNumber" valid:"Required"`
// 工号
JobNumber string `json:"jobNumber" valid:"Required"`
}
func (cancelCommand *CancelCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (cancelCommand *CancelCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(cancelCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type CreateOrderCommand struct {
// 订单编号
OrderNo string `json:"orderNo" valid:"Required"`
// 公司Id
CompanyId int64 `json:"companyId" valid:"Required"`
// 买家名称
BuyerName string `json:"buyerName,omitempty"`
// 联系信息
ContactInfo string `json:"contactInfo,omitempty"`
// 发货地址
ShippingAddress string `json:"shippingAddress,omitempty"`
// 订单商品总数
OrderNum int64 `json:"orderNum" valid:"Required"`
// 订单总价
TotalPrice float64 `json:"totalPrice" valid:"Required"`
}
func (createOrderCommand *CreateOrderCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (createOrderCommand *CreateOrderCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(createOrderCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type DrawDividendsCommand struct {
// 参与分红的合伙人
PartnerId int64 `json:"partnerId" valid:"Required"`
// 进行分红的订单编号
OrderNo string `json:"orderNo" valid:"Required"`
}
func (drawDividendsCommand *DrawDividendsCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (drawDividendsCommand *DrawDividendsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(drawDividendsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"time"
"github.com/beego/beego/v2/core/validation"
)
type ReceivingCommand struct {
// 订单号
OrderNo string `json:"orderNo" valid:"Required"`
// 是否是公司负责人
IsPrincipal bool `json:"isPrincipal" valid:"Required"`
// 统一用户id
Uid int64 `json:"uid" valid:"Required"`
// 用户账号
UserAccount string `json:"userAccount" valid:"Required"`
// 用户头像URL
UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
// 用户名称
UserName string `json:"userName" valid:"Required"`
// 邮件地址
Email string `json:"email" valid:"Required"`
// 性别
Gender int `json:"gender" valid:"Required"`
// 入职时间
EntryTime time.Time `json:"entryTime" valid:"Required"`
// 分机
Extension string `json:"extension" valid:"Required"`
// 工作地
Workplace string `json:"workplace" valid:"Required"`
// 私人电话
PrivateNumber string `json:"privateNumber" valid:"Required"`
// 工号
JobNumber string `json:"jobNumber" valid:"Required"`
// 公司id
CompanyId int64 `json:"companyId" valid:"Required"`
}
func (receivingCommand *ReceivingCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (receivingCommand *ReceivingCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(receivingCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type RemoveOrderCommand struct {
// 订单id
OrderId int64 `json:"orderId" valid:"Required"`
}
func (removeOrderCommand *RemoveOrderCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (removeOrderCommand *RemoveOrderCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(removeOrderCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"time"
"github.com/beego/beego/v2/core/validation"
)
type ReturnCommand struct {
// 订单号
OrderNo string `json:"orderNo" valid:"Required"`
// 是否是公司负责人
IsPrincipal bool `json:"isPrincipal" valid:"Required"`
// 统一用户id
Uid int64 `json:"uid" valid:"Required"`
// 用户账号
UserAccount string `json:"userAccount" valid:"Required"`
// 用户头像URL
UserAvatarUrl string `json:"userAvatarUrl" valid:"Required"`
// 用户名称
UserName string `json:"userName" valid:"Required"`
// 邮件地址
Email string `json:"email" valid:"Required"`
// 性别
Gender int `json:"gender" valid:"Required"`
// 入职时间
EntryTime time.Time `json:"entryTime" valid:"Required"`
// 分机
Extension string `json:"extension" valid:"Required"`
// 工作地
Workplace string `json:"workplace" valid:"Required"`
// 私人电话
PrivateNumber string `json:"privateNumber" valid:"Required"`
// 工号
JobNumber string `json:"jobNumber" valid:"Required"`
// 公司id
CompanyId int64 `json:"companyId" valid:"Required"`
}
func (returnCommand *ReturnCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (returnCommand *ReturnCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(returnCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type ShippingCommand struct {
// 发货单号
DeliveryCode string `json:"deliveryCode" valid:"Required"`
// 订单编号
OrderNo string `json:"orderNo" valid:"Required"`
// 订单数量
OrderNum int64 `json:"orderNum" valid:"Required"`
}
func (shippingCommand *ShippingCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (shippingCommand *ShippingCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(shippingCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package command
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type UpdateOrderCommand struct {
// 订单id
OrderId int64 `json:"orderId" valid:"Required"`
}
func (updateOrderCommand *UpdateOrderCommand) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (updateOrderCommand *UpdateOrderCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(updateOrderCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type GetOrderQuery struct {
// 订单id
OrderId int64 `json:"orderId" valid:"Required"`
}
func (getOrderQuery *GetOrderQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (getOrderQuery *GetOrderQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(getOrderQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package query
import (
"fmt"
"github.com/beego/beego/v2/core/validation"
)
type ListOrderQuery struct {
// 查询偏离量
Offset int `json:"offset" valid:"Required"`
// 查询限制
Limit int `json:"limit" valid:"Required"`
}
func (listOrderQuery *ListOrderQuery) Valid(validation *validation.Validation) {
validation.SetError("CustomValid", "未实现的自定义认证")
}
func (listOrderQuery *ListOrderQuery) ValidateQuery() error {
valid := validation.Validation{}
b, err := valid.Valid(listOrderQuery)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)
// 订单基础服务
type OrderService struct {
}
// 取消订单服务
func (orderService *OrderService) Cancel(cancelCommand *command.CancelCommand) (interface{}, error) {
if err := cancelCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 创建订单增删改查
func (orderService *OrderService) CreateOrder(createOrderCommand *command.CreateOrderCommand) (interface{}, error) {
if err := createOrderCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
newOrder := &domain.Order{
OrderNo: createOrderCommand.OrderNo,
CompanyId: createOrderCommand.CompanyId,
Buyer: &domain.BuyerInfo{
BuyerName: createOrderCommand.BuyerName,
},
TotalPrice: createOrderCommand.TotalPrice,
}
var orderRepository domain.OrderRepository
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
if order, err := orderRepository.Save(newOrder); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return order, nil
}
}
// 订单分红服务
func (orderService *OrderService) DarwDividends(darwDividendsCommand *command.DrawDividendsCommand) (interface{}, error) {
if err := darwDividendsCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 返回订单增删改查
func (orderService *OrderService) GetOrder(getOrderQuery *query.GetOrderQuery) (interface{}, error) {
if err := getOrderQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var orderRepository domain.OrderRepository
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err := orderRepository.FindOne(map[string]interface{}{"orderId": getOrderQuery.OrderId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if order == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getOrderQuery.OrderId)))
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return order, nil
}
}
// 返回订单增删改查列表
func (orderService *OrderService) ListOrder(listOrderQuery *query.ListOrderQuery) (interface{}, error) {
if err := listOrderQuery.ValidateQuery(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var orderRepository domain.OrderRepository
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
if count, orders, err := orderRepository.Find(tool_funs.SimpleStructToMap(listOrderQuery)); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return map[string]interface{}{
"count": count,
"orders": orders,
}, nil
}
}
// 订单收货服务
func (orderService *OrderService) Receiving(receivingCommand *command.ReceivingCommand) (interface{}, error) {
if err := receivingCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 移除订单增删改查
func (orderService *OrderService) RemoveOrder(removeOrderCommand *command.RemoveOrderCommand) (interface{}, error) {
if err := removeOrderCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var orderRepository domain.OrderRepository
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err := orderRepository.FindOne(map[string]interface{}{"orderId": removeOrderCommand.OrderId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if order == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeOrderCommand.OrderId)))
}
if order, err := orderRepository.Remove(order); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return order, nil
}
}
// 订单退货服务
func (orderService *OrderService) Return(returnCommand *command.ReturnCommand) (interface{}, error) {
if err := returnCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 订单发货服务
func (orderService *OrderService) Shipping(shippingCommand *command.ShippingCommand) (interface{}, error) {
if err := shippingCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 更新订单增删改查
func (orderService *OrderService) UpdateOrder(updateOrderCommand *command.UpdateOrderCommand) (interface{}, error) {
if err := updateOrderCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
var orderRepository domain.OrderRepository
if value, err := factory.CreateOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
orderRepository = value
}
order, err := orderRepository.FindOne(map[string]interface{}{"orderId": updateOrderCommand.OrderId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if order == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateOrderCommand.OrderId)))
}
if err := order.Update(tool_funs.SimpleStructToMap(updateOrderCommand)); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
if order, err := orderRepository.Save(order); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return order, nil
}
}
func NewOrderService(options map[string]interface{}) *OrderService {
newOrderService := &OrderService{}
return newOrderService
}
... ...
package controllers
import (
"github.com/linmadan/egglib-go/web/beego"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/query"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/order/service"
)
type OrderController struct {
beego.BaseController
}
func (controller *OrderController) CreateOrder() {
orderService := service.NewOrderService(nil)
createOrderCommand := &command.CreateOrderCommand{}
controller.Unmarshal(createOrderCommand)
data, err := orderService.CreateOrder(createOrderCommand)
controller.Response(data, err)
}
func (controller *OrderController) UpdateOrder() {
orderService := service.NewOrderService(nil)
updateOrderCommand := &command.UpdateOrderCommand{}
controller.Unmarshal(updateOrderCommand)
orderId, _ := controller.GetInt64(":orderId")
updateOrderCommand.OrderId = orderId
data, err := orderService.UpdateOrder(updateOrderCommand)
controller.Response(data, err)
}
func (controller *OrderController) GetOrder() {
orderService := service.NewOrderService(nil)
getOrderQuery := &query.GetOrderQuery{}
orderId, _ := controller.GetInt64(":orderId")
getOrderQuery.OrderId = orderId
data, err := orderService.GetOrder(getOrderQuery)
controller.Response(data, err)
}
func (controller *OrderController) RemoveOrder() {
orderService := service.NewOrderService(nil)
removeOrderCommand := &command.RemoveOrderCommand{}
controller.Unmarshal(removeOrderCommand)
orderId, _ := controller.GetInt64(":orderId")
removeOrderCommand.OrderId = orderId
data, err := orderService.RemoveOrder(removeOrderCommand)
controller.Response(data, err)
}
func (controller *OrderController) ListOrder() {
orderService := service.NewOrderService(nil)
listOrderQuery := &query.ListOrderQuery{}
offset, _ := controller.GetInt("offset")
listOrderQuery.Offset = offset
limit, _ := controller.GetInt("limit")
listOrderQuery.Limit = limit
data, err := orderService.ListOrder(listOrderQuery)
controller.Response(data, err)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego/controllers"
)
func init() {
web.Router("/orders/", &controllers.OrderController{}, "Post:CreateOrder")
web.Router("/orders/:orderId", &controllers.OrderController{}, "Put:UpdateOrder")
web.Router("/orders/:orderId", &controllers.OrderController{}, "Get:GetOrder")
web.Router("/orders/:orderId", &controllers.OrderController{}, "Delete:RemoveOrder")
web.Router("/orders/", &controllers.OrderController{}, "Get:ListOrder")
}
... ...
package order
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("创建订单增删改查", func() {
Describe("提交数据创建订单增删改查", func() {
Context("提交正确的新订单实体数据", func() {
It("返回订单实体数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"orderNo": "string",
"companyId": "int64",
"buyerName": "string",
"contactInfo": "string",
"shippingAddress": "string",
"orderNum": "int64",
"totalPrice": "float64",
}
httpExpect.POST("/orders/").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("orderId").ValueNotEqual("orderId", BeZero())
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM orders WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package order
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("返回订单增删改查", func() {
var orderId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orderId),
"INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
"testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据orderId参数返回订单实体", func() {
Context("传入有效的orderId", func() {
It("返回订单实体数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/orders/{orderId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM orders WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package order
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("返回订单增删改查列表", func() {
var orderId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orderId),
"INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
"testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数返回订单实体列表", func() {
Context("传入有效的参数", func() {
It("返回订单实体数据列表", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.GET("/orders/").
WithQuery("offset", "int").
WithQuery("limit", "int").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("count").ValueEqual("count", 1).
ContainsKey("orders").Value("orders").Array()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM orders WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package order
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/beego/beego/v2/server/web"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/port/beego"
)
func TestOrder(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Beego Port Order Correlations Test Case Suite")
}
var handler http.Handler
var server *httptest.Server
var _ = BeforeSuite(func() {
handler = web.BeeApp.Handlers
server = httptest.NewServer(handler)
})
var _ = AfterSuite(func() {
server.Close()
})
... ...
package order
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("移除订单增删改查", func() {
var orderId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orderId),
"INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
"testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("根据参数移除订单增删改查", func() {
Context("传入有效的orderId", func() {
It("返回被移除订单实体的数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
httpExpect.DELETE("/orders/{orderId}").
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM orders WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...
package order
import (
"net/http"
"github.com/gavv/httpexpect"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/pg"
)
var _ = Describe("更新订单增删改查", func() {
var orderId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&orderId),
"INSERT INTO orders (order_id, buyer, company_id, partner_id, delivery_code, is_disable, order_no, order_detail, order_dividend, order_dividend_status, order_goods, order_source, order_type, order_status, total_price, region_info, remarks, create_at, update_at, delete_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING order_id",
"testOrderId", "testBuyer", "testCompanyId", "testPartnerId", "testDeliveryCode", "testIsDisable", "testOrderNo", "testOrderDetail", "testOrderDividend", "testOrderDividendStatus", "testOrderGoods", "testOrderSource", "testOrderType", "testOrderStatus", "testTotalPrice", "testRegionInfo", "testRemarks", "testCreateAt", "testUpdateAt", "testDeleteAt")
Expect(err).NotTo(HaveOccurred())
})
Describe("提交数据更新订单增删改查", func() {
Context("提交正确的订单实体数据", func() {
It("返回更新后的订单实体数据", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{}
httpExpect.PUT("/orders/{orderId}").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object().
ContainsKey("orderId").ValueEqual("orderId", orderId)
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM orders WHERE true")
Expect(err).NotTo(HaveOccurred())
})
})
... ...