github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/certsubjectmapping/repository.go (about) 1 package certsubjectmapping 2 3 import ( 4 "context" 5 6 "github.com/kyma-incubator/compass/components/director/internal/model" 7 "github.com/kyma-incubator/compass/components/director/internal/repo" 8 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 9 "github.com/kyma-incubator/compass/components/director/pkg/log" 10 "github.com/kyma-incubator/compass/components/director/pkg/resource" 11 "github.com/pkg/errors" 12 ) 13 14 const ( 15 tableName string = `public.cert_subject_mapping` 16 idColumn string = "id" 17 ) 18 19 var ( 20 idTableColumns = []string{"id"} 21 updatableTableColumns = []string{"subject", "consumer_type", "internal_consumer_id", "tenant_access_levels"} 22 tableColumns = append(idTableColumns, updatableTableColumns...) 23 ) 24 25 // entityConverter converts between the internal model and entity 26 //go:generate mockery --exported --name=entityConverter --output=automock --outpkg=automock --case=underscore --disable-version-string 27 type entityConverter interface { 28 ToEntity(in *model.CertSubjectMapping) (*Entity, error) 29 FromEntity(entity *Entity) (*model.CertSubjectMapping, error) 30 } 31 32 type repository struct { 33 creator repo.CreatorGlobal 34 existQuerierGlobal repo.ExistQuerierGlobal 35 singleGetterGlobal repo.SingleGetterGlobal 36 pageableQuerierGlobal repo.PageableQuerierGlobal 37 updaterGlobal repo.UpdaterGlobal 38 deleterGlobal repo.DeleterGlobal 39 listerGlobal repo.ListerGlobal 40 conv entityConverter 41 } 42 43 // NewRepository creates a new CertSubjectMapping repository 44 func NewRepository(conv entityConverter) *repository { 45 return &repository{ 46 creator: repo.NewCreatorGlobal(resource.CertSubjectMapping, tableName, tableColumns), 47 existQuerierGlobal: repo.NewExistQuerierGlobal(resource.CertSubjectMapping, tableName), 48 singleGetterGlobal: repo.NewSingleGetterGlobal(resource.CertSubjectMapping, tableName, tableColumns), 49 pageableQuerierGlobal: repo.NewPageableQuerierGlobal(resource.CertSubjectMapping, tableName, tableColumns), 50 updaterGlobal: repo.NewUpdaterGlobal(resource.CertSubjectMapping, tableName, updatableTableColumns, idTableColumns), 51 deleterGlobal: repo.NewDeleterGlobal(resource.CertSubjectMapping, tableName), 52 listerGlobal: repo.NewListerGlobal(resource.CertSubjectMapping, tableName, tableColumns), 53 conv: conv, 54 } 55 } 56 57 // Create creates a new certificate subject mapping in the database with the fields from the model 58 func (r *repository) Create(ctx context.Context, model *model.CertSubjectMapping) error { 59 if model == nil { 60 return apperrors.NewInternalError("model can not be empty") 61 } 62 63 log.C(ctx).Debugf("Converting certificate subject mapping with ID: %s to entity", model.ID) 64 entity, err := r.conv.ToEntity(model) 65 if err != nil { 66 return errors.Wrapf(err, "while converting certificate subject mapping with ID: %s", model.ID) 67 } 68 69 log.C(ctx).Debugf("Persisting certificate mapping with ID: %s and subject: %s to DB", model.ID, model.Subject) 70 return r.creator.Create(ctx, entity) 71 } 72 73 // Get queries for a single certificate subject mapping matching by a given ID 74 func (r *repository) Get(ctx context.Context, id string) (*model.CertSubjectMapping, error) { 75 log.C(ctx).Debugf("Getting certificate mapping by ID: %s from DB", id) 76 var entity Entity 77 if err := r.singleGetterGlobal.GetGlobal(ctx, repo.Conditions{repo.NewEqualCondition("id", id)}, repo.NoOrderBy, &entity); err != nil { 78 return nil, err 79 } 80 81 result, err := r.conv.FromEntity(&entity) 82 if err != nil { 83 return nil, errors.Wrapf(err, "while converting certificate subject mapping with ID: %s", id) 84 } 85 86 return result, nil 87 } 88 89 // Update updates the certificate subject mapping with the provided input model 90 func (r *repository) Update(ctx context.Context, model *model.CertSubjectMapping) error { 91 if model == nil { 92 return apperrors.NewInternalError("model can not be empty") 93 } 94 95 log.C(ctx).Debugf("Converting certificate subject mapping with ID: %s to entity", model.ID) 96 entity, err := r.conv.ToEntity(model) 97 if err != nil { 98 return errors.Wrapf(err, "while converting certificate subject mapping with ID: %s", model.ID) 99 } 100 101 log.C(ctx).Debugf("Updating certificate mapping with ID: %s and subject: %s", model.ID, model.Subject) 102 return r.updaterGlobal.UpdateSingleGlobal(ctx, entity) 103 } 104 105 // Delete deletes a certificate subject mapping with given ID 106 func (r *repository) Delete(ctx context.Context, id string) error { 107 log.C(ctx).Debugf("Deleting certificate mapping with ID: %s from DB", id) 108 return r.deleterGlobal.DeleteOneGlobal(ctx, repo.Conditions{repo.NewEqualCondition("id", id)}) 109 } 110 111 // Exists check if a certificate subject mapping with given ID exists 112 func (r *repository) Exists(ctx context.Context, id string) (bool, error) { 113 log.C(ctx).Debugf("Check if certificate mapping with ID: %s exists", id) 114 return r.existQuerierGlobal.ExistsGlobal(ctx, repo.Conditions{repo.NewEqualCondition("id", id)}) 115 } 116 117 // List queries for all certificate subject mappings sorted by ID and paginated by the pageSize and cursor parameters 118 func (r *repository) List(ctx context.Context, pageSize int, cursor string) (*model.CertSubjectMappingPage, error) { 119 log.C(ctx).Debug("Listing certificate subject mappings from DB") 120 var entityCollection EntityCollection 121 page, totalCount, err := r.pageableQuerierGlobal.ListGlobal(ctx, pageSize, cursor, idColumn, &entityCollection) 122 if err != nil { 123 return nil, err 124 } 125 126 items := make([]*model.CertSubjectMapping, 0, len(entityCollection)) 127 128 for _, entity := range entityCollection { 129 result, err := r.conv.FromEntity(entity) 130 if err != nil { 131 return nil, errors.Wrapf(err, "while converting certificate subject mapping with ID: %s", entity.ID) 132 } 133 134 items = append(items, result) 135 } 136 137 return &model.CertSubjectMappingPage{ 138 Data: items, 139 TotalCount: totalCount, 140 PageInfo: page, 141 }, nil 142 }