pg_create_order_service.go 1.4 KB
package domainService

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	coreDomain "github.com/linmadan/egglib-go/core/domain"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/infrastructure/repository"
)

type CreateOrderService struct {
	coreDomain.BaseEventPublisher
	transactionContext *pgTransaction.TransactionContext
}

func (service *CreateOrderService) Create(newOrder *domain.Order) (*domain.Order, error) {
	var orderRepository domain.OrderRepository
	if value, err := repository.NewOrderRepository(service.transactionContext); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		orderRepository = value
	}
	if err := newOrder.Update(nil); err != nil {
		return nil, err
	}
	if order, err := orderRepository.Save(newOrder); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		return order, nil
	}
	return nil, nil
}

func NewCreateOrderService(transactionContext *pgTransaction.TransactionContext) (*CreateOrderService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &CreateOrderService{
			transactionContext: transactionContext,
		}, nil
	}
}