2025-11-28 23:30:49 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"carrotskin/internal/model"
|
2025-12-03 15:27:12 +08:00
|
|
|
"context"
|
2025-12-02 22:52:33 +08:00
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2025-11-28 23:30:49 +08:00
|
|
|
)
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
// tokenRepository TokenRepository的实现
|
|
|
|
|
type tokenRepository struct {
|
|
|
|
|
db *gorm.DB
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-02 22:52:33 +08:00
|
|
|
// NewTokenRepository 创建TokenRepository实例
|
|
|
|
|
func NewTokenRepository(db *gorm.DB) TokenRepository {
|
|
|
|
|
return &tokenRepository{db: db}
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) Create(ctx context.Context, token *model.Token) error {
|
|
|
|
|
return r.db.WithContext(ctx).Create(token).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) FindByAccessToken(ctx context.Context, accessToken string) (*model.Token, error) {
|
2025-12-02 10:33:19 +08:00
|
|
|
var token model.Token
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("access_token = ?", accessToken).First(&token).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2025-12-02 10:33:19 +08:00
|
|
|
return &token, nil
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) GetByUserID(ctx context.Context, userId int64) ([]*model.Token, error) {
|
2025-12-02 22:52:33 +08:00
|
|
|
var tokens []*model.Token
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Where("user_id = ?", userId).Find(&tokens).Error
|
2025-12-02 22:52:33 +08:00
|
|
|
return tokens, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) GetUUIDByAccessToken(ctx context.Context, accessToken string) (string, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var token model.Token
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Select("profile_id").Where("access_token = ?", accessToken).First(&token).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return token.ProfileId, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) GetUserIDByAccessToken(ctx context.Context, accessToken string) (int64, error) {
|
2025-11-28 23:30:49 +08:00
|
|
|
var token model.Token
|
2025-12-03 15:27:12 +08:00
|
|
|
err := r.db.WithContext(ctx).Select("user_id").Where("access_token = ?", accessToken).First(&token).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return 0, err
|
|
|
|
|
}
|
|
|
|
|
return token.UserID, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) DeleteByAccessToken(ctx context.Context, accessToken string) error {
|
|
|
|
|
return r.db.WithContext(ctx).Where("access_token = ?", accessToken).Delete(&model.Token{}).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) DeleteByUserID(ctx context.Context, userId int64) error {
|
|
|
|
|
return r.db.WithContext(ctx).Where("user_id = ?", userId).Delete(&model.Token{}).Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 15:27:12 +08:00
|
|
|
func (r *tokenRepository) BatchDelete(ctx context.Context, accessTokens []string) (int64, error) {
|
2025-12-02 22:52:33 +08:00
|
|
|
if len(accessTokens) == 0 {
|
|
|
|
|
return 0, nil
|
|
|
|
|
}
|
2025-12-03 15:27:12 +08:00
|
|
|
result := r.db.WithContext(ctx).Where("access_token IN ?", accessTokens).Delete(&model.Token{})
|
2025-12-02 22:52:33 +08:00
|
|
|
return result.RowsAffected, result.Error
|
2025-11-28 23:30:49 +08:00
|
|
|
}
|