Update with Tx

This commit is contained in:
aditya.siregar 2025-06-28 16:34:19 +07:00
parent 20d1ef5f2a
commit 46b30277ef
3 changed files with 33 additions and 1 deletions

View File

@ -18,6 +18,7 @@ type InProgressOrderRepository interface {
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)
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 {
@ -43,6 +44,21 @@ func (r *inprogressOrderRepository) FindByID(ctx mycontext.Context, id int64) (*
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) {
orderDB := r.toOrderDBModel(order)

View File

@ -34,6 +34,7 @@ type OrderRepository interface {
UpdateOrderItem(ctx mycontext.Context, orderItemID int64, quantity int) 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
FindByIDWithTx(ctx mycontext.Context, id int64, tx *gorm.DB) (*entity.Order, error)
}
type orderRepository struct {
@ -123,6 +124,20 @@ func (r *orderRepository) FindByID(ctx mycontext.Context, id int64) (*entity.Ord
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) {
inquiryDB := r.toOrderInquiryDBModel(inquiry)
inquiryItems := make([]models.InquiryItemDB, 0, len(inquiry.OrderItems))

View File

@ -25,6 +25,7 @@ type InProgressOrderService interface {
type OrderRepository interface {
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)
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)
@ -147,7 +148,7 @@ func (s *inProgressOrderSvc) AddItems(ctx mycontext.Context, orderID int64, newI
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 {
s.trx.Rollback(tx)
return nil, errors.Wrap(err, "failed to fetch updated order")