优化
This commit is contained in:
@@ -2,15 +2,71 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/service/dto"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ExportService struct{ uc *biz.ExportUsecase }
|
||||
var (
|
||||
ErrExportTokenInvalid = errors.New("export token invalid")
|
||||
ErrExportTokenMalformed = errors.New("export token malformed")
|
||||
ErrExportTokenType = errors.New("export token type mismatch")
|
||||
)
|
||||
|
||||
func NewExportService(uc *biz.ExportUsecase) *ExportService { return &ExportService{uc: uc} }
|
||||
type ExportService struct {
|
||||
uc *biz.ExportUsecase
|
||||
cache biz.Cache
|
||||
}
|
||||
|
||||
type ExportToken struct {
|
||||
TemplateID string
|
||||
Params map[string]string
|
||||
Blank bool
|
||||
}
|
||||
|
||||
type exportTokenCache struct {
|
||||
TemplateID string `json:"templateID"`
|
||||
Params map[string]string `json:"params"`
|
||||
Blank bool `json:"blank"`
|
||||
}
|
||||
|
||||
func NewExportService(uc *biz.ExportUsecase, cache biz.Cache) *ExportService {
|
||||
return &ExportService{uc: uc, cache: cache}
|
||||
}
|
||||
|
||||
func (s *ExportService) IssueToken(ctx context.Context, templateID string, params map[string]string, blank bool) (string, error) {
|
||||
token := strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
raw, err := json.Marshal(exportTokenCache{TemplateID: templateID, Params: params, Blank: blank})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = s.cache.Set(ctx, "export:"+token, string(raw), 30*time.Minute); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (s *ExportService) ConsumeToken(ctx context.Context, token string, expectBlank bool) (*ExportToken, error) {
|
||||
raw, ok, err := s.cache.Get(ctx, "export:"+token)
|
||||
if err != nil || !ok {
|
||||
return nil, ErrExportTokenInvalid
|
||||
}
|
||||
var value exportTokenCache
|
||||
if json.Unmarshal([]byte(raw), &value) != nil {
|
||||
return nil, ErrExportTokenMalformed
|
||||
}
|
||||
if value.Blank != expectBlank {
|
||||
return nil, ErrExportTokenType
|
||||
}
|
||||
_ = s.cache.Delete(ctx, "export:"+token)
|
||||
return &ExportToken{TemplateID: value.TemplateID, Params: value.Params, Blank: value.Blank}, nil
|
||||
}
|
||||
func exportTemplateDomain(value *dto.ExportTemplateRequest) *biz.ExportTemplate {
|
||||
out := &biz.ExportTemplate{ID: value.ID, DBName: value.DBName, Name: value.Name, TableName: value.TableName, TemplateID: value.TemplateID, TemplateInfo: value.TemplateInfo, SQL: value.SQL, ImportSQL: value.ImportSQL, Limit: value.Limit, Order: value.Order}
|
||||
for _, item := range value.Conditions {
|
||||
|
||||
Reference in New Issue
Block a user