refactor: Update service and repository methods to use context
- Refactored multiple service and repository methods to accept context as a parameter, enhancing consistency and enabling better control over request lifecycles. - Updated handlers to utilize context in method calls, improving error handling and performance. - Cleaned up Dockerfile by removing unnecessary whitespace.
This commit is contained in:
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"carrotskin/internal/model"
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -16,55 +17,55 @@ func NewTokenRepository(db *gorm.DB) TokenRepository {
|
||||
return &tokenRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *tokenRepository) Create(token *model.Token) error {
|
||||
return r.db.Create(token).Error
|
||||
func (r *tokenRepository) Create(ctx context.Context, token *model.Token) error {
|
||||
return r.db.WithContext(ctx).Create(token).Error
|
||||
}
|
||||
|
||||
func (r *tokenRepository) FindByAccessToken(accessToken string) (*model.Token, error) {
|
||||
func (r *tokenRepository) FindByAccessToken(ctx context.Context, accessToken string) (*model.Token, error) {
|
||||
var token model.Token
|
||||
err := r.db.Where("access_token = ?", accessToken).First(&token).Error
|
||||
err := r.db.WithContext(ctx).Where("access_token = ?", accessToken).First(&token).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
func (r *tokenRepository) GetByUserID(userId int64) ([]*model.Token, error) {
|
||||
func (r *tokenRepository) GetByUserID(ctx context.Context, userId int64) ([]*model.Token, error) {
|
||||
var tokens []*model.Token
|
||||
err := r.db.Where("user_id = ?", userId).Find(&tokens).Error
|
||||
err := r.db.WithContext(ctx).Where("user_id = ?", userId).Find(&tokens).Error
|
||||
return tokens, err
|
||||
}
|
||||
|
||||
func (r *tokenRepository) GetUUIDByAccessToken(accessToken string) (string, error) {
|
||||
func (r *tokenRepository) GetUUIDByAccessToken(ctx context.Context, accessToken string) (string, error) {
|
||||
var token model.Token
|
||||
err := r.db.Select("profile_id").Where("access_token = ?", accessToken).First(&token).Error
|
||||
err := r.db.WithContext(ctx).Select("profile_id").Where("access_token = ?", accessToken).First(&token).Error
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token.ProfileId, nil
|
||||
}
|
||||
|
||||
func (r *tokenRepository) GetUserIDByAccessToken(accessToken string) (int64, error) {
|
||||
func (r *tokenRepository) GetUserIDByAccessToken(ctx context.Context, accessToken string) (int64, error) {
|
||||
var token model.Token
|
||||
err := r.db.Select("user_id").Where("access_token = ?", accessToken).First(&token).Error
|
||||
err := r.db.WithContext(ctx).Select("user_id").Where("access_token = ?", accessToken).First(&token).Error
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return token.UserID, nil
|
||||
}
|
||||
|
||||
func (r *tokenRepository) DeleteByAccessToken(accessToken string) error {
|
||||
return r.db.Where("access_token = ?", accessToken).Delete(&model.Token{}).Error
|
||||
func (r *tokenRepository) DeleteByAccessToken(ctx context.Context, accessToken string) error {
|
||||
return r.db.WithContext(ctx).Where("access_token = ?", accessToken).Delete(&model.Token{}).Error
|
||||
}
|
||||
|
||||
func (r *tokenRepository) DeleteByUserID(userId int64) error {
|
||||
return r.db.Where("user_id = ?", userId).Delete(&model.Token{}).Error
|
||||
func (r *tokenRepository) DeleteByUserID(ctx context.Context, userId int64) error {
|
||||
return r.db.WithContext(ctx).Where("user_id = ?", userId).Delete(&model.Token{}).Error
|
||||
}
|
||||
|
||||
func (r *tokenRepository) BatchDelete(accessTokens []string) (int64, error) {
|
||||
func (r *tokenRepository) BatchDelete(ctx context.Context, accessTokens []string) (int64, error) {
|
||||
if len(accessTokens) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
result := r.db.Where("access_token IN ?", accessTokens).Delete(&model.Token{})
|
||||
result := r.db.WithContext(ctx).Where("access_token IN ?", accessTokens).Delete(&model.Token{})
|
||||
return result.RowsAffected, result.Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user