github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/statusupdate/repo.go (about) 1 package statusupdate 2 3 import ( 4 "context" 5 "database/sql" 6 "fmt" 7 8 "github.com/kyma-incubator/compass/components/director/internal/timestamp" 9 10 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 11 12 "github.com/pkg/errors" 13 ) 14 15 const ( 16 updateQuery = "UPDATE public.%s SET status_condition = 'CONNECTED', status_timestamp = $1 WHERE id = $2" 17 existsQuery = "SELECT 1 FROM public.%s WHERE id = $1 AND status_condition = 'CONNECTED'" 18 ) 19 20 type repository struct { 21 timestampGen timestamp.Generator 22 } 23 24 // NewRepository missing godoc 25 func NewRepository() *repository { 26 return &repository{timestampGen: timestamp.DefaultGenerator} 27 } 28 29 // UpdateStatus missing godoc 30 func (r *repository) UpdateStatus(ctx context.Context, id string, object WithStatusObject) error { 31 persist, err := persistence.FromCtx(ctx) 32 if err != nil { 33 return errors.Wrap(err, "while loading persistence from context") 34 } 35 36 stmt := fmt.Sprintf(updateQuery, object) 37 38 _, err = persist.ExecContext(ctx, stmt, r.timestampGen(), id) 39 40 if err != nil { 41 return errors.Wrap(err, fmt.Sprintf("while updating %s status", object)) 42 } 43 44 return nil 45 } 46 47 // IsConnected missing godoc 48 func (r *repository) IsConnected(ctx context.Context, id string, object WithStatusObject) (bool, error) { 49 persist, err := persistence.FromCtx(ctx) 50 if err != nil { 51 return false, errors.Wrap(err, "while loading persistence from context") 52 } 53 54 stmt := fmt.Sprintf(existsQuery, object) 55 56 var count int 57 err = persist.GetContext(ctx, &count, stmt, id) 58 if err != nil { 59 if err == sql.ErrNoRows { 60 return false, nil 61 } 62 return false, errors.Wrap(err, "while getting object from DB") 63 } 64 65 return true, nil 66 }