refactor(account): pass request context into services

This commit is contained in:
Leon
2025-12-09 16:05:10 +08:00
parent 4bedf58a03
commit 6804b71fe9
4 changed files with 42 additions and 39 deletions

View File

@@ -57,7 +57,7 @@ func (h *AccountHandler) CreateAccount(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if err := h.accountService.CreateAccount(&Account{ if err := h.accountService.CreateAccount(c.Request.Context(), &Account{
Username: req.Username, Username: req.Username,
Password: req.Password, Password: req.Password,
}); err != nil { }); err != nil {
@@ -78,7 +78,7 @@ func (h *AccountHandler) Rename(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if err := h.accountService.Rename(accountID, req.NewUsername); err != nil { if err := h.accountService.Rename(c.Request.Context(), accountID, req.NewUsername); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} }
@@ -91,7 +91,7 @@ func (h *AccountHandler) ChangePassword(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if err := h.accountService.ChangePassword(req.Username, req.OldPassword, req.NewPassword); err != nil { if err := h.accountService.ChangePassword(c.Request.Context(), req.Username, req.OldPassword, req.NewPassword); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} }
@@ -104,7 +104,7 @@ func (h *AccountHandler) FindByID(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if account, err := h.accountService.FindByID(req.ID); err != nil { if account, err := h.accountService.FindByID(c.Request.Context(), req.ID); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} else { } else {
@@ -118,7 +118,7 @@ func (h *AccountHandler) FindByUsername(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if account, err := h.accountService.FindByUsername(req.Username); err != nil { if account, err := h.accountService.FindByUsername(c.Request.Context(), req.Username); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} else { } else {
@@ -132,7 +132,7 @@ func (h *AccountHandler) Login(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if token, err := h.accountService.Login(req.Username, req.Password); err != nil { if token, err := h.accountService.Login(c.Request.Context(), req.Username, req.Password); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} else { } else {
@@ -146,7 +146,7 @@ func (h *AccountHandler) Logout(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} }
if err := h.accountService.Logout(accountID); err != nil { if err := h.accountService.Logout(c.Request.Context(), accountID); err != nil {
c.JSON(500, gin.H{"error": err.Error()}) c.JSON(500, gin.H{"error": err.Error()})
return return
} }

View File

@@ -1,6 +1,8 @@
package account package account
import ( import (
"context"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -12,52 +14,52 @@ func NewAccountRepository(db *gorm.DB) *AccountRepository {
return &AccountRepository{db: db} return &AccountRepository{db: db}
} }
func (ar *AccountRepository) CreateAccount(account *Account) error { func (ar *AccountRepository) CreateAccount(ctx context.Context, account *Account) error {
if err := ar.db.Create(account).Error; err != nil { if err := ar.db.WithContext(ctx).Create(account).Error; err != nil {
return err return err
} }
return nil return nil
} }
func (ar *AccountRepository) Rename(id uint, newUsername string) error { func (ar *AccountRepository) Rename(ctx context.Context, id uint, newUsername string) error {
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil { if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("username", newUsername).Error; err != nil {
return err return err
} }
return nil return nil
} }
func (ar *AccountRepository) ChangePassword(id uint, newPassword string) error { func (ar *AccountRepository) ChangePassword(ctx context.Context, id uint, newPassword string) error {
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("password", newPassword).Error; err != nil { if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("password", newPassword).Error; err != nil {
return err return err
} }
return nil return nil
} }
func (ar *AccountRepository) FindByID(id uint) (*Account, error) { func (ar *AccountRepository) FindByID(ctx context.Context, id uint) (*Account, error) {
var account Account var account Account
if err := ar.db.First(&account, id).Error; err != nil { if err := ar.db.WithContext(ctx).First(&account, id).Error; err != nil {
return nil, err return nil, err
} }
return &account, nil return &account, nil
} }
func (ar *AccountRepository) FindByUsername(username string) (*Account, error) { func (ar *AccountRepository) FindByUsername(ctx context.Context, username string) (*Account, error) {
var account Account var account Account
if err := ar.db.Where("username = ?", username).First(&account).Error; err != nil { if err := ar.db.WithContext(ctx).Where("username = ?", username).First(&account).Error; err != nil {
return nil, err return nil, err
} }
return &account, nil return &account, nil
} }
func (ar *AccountRepository) Login(id uint, token string) error { func (ar *AccountRepository) Login(ctx context.Context, id uint, token string) error {
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("token", token).Error; err != nil { if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("token", token).Error; err != nil {
return err return err
} }
return nil return nil
} }
func (ar *AccountRepository) Logout(id uint, token string) error { func (ar *AccountRepository) Logout(ctx context.Context, id uint) error {
if err := ar.db.Model(&Account{}).Where("id = ?", id).Update("token", "").Error; err != nil { if err := ar.db.WithContext(ctx).Model(&Account{}).Where("id = ?", id).Update("token", "").Error; err != nil {
return err return err
} }
return nil return nil

View File

@@ -1,6 +1,7 @@
package account package account
import ( import (
"context"
"errors" "errors"
"feedsystem_video_go/internal/auth" "feedsystem_video_go/internal/auth"
@@ -15,27 +16,27 @@ func NewAccountService(accountRepository *AccountRepository) *AccountService {
return &AccountService{accountRepository: accountRepository} return &AccountService{accountRepository: accountRepository}
} }
func (as *AccountService) CreateAccount(account *Account) error { func (as *AccountService) CreateAccount(ctx context.Context, account *Account) error {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost) passwordHash, err := bcrypt.GenerateFromPassword([]byte(account.Password), bcrypt.DefaultCost)
if err != nil { if err != nil {
return err return err
} }
account.Password = string(passwordHash) account.Password = string(passwordHash)
if err := as.accountRepository.CreateAccount(account); err != nil { if err := as.accountRepository.CreateAccount(ctx, account); err != nil {
return err return err
} }
return nil return nil
} }
func (as *AccountService) Rename(accountID uint, newUsername string) error { func (as *AccountService) Rename(ctx context.Context, accountID uint, newUsername string) error {
if err := as.accountRepository.Rename(accountID, newUsername); err != nil { if err := as.accountRepository.Rename(ctx, accountID, newUsername); err != nil {
return err return err
} }
return nil return nil
} }
func (as *AccountService) ChangePassword(username, oldPassword, newPassword string) error { func (as *AccountService) ChangePassword(ctx context.Context, username, oldPassword, newPassword string) error {
account, err := as.FindByUsername(username) account, err := as.FindByUsername(ctx, username)
if err != nil { if err != nil {
return err return err
} }
@@ -46,30 +47,30 @@ func (as *AccountService) ChangePassword(username, oldPassword, newPassword stri
if err != nil { if err != nil {
return err return err
} }
if err := as.accountRepository.ChangePassword(account.ID, string(passwordHash)); err != nil { if err := as.accountRepository.ChangePassword(ctx, account.ID, string(passwordHash)); err != nil {
return err return err
} }
return nil return nil
} }
func (as *AccountService) FindByID(id uint) (*Account, error) { func (as *AccountService) FindByID(ctx context.Context, id uint) (*Account, error) {
if account, err := as.accountRepository.FindByID(id); err != nil { if account, err := as.accountRepository.FindByID(ctx, id); err != nil {
return nil, err return nil, err
} else { } else {
return account, nil return account, nil
} }
} }
func (as *AccountService) FindByUsername(username string) (*Account, error) { func (as *AccountService) FindByUsername(ctx context.Context, username string) (*Account, error) {
if account, err := as.accountRepository.FindByUsername(username); err != nil { if account, err := as.accountRepository.FindByUsername(ctx, username); err != nil {
return nil, err return nil, err
} else { } else {
return account, nil return account, nil
} }
} }
func (as *AccountService) Login(username, password string) (string, error) { func (as *AccountService) Login(ctx context.Context, username, password string) (string, error) {
account, err := as.FindByUsername(username) account, err := as.FindByUsername(ctx, username)
if err != nil { if err != nil {
return "", err return "", err
} }
@@ -81,20 +82,20 @@ func (as *AccountService) Login(username, password string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if err := as.accountRepository.Login(account.ID, token); err != nil { if err := as.accountRepository.Login(ctx, account.ID, token); err != nil {
return "", err return "", err
} }
return token, nil return token, nil
} }
func (as *AccountService) Logout(accountID uint) error { func (as *AccountService) Logout(ctx context.Context, accountID uint) error {
account, err := as.FindByID(accountID) account, err := as.FindByID(ctx, accountID)
if err != nil { if err != nil {
return err return err
} }
if account.Token == "" { if account.Token == "" {
return errors.New("account already logged out") return errors.New("account already logged out")
} }
return as.accountRepository.Logout(account.ID, account.Token) return as.accountRepository.Logout(ctx, account.ID)
} }

View File

@@ -35,7 +35,7 @@ func JWTAuth(accountRepo *account.AccountRepository) gin.HandlerFunc {
return return
} }
accountInfo, err := accountRepo.FindByID(claims.AccountID) accountInfo, err := accountRepo.FindByID(c.Request.Context(), claims.AccountID)
if err != nil || accountInfo.Token == "" || accountInfo.Token != tokenString { if err != nil || accountInfo.Token == "" || accountInfo.Token != tokenString {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "token has been revoked"}) c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "token has been revoked"})
return return