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

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