refactor: 优化交易类型判断逻辑,使用switch语句增强可读性
This commit is contained in:
@@ -70,7 +70,11 @@ func (s *order) GetUserOrder(ctx context.Context, userID int64, id string) (*use
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func (s *order) Create(ctx context.Context, userID int64, form *transaction_dto.OrderCreateForm) (*transaction_dto.OrderCreateResponse, error) {
|
||||
func (s *order) Create(
|
||||
ctx context.Context,
|
||||
userID int64,
|
||||
form *transaction_dto.OrderCreateForm,
|
||||
) (*transaction_dto.OrderCreateResponse, error) {
|
||||
if userID == 0 {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
@@ -166,7 +170,12 @@ func (s *order) Create(ctx context.Context, userID int64, form *transaction_dto.
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *order) Pay(ctx context.Context, userID int64, id string, form *transaction_dto.OrderPayForm) (*transaction_dto.OrderPayResponse, error) {
|
||||
func (s *order) Pay(
|
||||
ctx context.Context,
|
||||
userID int64,
|
||||
id string,
|
||||
form *transaction_dto.OrderPayForm,
|
||||
) (*transaction_dto.OrderPayResponse, error) {
|
||||
if userID == 0 {
|
||||
return nil, errorx.ErrUnauthorized
|
||||
}
|
||||
@@ -174,7 +183,9 @@ func (s *order) Pay(ctx context.Context, userID int64, id string, form *transact
|
||||
oid := cast.ToInt64(id)
|
||||
|
||||
// Fetch Order
|
||||
o, err := models.OrderQuery.WithContext(ctx).Where(models.OrderQuery.ID.Eq(oid), models.OrderQuery.UserID.Eq(uid)).First()
|
||||
o, err := models.OrderQuery.WithContext(ctx).
|
||||
Where(models.OrderQuery.ID.Eq(oid), models.OrderQuery.UserID.Eq(uid)).
|
||||
First()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrRecordNotFound
|
||||
}
|
||||
@@ -253,7 +264,9 @@ func (s *order) settleOrder(ctx context.Context, o *models.Order, method, extern
|
||||
items, _ := tx.OrderItem.WithContext(ctx).Where(tx.OrderItem.OrderID.Eq(o.ID)).Find()
|
||||
for _, item := range items {
|
||||
// Check if access already exists (idempotency)
|
||||
exists, _ := tx.ContentAccess.WithContext(ctx).Where(tx.ContentAccess.UserID.Eq(o.UserID), tx.ContentAccess.ContentID.Eq(item.ContentID)).Exists()
|
||||
exists, _ := tx.ContentAccess.WithContext(ctx).
|
||||
Where(tx.ContentAccess.UserID.Eq(o.UserID), tx.ContentAccess.ContentID.Eq(item.ContentID)).
|
||||
Exists()
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user