- Updated main.go to initialize email service and include it in the dependency injection container. - Refactored handlers to utilize context in service method calls, improving consistency and error handling. - Introduced new service options for upload, security, and captcha services, enhancing modularity and testability. - Removed unused repository implementations to streamline the codebase. This commit continues the effort to improve the architecture by ensuring all services are properly injected and utilized across the application.
71 lines
1.8 KiB
Go
71 lines
1.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"carrotskin/internal/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// tokenRepository TokenRepository的实现
|
|
type tokenRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewTokenRepository 创建TokenRepository实例
|
|
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) FindByAccessToken(accessToken string) (*model.Token, error) {
|
|
var token model.Token
|
|
err := r.db.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) {
|
|
var tokens []*model.Token
|
|
err := r.db.Where("user_id = ?", userId).Find(&tokens).Error
|
|
return tokens, err
|
|
}
|
|
|
|
func (r *tokenRepository) GetUUIDByAccessToken(accessToken string) (string, error) {
|
|
var token model.Token
|
|
err := r.db.Where("access_token = ?", accessToken).First(&token).Error
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return token.ProfileId, nil
|
|
}
|
|
|
|
func (r *tokenRepository) GetUserIDByAccessToken(accessToken string) (int64, error) {
|
|
var token model.Token
|
|
err := r.db.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) DeleteByUserID(userId int64) error {
|
|
return r.db.Where("user_id = ?", userId).Delete(&model.Token{}).Error
|
|
}
|
|
|
|
func (r *tokenRepository) BatchDelete(accessTokens []string) (int64, error) {
|
|
if len(accessTokens) == 0 {
|
|
return 0, nil
|
|
}
|
|
result := r.db.Where("access_token IN ?", accessTokens).Delete(&model.Token{})
|
|
return result.RowsAffected, result.Error
|
|
}
|