github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/formationtemplateconstraintreferences/resolver.go (about)

     1  package formationtemplateconstraintreferences
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/model"
     7  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/log"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/persistence"
    10  )
    11  
    12  //go:generate mockery --exported --name=constraintReferenceConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    13  type constraintReferenceConverter interface {
    14  	ToModel(in *graphql.ConstraintReference) *model.FormationTemplateConstraintReference
    15  	ToGraphql(in *model.FormationTemplateConstraintReference) *graphql.ConstraintReference
    16  	ToEntity(in *model.FormationTemplateConstraintReference) *Entity
    17  	FromEntity(e *Entity) *model.FormationTemplateConstraintReference
    18  }
    19  
    20  //go:generate mockery --exported --name=constraintReferenceService --output=automock --outpkg=automock --case=underscore --disable-version-string
    21  type constraintReferenceService interface {
    22  	Create(ctx context.Context, in *model.FormationTemplateConstraintReference) error
    23  	Delete(ctx context.Context, constraintID, formationTemplateID string) error
    24  }
    25  
    26  // Resolver is the FormationConstraint resolver
    27  type Resolver struct {
    28  	transact persistence.Transactioner
    29  
    30  	svc       constraintReferenceService
    31  	converter constraintReferenceConverter
    32  }
    33  
    34  // NewResolver creates FormationConstraint resolver
    35  func NewResolver(transact persistence.Transactioner, converter constraintReferenceConverter, svc constraintReferenceService) *Resolver {
    36  	return &Resolver{
    37  		transact:  transact,
    38  		converter: converter,
    39  		svc:       svc,
    40  	}
    41  }
    42  
    43  // AttachConstraintToFormationTemplate creates a FormationTemplateConstraintReference using `in`
    44  func (r *Resolver) AttachConstraintToFormationTemplate(ctx context.Context, constraintID, formationTemplateID string) (*graphql.ConstraintReference, error) {
    45  	tx, err := r.transact.Begin()
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    50  
    51  	ctx = persistence.SaveToContext(ctx, tx)
    52  
    53  	in := &graphql.ConstraintReference{
    54  		ConstraintID:        constraintID,
    55  		FormationTemplateID: formationTemplateID,
    56  	}
    57  	err = r.svc.Create(ctx, r.converter.ToModel(in))
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	if err = tx.Commit(); err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	log.C(ctx).Infof("Successfully created an Formation Template Constraint Reference for Constraint with ID %q and Formation Template with ID %q", in.ConstraintID, in.FormationTemplateID)
    67  
    68  	return in, nil
    69  }
    70  
    71  // DetachConstraintFromFormationTemplate deletes the FormationTemplateConstraintReference matching ID `id`
    72  func (r *Resolver) DetachConstraintFromFormationTemplate(ctx context.Context, constraintID, formationTemplateID string) (*graphql.ConstraintReference, error) {
    73  	tx, err := r.transact.Begin()
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	defer r.transact.RollbackUnlessCommitted(ctx, tx)
    78  
    79  	ctx = persistence.SaveToContext(ctx, tx)
    80  
    81  	err = r.svc.Delete(ctx, constraintID, formationTemplateID)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	err = tx.Commit()
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	log.C(ctx).Infof("Successfully deleted Formation Template Constraint Reference for Constraint with ID %q and Formation Template with ID %q", constraintID, formationTemplateID)
    92  
    93  	return &graphql.ConstraintReference{ConstraintID: constraintID, FormationTemplateID: formationTemplateID}, nil
    94  }