98 lines
3.3 KiB
Go
98 lines
3.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"opencatd-open/team/dao"
|
|
"opencatd-open/team/model"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// 确保 TokenService 实现了 TokenServiceInterface 接口
|
|
var _ TokenService = (*TokenServiceImpl)(nil)
|
|
|
|
type TokenService interface {
|
|
Create(ctx context.Context, token *model.Token) error
|
|
GetByID(ctx context.Context, id int) (*model.Token, error)
|
|
GetByKey(ctx context.Context, key string) (*model.Token, error)
|
|
GetByUserID(ctx context.Context, userID int) (*model.Token, error)
|
|
Update(ctx context.Context, token *model.Token) error
|
|
UpdateWithCondition(ctx context.Context, token *model.Token, filters map[string]interface{}, updates map[string]interface{}) error
|
|
Delete(ctx context.Context, id int) error
|
|
Lists(ctx context.Context, offset, limit int) ([]model.Token, error)
|
|
ListsWithFilters(ctx context.Context, offset, limit int, filters map[string]interface{}) ([]model.Token, int64, error)
|
|
Disable(ctx context.Context, id int) error
|
|
Enable(ctx context.Context, id int) error
|
|
BatchDisable(ctx context.Context, ids []int) error
|
|
BatchEnable(ctx context.Context, ids []int) error
|
|
BatchDelete(ctx context.Context, ids []int) error
|
|
}
|
|
|
|
type TokenServiceImpl struct {
|
|
tokenRepo dao.TokenRepository
|
|
}
|
|
|
|
func NewTokenService(tokenRepo dao.TokenRepository) TokenService {
|
|
return &TokenServiceImpl{tokenRepo: tokenRepo}
|
|
}
|
|
|
|
func (s *TokenServiceImpl) Create(ctx context.Context, token *model.Token) error {
|
|
if token.Key == "" {
|
|
token.Key = "team-" + strings.ReplaceAll(uuid.New().String(), "-", "")
|
|
}
|
|
return s.tokenRepo.Create(ctx, token)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) GetByID(ctx context.Context, id int) (*model.Token, error) {
|
|
return s.tokenRepo.GetByID(ctx, id)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) GetByKey(ctx context.Context, key string) (*model.Token, error) {
|
|
return s.tokenRepo.GetByKey(ctx, key)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) GetByUserID(ctx context.Context, userID int) (*model.Token, error) {
|
|
return s.tokenRepo.GetByUserID(ctx, userID)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) Update(ctx context.Context, token *model.Token) error {
|
|
return s.tokenRepo.Update(ctx, token)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) UpdateWithCondition(ctx context.Context, token *model.Token, filters map[string]interface{}, updates map[string]interface{}) error {
|
|
return s.tokenRepo.UpdateWithCondition(ctx, token, filters, updates)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) Delete(ctx context.Context, id int) error {
|
|
return s.tokenRepo.Delete(ctx, id)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) Lists(ctx context.Context, offset, limit int) ([]model.Token, error) {
|
|
return s.tokenRepo.List(ctx, offset, limit)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) ListsWithFilters(ctx context.Context, offset, limit int, filters map[string]interface{}) ([]model.Token, int64, error) {
|
|
return s.tokenRepo.ListWithFilters(ctx, offset, limit, filters)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) Disable(ctx context.Context, id int) error {
|
|
return s.tokenRepo.Disable(ctx, id)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) Enable(ctx context.Context, id int) error {
|
|
return s.tokenRepo.Enable(ctx, id)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) BatchDisable(ctx context.Context, ids []int) error {
|
|
return s.tokenRepo.BatchDisable(ctx, ids)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) BatchEnable(ctx context.Context, ids []int) error {
|
|
return s.tokenRepo.BatchEnable(ctx, ids)
|
|
}
|
|
|
|
func (s *TokenServiceImpl) BatchDelete(ctx context.Context, ids []int) error {
|
|
return s.tokenRepo.BatchDelete(ctx, ids)
|
|
}
|