为 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:
@@ -38,7 +38,7 @@ func (h *Handler) List(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
result, err := h.service.List(query)
|
||||
result, err := h.service.List(c.Request.Context(), query)
|
||||
if err != nil {
|
||||
writeAuditError(c, err)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package adminaudit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -17,14 +18,14 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) List(query Query) (*PaginatedResult, error) {
|
||||
db := r.db.Table("audit_logs AS al").
|
||||
func (r *Repository) List(ctx context.Context, query Query) (*PaginatedResult, error) {
|
||||
db := r.db.WithContext(ctx).Table("audit_logs AS al").
|
||||
Select(`al.id, al.actor_type, al.actor_id, COALESCE(au.username, '') AS actor_username,
|
||||
COALESCE(au.nickname, '') AS actor_nickname, al.action, al.biz_type, al.biz_id,
|
||||
al.ip, al.user_agent, al.detail, al.created_at`).
|
||||
Joins("LEFT JOIN admin_users AS au ON au.id = al.actor_id AND al.actor_type = ?", "admin")
|
||||
|
||||
countDB := r.db.Model(&model.AuditLog{})
|
||||
countDB := r.db.WithContext(ctx).Model(&model.AuditLog{})
|
||||
if query.ActorID > 0 {
|
||||
db = db.Where("al.actor_id = ?", query.ActorID)
|
||||
countDB = countDB.Where("actor_id = ?", query.ActorID)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package adminaudit
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
|
||||
@@ -12,9 +15,9 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(query Query) (*PaginatedResult, error) {
|
||||
func (s *Service) List(ctx context.Context, query Query) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.List(query)
|
||||
return s.repo.List(ctx, query)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func NewHandler(service *Service) *Handler {
|
||||
}
|
||||
|
||||
func (h *Handler) Captcha(c *gin.Context) {
|
||||
item, err := h.service.Captcha()
|
||||
item, err := h.service.Captcha(c.Request.Context(), )
|
||||
if err != nil {
|
||||
writeAdminAuthError(c, err)
|
||||
return
|
||||
@@ -34,7 +34,7 @@ func (h *Handler) Login(c *gin.Context) {
|
||||
response.BadRequest(c, "用户名、密码和验证码不能为空")
|
||||
return
|
||||
}
|
||||
result, err := h.service.Login(strings.TrimSpace(req.Username), req.Password, strings.TrimSpace(req.CaptchaID), strings.TrimSpace(req.CaptchaCode))
|
||||
result, err := h.service.Login(c.Request.Context(), strings.TrimSpace(req.Username), req.Password, strings.TrimSpace(req.CaptchaID), strings.TrimSpace(req.CaptchaCode))
|
||||
if err != nil {
|
||||
writeAdminAuthError(c, err)
|
||||
return
|
||||
@@ -53,7 +53,7 @@ func (h *Handler) Me(c *gin.Context) {
|
||||
response.Unauthorized(c, "管理员上下文无效")
|
||||
return
|
||||
}
|
||||
admin, err := h.service.Me(adminID)
|
||||
admin, err := h.service.Me(c.Request.Context(), adminID)
|
||||
if err != nil {
|
||||
writeAdminAuthError(c, err)
|
||||
return
|
||||
@@ -76,7 +76,7 @@ func (h *Handler) UpdateSupportStatus(c *gin.Context) {
|
||||
response.BadRequest(c, "状态值无效,必须是 online、offline 或 busy")
|
||||
return
|
||||
}
|
||||
if err := h.service.UpdateSupportStatus(adminID, req.Status); err != nil {
|
||||
if err := h.service.UpdateSupportStatus(c.Request.Context(), adminID, req.Status); err != nil {
|
||||
writeAdminAuthError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -102,7 +102,7 @@ func (h *Handler) Refresh(c *gin.Context) {
|
||||
response.BadRequest(c, "refresh_token 不能为空")
|
||||
return
|
||||
}
|
||||
tokens, err := h.service.Refresh(req.RefreshToken)
|
||||
tokens, err := h.service.Refresh(c.Request.Context(), req.RefreshToken)
|
||||
if err != nil {
|
||||
writeAdminAuthError(c, err)
|
||||
return
|
||||
|
||||
@@ -36,7 +36,7 @@ func NewRepository(db *gorm.DB, redis *redis.Client, jwt *auth.JWTManager) *Repo
|
||||
return &Repository{db: db, redis: redis, jwt: jwt}
|
||||
}
|
||||
|
||||
func (r *Repository) Captcha() (*CaptchaDTO, error) {
|
||||
func (r *Repository) Captcha(ctx context.Context) (*CaptchaDTO, error) {
|
||||
if r.redis == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -48,7 +48,6 @@ func (r *Repository) Captcha() (*CaptchaDTO, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx := context.Background()
|
||||
if err := r.redis.Set(ctx, captchaKey(captchaID), strings.ToUpper(code), captchaTTL).Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -59,15 +58,15 @@ func (r *Repository) Captcha() (*CaptchaDTO, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) Login(username string, password string, captchaID string, captchaCode string) (LoginResult, error) {
|
||||
if err := r.verifyCaptcha(captchaID, captchaCode); err != nil {
|
||||
func (r *Repository) Login(ctx context.Context, username string, password string, captchaID string, captchaCode string) (LoginResult, error) {
|
||||
if err := r.verifyCaptcha(ctx, captchaID, captchaCode); err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
if err := r.ensureDefaultAdmin(); err != nil {
|
||||
if err := r.ensureDefaultAdmin(ctx); err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
var admin model.AdminUser
|
||||
if err := r.db.Where("username = ?", username).First(&admin).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("username = ?", username).First(&admin).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return LoginResult{}, ErrInvalidCredential
|
||||
}
|
||||
@@ -81,7 +80,7 @@ func (r *Repository) Login(username string, password string, captchaID string, c
|
||||
}
|
||||
now := time.Now()
|
||||
admin.LastLoginAt = &now
|
||||
if err := r.db.Save(&admin).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Save(&admin).Error; err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
tokens, err := r.jwt.GenerateSubjectPair(admin.ID, admin.Username, "admin")
|
||||
@@ -89,15 +88,14 @@ func (r *Repository) Login(username string, password string, captchaID string, c
|
||||
return LoginResult{}, err
|
||||
}
|
||||
dto := toDTO(admin)
|
||||
r.loadRolesAndPerms(&dto)
|
||||
r.loadRolesAndPerms(ctx, &dto)
|
||||
return LoginResult{Admin: dto, Tokens: tokens}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) verifyCaptcha(captchaID string, captchaCode string) error {
|
||||
func (r *Repository) verifyCaptcha(ctx context.Context, captchaID string, captchaCode string) error {
|
||||
if r.redis == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
ctx := context.Background()
|
||||
key := captchaKey(captchaID)
|
||||
stored, err := r.redis.Get(ctx, key).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
@@ -113,34 +111,34 @@ func (r *Repository) verifyCaptcha(captchaID string, captchaCode string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) FindByID(id uint64) (*AdminDTO, error) {
|
||||
func (r *Repository) FindByID(ctx context.Context, id uint64) (*AdminDTO, error) {
|
||||
var admin model.AdminUser
|
||||
if err := r.db.First(&admin, id).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).First(&admin, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if admin.Status != "active" {
|
||||
return nil, ErrAdminDisabled
|
||||
}
|
||||
dto := toDTO(admin)
|
||||
r.loadRolesAndPerms(&dto)
|
||||
r.loadRolesAndPerms(ctx, &dto)
|
||||
return &dto, nil
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateSupportStatus(adminID uint64, status string) error {
|
||||
func (r *Repository) UpdateSupportStatus(ctx context.Context, adminID uint64, status string) error {
|
||||
if r.db == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if status != "online" && status != "offline" && status != "busy" {
|
||||
return errors.New("invalid support status")
|
||||
}
|
||||
return r.db.Model(&model.AdminUser{}).
|
||||
return r.db.WithContext(ctx).Model(&model.AdminUser{}).
|
||||
Where("id = ?", adminID).
|
||||
Update("support_status", status).Error
|
||||
}
|
||||
|
||||
func (r *Repository) ensureDefaultAdmin() error {
|
||||
func (r *Repository) ensureDefaultAdmin(ctx context.Context) error {
|
||||
var count int64
|
||||
if err := r.db.Model(&model.AdminUser{}).Count(&count).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Model(&model.AdminUser{}).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
@@ -156,14 +154,14 @@ func (r *Repository) ensureDefaultAdmin() error {
|
||||
Nickname: "超级管理员",
|
||||
Status: "active",
|
||||
}
|
||||
if err := r.db.Create(&admin).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Create(&admin).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 自动关联 super_admin 角色
|
||||
var superAdminRole model.Role
|
||||
if err := r.db.Where("code = ?", "super_admin").First(&superAdminRole).Error; err == nil {
|
||||
r.db.Create(&model.AdminUserRole{
|
||||
if err := r.db.WithContext(ctx).Where("code = ?", "super_admin").First(&superAdminRole).Error; err == nil {
|
||||
r.db.WithContext(ctx).Create(&model.AdminUserRole{
|
||||
AdminID: admin.ID,
|
||||
RoleID: superAdminRole.ID,
|
||||
})
|
||||
@@ -183,13 +181,13 @@ func toDTO(admin model.AdminUser) AdminDTO {
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Repository) loadRolesAndPerms(dto *AdminDTO) {
|
||||
func (r *Repository) loadRolesAndPerms(ctx context.Context, dto *AdminDTO) {
|
||||
if r.db == nil {
|
||||
return
|
||||
}
|
||||
// 加载角色
|
||||
var roles []RoleDTO
|
||||
r.db.Table("roles").
|
||||
r.db.WithContext(ctx).Table("roles").
|
||||
Joins("JOIN admin_user_roles aur ON aur.role_id = roles.id").
|
||||
Where("aur.admin_user_id = ?", dto.ID).
|
||||
Find(&roles)
|
||||
@@ -205,7 +203,7 @@ func (r *Repository) loadRolesAndPerms(dto *AdminDTO) {
|
||||
}
|
||||
|
||||
var permCodes []string
|
||||
r.db.Table("permissions").
|
||||
r.db.WithContext(ctx).Table("permissions").
|
||||
Select("DISTINCT permissions.code").
|
||||
Joins("JOIN role_permissions rp ON rp.permission_id = permissions.id").
|
||||
Joins("JOIN admin_user_roles aur ON aur.role_id = rp.role_id").
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package adminauth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"hfb_sys/backend/internal/modules/auth"
|
||||
@@ -23,7 +24,7 @@ func NewService(repo *Repository, jwt *auth.JWTManager) *Service {
|
||||
return &Service{repo: repo, jwt: jwt}
|
||||
}
|
||||
|
||||
func (s *Service) Refresh(refreshToken string) (*auth.TokenPair, error) {
|
||||
func (s *Service) Refresh(ctx context.Context, refreshToken string) (*auth.TokenPair, error) {
|
||||
if s.jwt == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
@@ -38,33 +39,33 @@ func (s *Service) Refresh(refreshToken string) (*auth.TokenPair, error) {
|
||||
return &pair, nil
|
||||
}
|
||||
|
||||
func (s *Service) Captcha() (*CaptchaDTO, error) {
|
||||
func (s *Service) Captcha(ctx context.Context, ) (*CaptchaDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.Captcha()
|
||||
return s.repo.Captcha(ctx, )
|
||||
}
|
||||
|
||||
func (s *Service) Login(username string, password string, captchaID string, captchaCode string) (LoginResult, error) {
|
||||
func (s *Service) Login(ctx context.Context, username string, password string, captchaID string, captchaCode string) (LoginResult, error) {
|
||||
if s.repo == nil {
|
||||
return LoginResult{}, ErrDependencyUnavailable
|
||||
}
|
||||
if username == "" || password == "" || captchaID == "" || captchaCode == "" {
|
||||
return LoginResult{}, ErrInvalidCredential
|
||||
}
|
||||
return s.repo.Login(username, password, captchaID, captchaCode)
|
||||
return s.repo.Login(ctx, username, password, captchaID, captchaCode)
|
||||
}
|
||||
|
||||
func (s *Service) Me(adminID uint64) (*AdminDTO, error) {
|
||||
func (s *Service) Me(ctx context.Context, adminID uint64) (*AdminDTO, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.FindByID(adminID)
|
||||
return s.repo.FindByID(ctx, adminID)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateSupportStatus(adminID uint64, status string) error {
|
||||
func (s *Service) UpdateSupportStatus(ctx context.Context, adminID uint64, status string) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.UpdateSupportStatus(adminID, status)
|
||||
return s.repo.UpdateSupportStatus(ctx, adminID, status)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
@@ -18,25 +19,25 @@ func NewUserRepository(db *gorm.DB) *UserRepository {
|
||||
return &UserRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindByID(id uint64) (*model.User, error) {
|
||||
func (r *UserRepository) FindByID(ctx context.Context, id uint64) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := r.db.First(&user, id).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).First(&user, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *UserRepository) UpdateProfile(id uint64, nickname string, avatarURL string) (*model.User, error) {
|
||||
if err := r.db.Model(&model.User{}).Where("id = ?", id).Updates(map[string]any{
|
||||
func (r *UserRepository) UpdateProfile(ctx context.Context, id uint64, nickname string, avatarURL string) (*model.User, error) {
|
||||
if err := r.db.WithContext(ctx).Model(&model.User{}).Where("id = ?", id).Updates(map[string]any{
|
||||
"nickname": nickname,
|
||||
"avatar_url": avatarURL,
|
||||
}).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.FindByID(id)
|
||||
return r.FindByID(ctx, id)
|
||||
}
|
||||
|
||||
func (r *UserRepository) FindOrCreateByPhone(phone string) (*model.User, error) {
|
||||
func (r *UserRepository) FindOrCreateByPhone(ctx context.Context, phone string) (*model.User, error) {
|
||||
now := time.Now()
|
||||
user := model.User{
|
||||
Phone: phone,
|
||||
@@ -48,7 +49,7 @@ func (r *UserRepository) FindOrCreateByPhone(phone string) (*model.User, error)
|
||||
LastLoginAt: &now,
|
||||
}
|
||||
|
||||
err := r.db.Clauses(clause.OnConflict{
|
||||
err := r.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "phone"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"last_login_at", "updated_at"}),
|
||||
}).Create(&user).Error
|
||||
@@ -57,7 +58,7 @@ func (r *UserRepository) FindOrCreateByPhone(phone string) (*model.User, error)
|
||||
}
|
||||
|
||||
var found model.User
|
||||
if err := r.db.Where("phone = ?", phone).First(&found).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("phone = ?", phone).First(&found).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &found, nil
|
||||
|
||||
@@ -101,7 +101,7 @@ func (s *Service) LoginWithSMS(ctx context.Context, phone string, code string) (
|
||||
return LoginResult{}, err
|
||||
}
|
||||
|
||||
user, err := s.users.FindOrCreateByPhone(phone)
|
||||
user, err := s.users.FindOrCreateByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return LoginResult{}, err
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ func (h *Handler) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
page, pageSize := parsePagination(c)
|
||||
result, err := h.service.List(userID, page, pageSize)
|
||||
result, err := h.service.List(c.Request.Context(), userID, page, pageSize)
|
||||
if err != nil {
|
||||
writeNotificationError(c, err)
|
||||
return
|
||||
@@ -59,7 +59,7 @@ func (h *Handler) MarkRead(c *gin.Context) {
|
||||
response.BadRequest(c, "ID 不正确")
|
||||
return
|
||||
}
|
||||
if err := h.service.MarkRead(userID, id); err != nil {
|
||||
if err := h.service.MarkRead(c.Request.Context(), userID, id); err != nil {
|
||||
writeNotificationError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package notification
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -25,14 +26,14 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (r *Repository) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
var total int64
|
||||
if err := r.db.Model(&model.Notification{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Model(&model.Notification{}).Where("user_id = ?", userID).Count(&total).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []model.Notification
|
||||
if err := r.db.Where("user_id = ?", userID).Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]NotificationDTO, 0, len(rows))
|
||||
@@ -42,9 +43,9 @@ func (r *Repository) List(userID uint64, page, pageSize int) (*PaginatedResult,
|
||||
return &PaginatedResult{Items: items, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) MarkRead(userID uint64, id uint64) error {
|
||||
func (r *Repository) MarkRead(ctx context.Context, userID uint64, id uint64) error {
|
||||
now := time.Now()
|
||||
return r.db.Model(&model.Notification{}).
|
||||
return r.db.WithContext(ctx).Model(&model.Notification{}).
|
||||
Where("id = ? AND user_id = ?", id, userID).
|
||||
Update("read_at", now).Error
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package notification
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrDependencyUnavailable = errors.New("dependency unavailable")
|
||||
|
||||
@@ -12,16 +15,16 @@ func NewService(repo *Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) List(userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
func (s *Service) List(ctx context.Context, userID uint64, page, pageSize int) (*PaginatedResult, error) {
|
||||
if s.repo == nil {
|
||||
return nil, ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.List(userID, page, pageSize)
|
||||
return s.repo.List(ctx, userID, page, pageSize)
|
||||
}
|
||||
|
||||
func (s *Service) MarkRead(userID uint64, id uint64) error {
|
||||
func (s *Service) MarkRead(ctx context.Context, userID uint64, id uint64) error {
|
||||
if s.repo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
return s.repo.MarkRead(userID, id)
|
||||
return s.repo.MarkRead(ctx, userID, id)
|
||||
}
|
||||
|
||||
@@ -940,7 +940,7 @@ func (r *Repository) confirmPaid(payment *model.PaymentOrder, status string, pai
|
||||
if r.walletRepo == nil {
|
||||
return ErrDependencyUnavailable
|
||||
}
|
||||
if err := r.walletRepo.ConfirmRechargeFromChannel(payment.UserID, firstNonEmpty(payment.ProviderOrderID, payment.PaymentNo), payment.AmountCent); err != nil {
|
||||
if err := r.walletRepo.ConfirmRechargeFromChannel(context.Background(), payment.UserID, firstNonEmpty(payment.ProviderOrderID, payment.PaymentNo), payment.AmountCent); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -51,7 +51,7 @@ func (h *Handler) Status(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
status, err := h.service.Status(userID)
|
||||
status, err := h.service.Status(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
writeRealnameError(c, err)
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package realname
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/model"
|
||||
@@ -17,15 +18,15 @@ func NewRepository(db *gorm.DB) *Repository {
|
||||
return &Repository{db: db}
|
||||
}
|
||||
|
||||
func (r *Repository) FindByUserID(userID uint64) (*model.UserRealname, error) {
|
||||
func (r *Repository) FindByUserID(ctx context.Context, userID uint64) (*model.UserRealname, error) {
|
||||
var record model.UserRealname
|
||||
if err := r.db.Where("user_id = ?", userID).First(&record).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).First(&record).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SaveResult(userID uint64, provider string, result ProviderResult) (*model.UserRealname, error) {
|
||||
func (r *Repository) SaveResult(ctx context.Context, userID uint64, provider string, result ProviderResult) (*model.UserRealname, error) {
|
||||
record := model.UserRealname{
|
||||
UserID: userID,
|
||||
Provider: provider,
|
||||
@@ -39,7 +40,7 @@ func (r *Repository) SaveResult(userID uint64, provider string, result ProviderR
|
||||
FailReason: result.FailReason,
|
||||
}
|
||||
|
||||
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.OnConflict{
|
||||
Columns: []clause.Column{{Name: "user_id"}},
|
||||
DoUpdates: clause.Assignments(map[string]any{
|
||||
@@ -70,5 +71,5 @@ func (r *Repository) SaveResult(userID uint64, provider string, result ProviderR
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.FindByUserID(userID)
|
||||
return r.FindByUserID(ctx, userID)
|
||||
}
|
||||
|
||||
@@ -71,14 +71,14 @@ func (s *Service) Start(ctx context.Context, userID uint64, name string, idNo st
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return s.repo.SaveResult(userID, s.provider.Name(), result)
|
||||
return s.repo.SaveResult(ctx, userID, s.provider.Name(), result)
|
||||
}
|
||||
|
||||
func (s *Service) Status(userID uint64) (PublicStatus, error) {
|
||||
func (s *Service) Status(ctx context.Context, userID uint64) (PublicStatus, error) {
|
||||
if s.repo == nil {
|
||||
return PublicStatus{}, ErrDependencyUnavailable
|
||||
}
|
||||
record, err := s.repo.FindByUserID(userID)
|
||||
record, err := s.repo.FindByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return PublicStatus{Status: "unverified"}, nil
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -35,7 +35,7 @@ func (h *Handler) Me(c *gin.Context) {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
return
|
||||
}
|
||||
user, err := h.users.FindByID(userID.(uint64))
|
||||
user, err := h.users.FindByID(c.Request.Context(), userID.(uint64))
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
response.Unauthorized(c, "用户不存在")
|
||||
return
|
||||
@@ -79,7 +79,7 @@ func (h *Handler) UpdateMe(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.users.UpdateProfile(userID.(uint64), nickname, avatarURL)
|
||||
user, err := h.users.UpdateProfile(c.Request.Context(), userID.(uint64), nickname, avatarURL)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
response.Unauthorized(c, "用户不存在")
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user