为 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:
@@ -19,7 +19,7 @@ func NewHandler(service *Service) *Handler {
|
||||
}
|
||||
|
||||
func (h *Handler) List(c *gin.Context) {
|
||||
items, err := h.service.List()
|
||||
items, err := h.service.List(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -28,7 +28,7 @@ func (h *Handler) List(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) PublishOptions(c *gin.Context) {
|
||||
options, err := h.service.PublishOptions()
|
||||
options, err := h.service.PublishOptions(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -37,7 +37,7 @@ func (h *Handler) PublishOptions(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) SalePriceConfig(c *gin.Context) {
|
||||
config, err := h.service.SalePriceConfig()
|
||||
config, err := h.service.SalePriceConfig(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -46,7 +46,7 @@ func (h *Handler) SalePriceConfig(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) OrderAgreements(c *gin.Context) {
|
||||
agreements, err := h.service.OrderAgreements()
|
||||
agreements, err := h.service.OrderAgreements(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -55,7 +55,7 @@ func (h *Handler) OrderAgreements(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) ListingPublishAgreements(c *gin.Context) {
|
||||
agreements, err := h.service.ListingPublishAgreements()
|
||||
agreements, err := h.service.ListingPublishAgreements(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -64,7 +64,7 @@ func (h *Handler) ListingPublishAgreements(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) PostRentalNotice(c *gin.Context) {
|
||||
notice, err := h.service.PostRentalNotice()
|
||||
notice, err := h.service.PostRentalNotice(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -73,7 +73,7 @@ func (h *Handler) PostRentalNotice(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) HomeAnnouncements(c *gin.Context) {
|
||||
announcements, err := h.service.HomeAnnouncements()
|
||||
announcements, err := h.service.HomeAnnouncements(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -82,7 +82,7 @@ func (h *Handler) HomeAnnouncements(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) HomeConfig(c *gin.Context) {
|
||||
config, err := h.service.HomeConfig()
|
||||
config, err := h.service.HomeConfig(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
@@ -102,7 +102,7 @@ func (h *Handler) Update(c *gin.Context) {
|
||||
response.BadRequest(c, "配置值不能为空")
|
||||
return
|
||||
}
|
||||
item, err := h.service.Update(adminID, key, req, auditMeta(c))
|
||||
item, err := h.service.Update(c.Request.Context(), adminID, key, req, auditMeta(c))
|
||||
if err != nil {
|
||||
writeConfigError(c, err)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package systemconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
@@ -64,12 +65,12 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) List() ([]ConfigDTO, error) {
|
||||
if err := r.ensureDefaults(); err != nil {
|
||||
func (r *Repository) List(ctx context.Context) ([]ConfigDTO, error) {
|
||||
if err := r.ensureDefaults(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var rows []model.SystemConfig
|
||||
if err := r.db.Where("`key` IN ?", adminVisibleConfigKeys).Order("`key` ASC").Find(&rows).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("`key` IN ?", adminVisibleConfigKeys).Order("`key` ASC").Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]ConfigDTO, 0, len(rows))
|
||||
@@ -79,20 +80,20 @@ func (r *Repository) List() ([]ConfigDTO, error) {
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func (r *Repository) FindValue(key string) (string, error) {
|
||||
if err := r.ensureDefaults(); err != nil {
|
||||
func (r *Repository) FindValue(ctx context.Context, key string) (string, error) {
|
||||
if err := r.ensureDefaults(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var row model.SystemConfig
|
||||
if err := r.db.Where("`key` = ?", key).First(&row).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("`key` = ?", key).First(&row).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return row.Value, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Update(actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
|
||||
func (r *Repository) Update(ctx context.Context, actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
|
||||
var row model.SystemConfig
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("`key` = ?", key).First(&row).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
row = model.SystemConfig{
|
||||
@@ -138,8 +139,8 @@ func (r *Repository) Update(actorID uint64, key string, req UpdateRequest, meta
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func (r *Repository) ensureDefaults() error {
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
func (r *Repository) ensureDefaults(ctx context.Context) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, item := range defaultConfigs {
|
||||
row := model.SystemConfig{
|
||||
Key: item.Key,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package systemconfig
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
@@ -19,70 +20,70 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List() ([]ConfigDTO, error) {
|
||||
func (s *Service) List(ctx context.Context) ([]ConfigDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.List()
|
||||
return s.repo.List(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) PublishOptions() (*PublishOptionsDTO, error) {
|
||||
func (s *Service) PublishOptions(ctx context.Context) (*PublishOptionsDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
options, err := s.publishOptions()
|
||||
options, err := s.publishOptions(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &options, nil
|
||||
}
|
||||
|
||||
func (s *Service) SalePriceConfig() (*PublishSalePriceConfig, error) {
|
||||
func (s *Service) SalePriceConfig(ctx context.Context) (*PublishSalePriceConfig, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
config, err := s.salePriceConfig()
|
||||
config, err := s.salePriceConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func (s *Service) OrderAgreements() (*OrderAgreementsDTO, error) {
|
||||
func (s *Service) OrderAgreements(ctx context.Context) (*OrderAgreementsDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
agreements, err := s.orderAgreements()
|
||||
agreements, err := s.orderAgreements(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &agreements, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListingPublishAgreements() (*ListingPublishAgreementsDTO, error) {
|
||||
func (s *Service) ListingPublishAgreements(ctx context.Context) (*ListingPublishAgreementsDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
agreements, err := s.listingPublishAgreements()
|
||||
agreements, err := s.listingPublishAgreements(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &agreements, nil
|
||||
}
|
||||
|
||||
func (s *Service) PostRentalNotice() (*PostRentalNoticeDTO, error) {
|
||||
func (s *Service) PostRentalNotice(ctx context.Context) (*PostRentalNoticeDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
notice, err := s.postRentalNotice()
|
||||
notice, err := s.postRentalNotice(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ¬ice, nil
|
||||
}
|
||||
|
||||
func (s *Service) publishOptions() (PublishOptionsDTO, error) {
|
||||
value, err := s.repo.FindValue(publishOptionsConfigKey)
|
||||
func (s *Service) publishOptions(ctx context.Context) (PublishOptionsDTO, error) {
|
||||
value, err := s.repo.FindValue(ctx, publishOptionsConfigKey)
|
||||
if err != nil {
|
||||
return PublishOptionsDTO{}, err
|
||||
}
|
||||
@@ -94,8 +95,8 @@ func (s *Service) publishOptions() (PublishOptionsDTO, error) {
|
||||
return options, nil
|
||||
}
|
||||
|
||||
func (s *Service) salePriceConfig() (PublishSalePriceConfig, error) {
|
||||
value, err := s.repo.FindValue(salePriceConfigKey)
|
||||
func (s *Service) salePriceConfig(ctx context.Context) (PublishSalePriceConfig, error) {
|
||||
value, err := s.repo.FindValue(ctx, salePriceConfigKey)
|
||||
if err != nil {
|
||||
return PublishSalePriceConfig{}, err
|
||||
}
|
||||
@@ -107,8 +108,8 @@ func (s *Service) salePriceConfig() (PublishSalePriceConfig, error) {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func (s *Service) orderAgreements() (OrderAgreementsDTO, error) {
|
||||
value, err := s.repo.FindValue(orderAgreementsConfigKey)
|
||||
func (s *Service) orderAgreements(ctx context.Context) (OrderAgreementsDTO, error) {
|
||||
value, err := s.repo.FindValue(ctx, orderAgreementsConfigKey)
|
||||
if err != nil {
|
||||
return OrderAgreementsDTO{}, err
|
||||
}
|
||||
@@ -120,8 +121,8 @@ func (s *Service) orderAgreements() (OrderAgreementsDTO, error) {
|
||||
return agreements, nil
|
||||
}
|
||||
|
||||
func (s *Service) listingPublishAgreements() (ListingPublishAgreementsDTO, error) {
|
||||
value, err := s.repo.FindValue(listingPublishAgreementsConfigKey)
|
||||
func (s *Service) listingPublishAgreements(ctx context.Context) (ListingPublishAgreementsDTO, error) {
|
||||
value, err := s.repo.FindValue(ctx, listingPublishAgreementsConfigKey)
|
||||
if err != nil {
|
||||
return ListingPublishAgreementsDTO{}, err
|
||||
}
|
||||
@@ -133,8 +134,8 @@ func (s *Service) listingPublishAgreements() (ListingPublishAgreementsDTO, error
|
||||
return agreements, nil
|
||||
}
|
||||
|
||||
func (s *Service) postRentalNotice() (PostRentalNoticeDTO, error) {
|
||||
value, err := s.repo.FindValue(postRentalNoticeConfigKey)
|
||||
func (s *Service) postRentalNotice(ctx context.Context) (PostRentalNoticeDTO, error) {
|
||||
value, err := s.repo.FindValue(ctx, postRentalNoticeConfigKey)
|
||||
if err != nil {
|
||||
return PostRentalNoticeDTO{}, err
|
||||
}
|
||||
@@ -146,30 +147,30 @@ func (s *Service) postRentalNotice() (PostRentalNoticeDTO, error) {
|
||||
return notice, nil
|
||||
}
|
||||
|
||||
func (s *Service) HomeAnnouncements() (*HomeAnnouncementsDTO, error) {
|
||||
func (s *Service) HomeAnnouncements(ctx context.Context) (*HomeAnnouncementsDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
items, err := s.homeAnnouncements()
|
||||
items, err := s.homeAnnouncements(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &HomeAnnouncementsDTO{Items: items}, nil
|
||||
}
|
||||
|
||||
func (s *Service) HomeConfig() (*HomeConfigDTO, error) {
|
||||
func (s *Service) HomeConfig(ctx context.Context) (*HomeConfigDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
announcements, err := s.homeAnnouncements()
|
||||
announcements, err := s.homeAnnouncements(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
banners, err := s.homeBanners()
|
||||
banners, err := s.homeBanners(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
publishOptions, err := s.publishOptions()
|
||||
publishOptions, err := s.publishOptions(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -180,8 +181,8 @@ func (s *Service) HomeConfig() (*HomeConfigDTO, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) homeAnnouncements() ([]string, error) {
|
||||
value, err := s.repo.FindValue(homeAnnouncementsConfigKey)
|
||||
func (s *Service) homeAnnouncements(ctx context.Context) ([]string, error) {
|
||||
value, err := s.repo.FindValue(ctx, homeAnnouncementsConfigKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -192,8 +193,8 @@ func (s *Service) homeAnnouncements() ([]string, error) {
|
||||
return normalizeAnnouncements(items), nil
|
||||
}
|
||||
|
||||
func (s *Service) homeBanners() ([]HomeBannerItem, error) {
|
||||
value, err := s.repo.FindValue(homeBannersConfigKey)
|
||||
func (s *Service) homeBanners(ctx context.Context) ([]HomeBannerItem, error) {
|
||||
value, err := s.repo.FindValue(ctx, homeBannersConfigKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -204,14 +205,14 @@ func (s *Service) homeBanners() ([]HomeBannerItem, error) {
|
||||
return normalizeHomeBanners(items), nil
|
||||
}
|
||||
|
||||
func (s *Service) Update(actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
|
||||
func (s *Service) Update(ctx context.Context, actorID uint64, key string, req UpdateRequest, meta AuditMeta) (*ConfigDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
if key == "" || req.Value == "" {
|
||||
return nil, ErrInvalidConfig
|
||||
}
|
||||
return s.repo.Update(actorID, key, req, meta)
|
||||
return s.repo.Update(ctx, actorID, key, req, meta)
|
||||
}
|
||||
|
||||
func normalizeAnnouncements(items []string) []string {
|
||||
|
||||
Reference in New Issue
Block a user