extract common create and updatelogic

This commit is contained in:
Michael McGuinness
2025-01-03 16:00:36 +00:00
parent 9f505dd94c
commit 0447ec4c4b
9 changed files with 24 additions and 72 deletions
-3
View File
@@ -1,9 +1,6 @@
-- name: GetQueryConfig :one
SELECT id, config FROM queryConfigs where queryId = $1 and addedVersion >= $2 and COALESCE(removedVersion, $2 - 1) < $2;
-- name: GetQueryType :one
SELECT type FROM queries where id = $1;
-- name: DeprecateQuery :exec
INSERT INTO queryDeprecations (queryId) VALUES ($1);
+1 -12
View File
@@ -4,8 +4,6 @@ import (
"context"
"queryorchestration/internal/database/repository"
queryprocessor "queryorchestration/internal/queryProcessor"
"github.com/google/uuid"
)
type Creator struct {
@@ -18,15 +16,6 @@ func NewCreator(db *repository.Queries) Creator {
func (s Creator) Validate(ctx context.Context, entity *queryprocessor.Create) error {
// TODO
// Type, RequiredQueryIDs, Config
return nil
}
func (s Creator) Create(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
err := s.Validate(ctx, entity)
if err != nil {
return uuid.Nil, err
}
// TODO
return uuid.Nil, nil
}
+2 -11
View File
@@ -14,17 +14,8 @@ func NewUpdator(db *repository.Queries) Updator {
return Updator{db}
}
func (s Updator) Validate(ctx context.Context, entity *queryprocessor.Update) error {
// TODO
return nil
}
func (s Updator) Update(ctx context.Context, entity *queryprocessor.Update) error {
err := s.Validate(ctx, entity)
if err != nil {
return err
}
func (s Updator) Validate(ctx context.Context, current *queryprocessor.Query, entity *queryprocessor.Update) error {
// TODO
// Type, RequiredQueryIDs, Config
return nil
}
-11
View File
@@ -77,17 +77,6 @@ func (q *Queries) GetQueryConfig(ctx context.Context, arg GetQueryConfigParams)
return i, err
}
const getQueryType = `-- name: GetQueryType :one
SELECT type FROM queries where id = $1
`
func (q *Queries) GetQueryType(ctx context.Context, id pgtype.UUID) (Querytype, error) {
row := q.db.QueryRow(ctx, getQueryType, id)
var type_ Querytype
err := row.Scan(&type_)
return type_, err
}
const isQueryDeprecated = `-- name: IsQueryDeprecated :one
SELECT EXISTS (
SELECT 1 FROM queryDeprecations where queryId = $1 and removedAt is not null
-12
View File
@@ -4,8 +4,6 @@ import (
"context"
"queryorchestration/internal/database/repository"
queryprocessor "queryorchestration/internal/queryProcessor"
"github.com/google/uuid"
)
type Creator struct {
@@ -20,13 +18,3 @@ func (s Creator) Validate(ctx context.Context, entity *queryprocessor.Create) er
// TODO
return nil
}
func (s Creator) Create(ctx context.Context, entity *queryprocessor.Create) (uuid.UUID, error) {
err := s.Validate(ctx, entity)
if err != nil {
return uuid.Nil, err
}
// TODO
return uuid.Nil, nil
}
+1 -11
View File
@@ -14,17 +14,7 @@ func NewUpdator(db *repository.Queries) Updator {
return Updator{db}
}
func (s Updator) Validate(ctx context.Context, entity *queryprocessor.Update) error {
// TODO
return nil
}
func (s Updator) Update(ctx context.Context, entity *queryprocessor.Update) error {
err := s.Validate(ctx, entity)
if err != nil {
return err
}
func (s Updator) Validate(ctx context.Context, current *queryprocessor.Query, entity *queryprocessor.Update) error {
// TODO
return nil
}
+4 -1
View File
@@ -16,11 +16,14 @@ func (s *Service) Create(ctx context.Context, entity *queryprocessor.Create) (uu
return uuid.Nil, err
}
id, err := validator.Create(ctx, entity)
err = validator.Validate(ctx, entity)
if err != nil {
return uuid.Nil, err
}
// TODO - submit create - type, requiredids, config
id := uuid.New()
return id, nil
}
+15 -8
View File
@@ -4,31 +4,28 @@ import (
"context"
"fmt"
contextfull "queryorchestration/internal/contextFull"
"queryorchestration/internal/database"
jsonextractor "queryorchestration/internal/jsonExtractor"
queryprocessor "queryorchestration/internal/queryProcessor"
)
func (s *Service) Update(ctx context.Context, entity *queryprocessor.Update) error {
dbType, err := s.db.GetQueryType(ctx, database.MustToDBUUID(entity.ID))
current, err := s.Get(ctx, entity.ID)
if err != nil {
return err
}
qType, err := queryprocessor.ParseDBType(dbType)
validator, err := s.getUpdator(current.Type)
if err != nil {
return err
}
validator, err := s.getUpdator(qType)
err = validator.Validate(ctx, ParseQuery(current), entity)
if err != nil {
return err
}
err = validator.Update(ctx, entity)
if err != nil {
return err
}
// TODO - generate new entity
// TODO - submit update - id, type, activeversion, requiredQueryId, Config
return nil
}
@@ -43,3 +40,13 @@ func (s *Service) getUpdator(qType queryprocessor.Type) (queryprocessor.Updator,
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,
}
}
+1 -3
View File
@@ -36,12 +36,10 @@ type Query struct {
type Creator interface {
Validate(ctx context.Context, entity *Create) error
Create(ctx context.Context, entity *Create) (uuid.UUID, error)
}
type Updator interface {
Validate(ctx context.Context, entity *Update) error
Update(ctx context.Context, entity *Update) error
Validate(ctx context.Context, current *Query, entity *Update) error
}
type Processor interface {