fix(supply-api): 修复编译错误和测试问题
- 添加 ErrNotFound 和 ErrConcurrencyConflict 错误定义 - 修复 pgx.NullTime 替换为 *time.Time - 修复 db.go 事务类型 (pgx.Tx vs pgxpool.Tx) - 移除未使用的导入和变量 - 修复 NewSupplyAPI 调用参数 - 修复中间件链路 handler 类型问题 - 修复适配器类型引用 (storage.InMemoryAccountStore 等) - 所有测试通过 Test: go test ./...
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"lijiaoqiao/supply-api/internal/config"
|
||||
)
|
||||
@@ -69,7 +70,7 @@ type Transaction interface {
|
||||
}
|
||||
|
||||
type txWrapper struct {
|
||||
tx pgxpool.Tx
|
||||
tx pgx.Tx
|
||||
}
|
||||
|
||||
func (t *txWrapper) Commit(ctx context.Context) error {
|
||||
|
||||
12
supply-api/internal/repository/errors.go
Normal file
12
supply-api/internal/repository/errors.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package repository
|
||||
|
||||
import "errors"
|
||||
|
||||
// 仓储层错误定义
|
||||
var (
|
||||
// ErrNotFound 资源不存在
|
||||
ErrNotFound = errors.New("resource not found")
|
||||
|
||||
// ErrConcurrencyConflict 并发冲突(乐观锁失败)
|
||||
ErrConcurrencyConflict = errors.New("concurrency conflict: resource was modified by another transaction")
|
||||
)
|
||||
@@ -16,8 +16,8 @@ type IdempotencyStatus string
|
||||
|
||||
const (
|
||||
IdempotencyStatusProcessing IdempotencyStatus = "processing"
|
||||
IdempotencyStatusSucceeded IdempotencyStatus = "succeeded"
|
||||
IdempotencyStatusFailed IdempotencyStatus = "failed"
|
||||
IdempotencyStatusSucceeded IdempotencyStatus = "succeeded"
|
||||
IdempotencyStatusFailed IdempotencyStatus = "failed"
|
||||
)
|
||||
|
||||
// IdempotencyRecord 幂等记录
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
@@ -84,7 +83,7 @@ func (r *PackageRepository) GetByID(ctx context.Context, supplierID, id int64) (
|
||||
`
|
||||
|
||||
pkg := &domain.Package{}
|
||||
var startAt, endAt pgx.NullTime
|
||||
var startAt, endAt *time.Time
|
||||
err := r.pool.QueryRow(ctx, query, id, supplierID).Scan(
|
||||
&pkg.ID, &pkg.SupplierID, &pkg.SupplierID, &pkg.Platform, &pkg.Model,
|
||||
&pkg.TotalQuota, &pkg.AvailableQuota, &pkg.SoldQuota, &pkg.ReservedQuota,
|
||||
@@ -103,11 +102,11 @@ func (r *PackageRepository) GetByID(ctx context.Context, supplierID, id int64) (
|
||||
return nil, fmt.Errorf("failed to get package: %w", err)
|
||||
}
|
||||
|
||||
if startAt.Valid {
|
||||
pkg.StartAt = startAt.Time
|
||||
if startAt != nil {
|
||||
pkg.StartAt = *startAt
|
||||
}
|
||||
if endAt.Valid {
|
||||
pkg.EndAt = endAt.Time
|
||||
if endAt != nil {
|
||||
pkg.EndAt = *endAt
|
||||
}
|
||||
|
||||
return pkg, nil
|
||||
|
||||
@@ -63,7 +63,7 @@ func (r *SettlementRepository) GetByID(ctx context.Context, supplierID, id int64
|
||||
`
|
||||
|
||||
s := &domain.Settlement{}
|
||||
var paidAt pgx.NullTime
|
||||
var paidAt *time.Time
|
||||
err := r.pool.QueryRow(ctx, query, id, supplierID).Scan(
|
||||
&s.ID, &s.SettlementNo, &s.SupplierID, &s.TotalAmount, &s.FeeAmount, &s.NetAmount,
|
||||
&s.Status, &s.PaymentMethod, &s.PaymentAccount,
|
||||
@@ -79,8 +79,8 @@ func (r *SettlementRepository) GetByID(ctx context.Context, supplierID, id int64
|
||||
return nil, fmt.Errorf("failed to get settlement: %w", err)
|
||||
}
|
||||
|
||||
if paidAt.Valid {
|
||||
s.PaidAt = &paidAt.Time
|
||||
if paidAt != nil {
|
||||
s.PaidAt = paidAt
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
||||
Reference in New Issue
Block a user