Update with Tx
This commit is contained in:
parent
20d1ef5f2a
commit
46b30277ef
@ -18,6 +18,7 @@ type InProgressOrderRepository interface {
|
|||||||
CreateOrderItems(ctx mycontext.Context, orderID int64, items []entity.OrderItem, tx *gorm.DB) error
|
CreateOrderItems(ctx mycontext.Context, orderID int64, items []entity.OrderItem, tx *gorm.DB) error
|
||||||
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error)
|
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error)
|
||||||
FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error)
|
FindByIDAndPartnerID(ctx mycontext.Context, id int64, partnerID int64) (*entity.Order, error)
|
||||||
|
FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type inprogressOrderRepository struct {
|
type inprogressOrderRepository struct {
|
||||||
@ -43,6 +44,21 @@ func (r *inprogressOrderRepository) FindByID(ctx mycontext.Context, id int64) (*
|
|||||||
return order, nil
|
return order, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *inprogressOrderRepository) FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) {
|
||||||
|
var orderDB models.OrderDB
|
||||||
|
|
||||||
|
if err := tx.Preload("OrderItems").First(&orderDB, id).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, errors.New("order not found")
|
||||||
|
}
|
||||||
|
return nil, errors.Wrap(err, "failed to find order")
|
||||||
|
}
|
||||||
|
|
||||||
|
order := r.toDomainOrderModel(&orderDB)
|
||||||
|
|
||||||
|
return order, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *inprogressOrderRepository) CreateOrder(ctx mycontext.Context, order *entity.Order, tx *gorm.DB) (*entity.Order, error) {
|
func (r *inprogressOrderRepository) CreateOrder(ctx mycontext.Context, order *entity.Order, tx *gorm.DB) (*entity.Order, error) {
|
||||||
orderDB := r.toOrderDBModel(order)
|
orderDB := r.toOrderDBModel(order)
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,7 @@ type OrderRepository interface {
|
|||||||
UpdateOrderItem(ctx mycontext.Context, orderItemID int64, quantity int) error
|
UpdateOrderItem(ctx mycontext.Context, orderItemID int64, quantity int) error
|
||||||
UpdateOrderTotals(ctx mycontext.Context, orderID int64, amount, tax, total float64) error
|
UpdateOrderTotals(ctx mycontext.Context, orderID int64, amount, tax, total float64) error
|
||||||
UpdateOrderTotalsWithTx(ctx mycontext.Context, trx *gorm.DB, orderID int64, amount, tax, total float64) error
|
UpdateOrderTotalsWithTx(ctx mycontext.Context, trx *gorm.DB, orderID int64, amount, tax, total float64) error
|
||||||
|
FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type orderRepository struct {
|
type orderRepository struct {
|
||||||
@ -123,6 +124,20 @@ func (r *orderRepository) FindByID(ctx mycontext.Context, id int64) (*entity.Ord
|
|||||||
return order, nil
|
return order, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *orderRepository) FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error) {
|
||||||
|
var orderDB models.OrderDB
|
||||||
|
|
||||||
|
if err := tx.Preload("OrderItems").First(&orderDB, id).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, errors.New("order not found")
|
||||||
|
}
|
||||||
|
return nil, errors.Wrap(err, "failed to find order in transaction")
|
||||||
|
}
|
||||||
|
|
||||||
|
order := r.toDomainOrderModel(&orderDB)
|
||||||
|
return order, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *orderRepository) CreateInquiry(ctx mycontext.Context, inquiry *entity.OrderInquiry) (*entity.OrderInquiry, error) {
|
func (r *orderRepository) CreateInquiry(ctx mycontext.Context, inquiry *entity.OrderInquiry) (*entity.OrderInquiry, error) {
|
||||||
inquiryDB := r.toOrderInquiryDBModel(inquiry)
|
inquiryDB := r.toOrderInquiryDBModel(inquiry)
|
||||||
inquiryItems := make([]models.InquiryItemDB, 0, len(inquiry.OrderItems))
|
inquiryItems := make([]models.InquiryItemDB, 0, len(inquiry.OrderItems))
|
||||||
|
|||||||
@ -25,6 +25,7 @@ type InProgressOrderService interface {
|
|||||||
|
|
||||||
type OrderRepository interface {
|
type OrderRepository interface {
|
||||||
FindByID(ctx mycontext.Context, id int64) (*entity.Order, error)
|
FindByID(ctx mycontext.Context, id int64) (*entity.Order, error)
|
||||||
|
FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error)
|
||||||
CreateOrder(ctx mycontext.Context, order *entity.Order, tx *gorm.DB) (*entity.Order, error)
|
CreateOrder(ctx mycontext.Context, order *entity.Order, tx *gorm.DB) (*entity.Order, error)
|
||||||
CreateOrderItems(ctx mycontext.Context, orderID int64, items []entity.OrderItem, tx *gorm.DB) error
|
CreateOrderItems(ctx mycontext.Context, orderID int64, items []entity.OrderItem, tx *gorm.DB) error
|
||||||
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error)
|
GetListByPartnerID(ctx mycontext.Context, partnerID int64, limit, offset int, status string) ([]*entity.Order, error)
|
||||||
@ -147,7 +148,7 @@ func (s *inProgressOrderSvc) AddItems(ctx mycontext.Context, orderID int64, newI
|
|||||||
return nil, errors.Wrap(err, "failed to add order items")
|
return nil, errors.Wrap(err, "failed to add order items")
|
||||||
}
|
}
|
||||||
|
|
||||||
updatedOrder, err := s.repo.FindByID(ctx, existingOrder.ID)
|
updatedOrder, err := s.repo.FindByIDWithTx(ctx, existingOrder.ID, tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.trx.Rollback(tx)
|
s.trx.Rollback(tx)
|
||||||
return nil, errors.Wrap(err, "failed to fetch updated order")
|
return nil, errors.Wrap(err, "failed to fetch updated order")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user