fa95d733ca
Add short tests and Tidy internal directories * complete the tasks
61 lines
1.4 KiB
Go
61 lines
1.4 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
queryprocessor "queryorchestration/internal/query/processor"
|
|
contextfull "queryorchestration/internal/query/types/contextFull"
|
|
jsonextractor "queryorchestration/internal/query/types/jsonExtractor"
|
|
)
|
|
|
|
func (s *Service) Update(ctx context.Context, entity *queryprocessor.Update) error {
|
|
current, err := s.Get(ctx, entity.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
validator, err := s.getUpdator(current.Type)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = validator.Validate(ctx, ParseQuery(current), entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.submitUpdate(ctx, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) submitUpdate(ctx context.Context, entity *queryprocessor.Update) error {
|
|
// TODO - generate new entity
|
|
// TODO - submit update - id, type, activeversion, requiredQueryId, Config
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) getUpdator(qType queryprocessor.Type) (queryprocessor.Updator, error) {
|
|
switch qType {
|
|
case queryprocessor.TypeJsonExtractor:
|
|
return jsonextractor.NewUpdator(s.db), nil
|
|
case queryprocessor.TypeContextFull:
|
|
return contextfull.NewUpdator(s.db), nil
|
|
default:
|
|
return nil, fmt.Errorf("attempting to process invalid query type")
|
|
}
|
|
}
|
|
|
|
func ParseQuery(q *Query) *queryprocessor.Query {
|
|
return &queryprocessor.Query{
|
|
ID: q.ID,
|
|
Type: q.Type,
|
|
Version: q.ActiveVersion,
|
|
RequiredQueryIDs: q.RequiredQueryIDs,
|
|
Config: q.Config,
|
|
}
|
|
}
|