为 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:
@@ -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