From 0447ec4c4bbc9d61fb07b1b86e02f59d21b4dcae Mon Sep 17 00:00:00 2001 From: Michael McGuinness Date: Fri, 3 Jan 2025 16:00:36 +0000 Subject: [PATCH] extract common create and updatelogic --- database/queries/query.sql | 3 --- internal/contextFull/creator.go | 13 +------------ internal/contextFull/updator.go | 13 ++----------- internal/database/repository/query.sql.go | 11 ----------- internal/jsonExtractor/creator.go | 12 ------------ internal/jsonExtractor/updator.go | 12 +----------- internal/query/create.go | 5 ++++- internal/query/update.go | 23 +++++++++++++++-------- internal/queryProcessor/service.go | 4 +--- 9 files changed, 24 insertions(+), 72 deletions(-) diff --git a/database/queries/query.sql b/database/queries/query.sql index 5a27976a..f9eba802 100644 --- a/database/queries/query.sql +++ b/database/queries/query.sql @@ -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); diff --git a/internal/contextFull/creator.go b/internal/contextFull/creator.go index 3fcc90e6..d74cf825 100644 --- a/internal/contextFull/creator.go +++ b/internal/contextFull/creator.go @@ -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 -} diff --git a/internal/contextFull/updator.go b/internal/contextFull/updator.go index 5dfde987..5dd299e6 100644 --- a/internal/contextFull/updator.go +++ b/internal/contextFull/updator.go @@ -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 } diff --git a/internal/database/repository/query.sql.go b/internal/database/repository/query.sql.go index 6755aac9..36e81644 100644 --- a/internal/database/repository/query.sql.go +++ b/internal/database/repository/query.sql.go @@ -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 diff --git a/internal/jsonExtractor/creator.go b/internal/jsonExtractor/creator.go index 0298162a..e69f990e 100644 --- a/internal/jsonExtractor/creator.go +++ b/internal/jsonExtractor/creator.go @@ -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 -} diff --git a/internal/jsonExtractor/updator.go b/internal/jsonExtractor/updator.go index a9853824..2a689d03 100644 --- a/internal/jsonExtractor/updator.go +++ b/internal/jsonExtractor/updator.go @@ -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 } diff --git a/internal/query/create.go b/internal/query/create.go index 109ca74f..3a50b256 100644 --- a/internal/query/create.go +++ b/internal/query/create.go @@ -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 } diff --git a/internal/query/update.go b/internal/query/update.go index a23630a2..4c646f25 100644 --- a/internal/query/update.go +++ b/internal/query/update.go @@ -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, + } +} diff --git a/internal/queryProcessor/service.go b/internal/queryProcessor/service.go index 9addc10b..1790212a 100644 --- a/internal/queryProcessor/service.go +++ b/internal/queryProcessor/service.go @@ -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 {