为 9 个模块添加 Context 超时控制
完成模块: - auth: 3 个 Repository 方法 + Service + Handler + Middleware - wallet: 已有 context 支持,修复依赖调用 - payment: 已有 context 支持,修复 wallet 调用 - adminaudit: 1 个方法 - notification: 2 个方法 - realname: 2 个方法 - systemconfig: 4 个方法 - adminauth: 7 个方法 - adminuser: 6 个方法 所有数据库调用已改为 r.db.WithContext(ctx),完整传递 context 链路。 待完成模块: order, listing, chat 等 12 个模块(约 157 个方法)
This commit is contained in:
@@ -36,7 +36,7 @@ func parsePagination(c *gin.Context) (int, int) {
|
||||
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
page, pageSize := parsePagination(c)
|
||||
result, err := h.service.List(page, pageSize)
|
||||
result, err := h.service.List(c.Request.Context(), page, pageSize)
|
||||
if err != nil {
|
||||
writeAdminUserError(c, err)
|
||||
return
|
||||
@@ -56,7 +56,7 @@ func (h *Handler) Freeze(c *gin.Context) {
|
||||
}
|
||||
var req FreezeRequest
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
item, err := h.service.Freeze(adminID, userID, req, auditMeta(c))
|
||||
item, err := h.service.Freeze(c.Request.Context(), adminID, userID, req, auditMeta(c))
|
||||
if err != nil {
|
||||
writeAdminUserError(c, err)
|
||||
return
|
||||
@@ -74,7 +74,7 @@ func (h *Handler) Unfreeze(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
item, err := h.service.Unfreeze(adminID, userID, auditMeta(c))
|
||||
item, err := h.service.Unfreeze(c.Request.Context(), adminID, userID, auditMeta(c))
|
||||
if err != nil {
|
||||
writeAdminUserError(c, err)
|
||||
return
|
||||
@@ -97,7 +97,7 @@ func (h *Handler) SetDepositFreeQuota(c *gin.Context) {
|
||||
response.BadRequest(c, "免押额度不正确")
|
||||
return
|
||||
}
|
||||
item, err := h.service.SetDepositFreeQuota(adminID, userID, req, auditMeta(c))
|
||||
item, err := h.service.SetDepositFreeQuota(c.Request.Context(), adminID, userID, req, auditMeta(c))
|
||||
if err != nil {
|
||||
writeAdminUserError(c, err)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package adminuser
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"hfb_sys/backend/internal/auditlog"
|
||||
@@ -20,14 +21,14 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) List(page, pageSize int) (*PaginatedResult, error) {
|
||||
func (r *Repository) List(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
|
||||
var total int64
|
||||
if err := r.db.Model(&model.User{}).Count(&total).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []userRow
|
||||
err := r.db.Table("users AS u").
|
||||
err := r.db.WithContext(ctx).Table("users AS u").
|
||||
Select(`u.*,
|
||||
COALESCE(o.order_count, 0) AS order_count,
|
||||
COALESCE(l.listing_count, 0) AS listing_count,
|
||||
@@ -50,20 +51,20 @@ func (r *Repository) List(page, pageSize int) (*PaginatedResult, error) {
|
||||
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Freeze(adminID uint64, userID uint64, req FreezeRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
return r.updateStatus(adminID, userID, "frozen", "frozen", "admin_user.freeze", req.Reason, meta)
|
||||
func (r *Repository) Freeze(ctx context.Context, adminID uint64, userID uint64, req FreezeRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
return r.updateStatus(ctx, adminID, userID, "frozen", "frozen", "admin_user.freeze", req.Reason, meta)
|
||||
}
|
||||
|
||||
func (r *Repository) Unfreeze(adminID uint64, userID uint64, meta AuditMeta) (*UserDTO, error) {
|
||||
return r.updateStatus(adminID, userID, "active", "normal", "admin_user.unfreeze", "", meta)
|
||||
func (r *Repository) Unfreeze(ctx context.Context, adminID uint64, userID uint64, meta AuditMeta) (*UserDTO, error) {
|
||||
return r.updateStatus(ctx, adminID, userID, "active", "normal", "admin_user.unfreeze", "", meta)
|
||||
}
|
||||
|
||||
func (r *Repository) SetDepositFreeQuota(adminID uint64, userID uint64, req DepositFreeQuotaRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
func (r *Repository) SetDepositFreeQuota(ctx context.Context, adminID uint64, userID uint64, req DepositFreeQuotaRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
amountCent := depositFreeQuotaAmountCent(req)
|
||||
if amountCent < 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
@@ -82,11 +83,11 @@ func (r *Repository) SetDepositFreeQuota(adminID uint64, userID uint64, req Depo
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Find(userID)
|
||||
return r.Find(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) updateStatus(adminID uint64, userID uint64, status string, riskStatus string, action string, reason string, meta AuditMeta) (*UserDTO, error) {
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
func (r *Repository) updateStatus(ctx context.Context, adminID uint64, userID uint64, status string, riskStatus string, action string, reason string, meta AuditMeta) (*UserDTO, error) {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).First(&user, userID).Error; err != nil {
|
||||
return err
|
||||
@@ -110,12 +111,12 @@ func (r *Repository) updateStatus(adminID uint64, userID uint64, status string,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.Find(userID)
|
||||
return r.Find(ctx, userID)
|
||||
}
|
||||
|
||||
func (r *Repository) Find(userID uint64) (*UserDTO, error) {
|
||||
func (r *Repository) Find(ctx context.Context, userID uint64) (*UserDTO, error) {
|
||||
var row userRow
|
||||
err := r.db.Table("users AS u").
|
||||
err := r.db.WithContext(ctx).Table("users AS u").
|
||||
Select(`u.*,
|
||||
COALESCE(o.order_count, 0) AS order_count,
|
||||
COALESCE(l.listing_count, 0) AS listing_count,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package adminuser
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
@@ -15,39 +18,39 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) List(ctx context.Context, page, pageSize int) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.List(page, pageSize)
|
||||
return s.repo.List(ctx, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) Freeze(adminID uint64, userID uint64, req FreezeRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
func (s *Service) Freeze(ctx context.Context, adminID uint64, userID uint64, req FreezeRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
return s.repo.Freeze(adminID, userID, req, meta)
|
||||
return s.repo.Freeze(ctx, adminID, userID, req, meta)
|
||||
}
|
||||
|
||||
func (s *Service) Unfreeze(adminID uint64, userID uint64, meta AuditMeta) (*UserDTO, error) {
|
||||
func (s *Service) Unfreeze(ctx context.Context, adminID uint64, userID uint64, meta AuditMeta) (*UserDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
return s.repo.Unfreeze(adminID, userID, meta)
|
||||
return s.repo.Unfreeze(ctx, adminID, userID, meta)
|
||||
}
|
||||
|
||||
func (s *Service) SetDepositFreeQuota(adminID uint64, userID uint64, req DepositFreeQuotaRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
func (s *Service) SetDepositFreeQuota(ctx context.Context, adminID uint64, userID uint64, req DepositFreeQuotaRequest, meta AuditMeta) (*UserDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if userID == 0 || req.AmountCent < 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
return s.repo.SetDepositFreeQuota(adminID, userID, req, meta)
|
||||
return s.repo.SetDepositFreeQuota(ctx, adminID, userID, req, meta)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user