github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/blockchain/api_tx.go (about) 1 package blockchain 2 3 import ( 4 "context" 5 "time" 6 7 confid "github.com/machinefi/w3bstream/pkg/depends/conf/id" 8 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 9 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 10 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/datatypes" 11 "github.com/machinefi/w3bstream/pkg/errors/status" 12 "github.com/machinefi/w3bstream/pkg/models" 13 "github.com/machinefi/w3bstream/pkg/types" 14 ) 15 16 type CreateChainTxReq struct { 17 ProjectName string `json:"-"` 18 models.ChainTxInfo 19 } 20 21 func CreateChainTx(ctx context.Context, r *CreateChainTxReq) (*models.ChainTx, error) { 22 d := types.MustMonitorDBExecutorFromContext(ctx) 23 idg := confid.MustSFIDGeneratorFromContext(ctx) 24 25 if err := checkChainID(ctx, r.ChainID); err != nil { 26 return nil, err 27 } 28 29 m := &models.ChainTx{ 30 RelChainTx: models.RelChainTx{ChainTxID: idg.MustGenSFID()}, 31 ChainTxData: models.ChainTxData{ 32 ProjectName: r.ProjectName, 33 Uniq: chainUniqFlag, 34 Finished: datatypes.FALSE, 35 ChainTxInfo: r.ChainTxInfo, 36 }, 37 } 38 if err := m.Create(d); err != nil { 39 if sqlx.DBErr(err).IsConflict() { 40 return nil, status.ChainTxConflict 41 } 42 return nil, status.DatabaseError.StatusErr().WithDesc(err.Error()) 43 } 44 return m, nil 45 } 46 47 func GetChainTxBySFID(ctx context.Context, id types.SFID) (*models.ChainTx, error) { 48 d := types.MustMonitorDBExecutorFromContext(ctx) 49 50 m := &models.ChainTx{RelChainTx: models.RelChainTx{ChainTxID: id}} 51 if err := m.FetchByChainTxID(d); err != nil { 52 if sqlx.DBErr(err).IsNotFound() { 53 return nil, status.ChainTxNotFound 54 } 55 return nil, status.DatabaseError.StatusErr().WithDesc(err.Error()) 56 } 57 return m, nil 58 } 59 60 func ListChainTxBySFIDs(ctx context.Context, ids []types.SFID) ([]models.ChainTx, error) { 61 d := types.MustMonitorDBExecutorFromContext(ctx) 62 m := &models.ChainTx{} 63 64 data, err := m.List(d, m.ColChainTxID().In(ids)) 65 if err != nil { 66 return nil, status.DatabaseError.StatusErr().WithDesc(err.Error()) 67 } 68 return data, nil 69 } 70 71 func RemoveChainTxBySFID(ctx context.Context, id types.SFID) error { 72 d := types.MustMonitorDBExecutorFromContext(ctx) 73 74 m := &models.ChainTx{RelChainTx: models.RelChainTx{ChainTxID: id}} 75 if err := m.DeleteByChainTxID(d); err != nil { 76 return status.DatabaseError.StatusErr().WithDesc(err.Error()) 77 } 78 return nil 79 } 80 81 func BatchUpdateChainTxPausedBySFIDs(ctx context.Context, ids []types.SFID, s datatypes.Bool) error { 82 d := types.MustMonitorDBExecutorFromContext(ctx) 83 m := &models.ChainTx{ 84 ChainTxData: models.ChainTxData{ 85 ChainTxInfo: models.ChainTxInfo{ 86 Paused: s, 87 }, 88 }, 89 } 90 91 expr := builder.Update(d.T(m)).Set( 92 m.ColPaused().ValueBy(s), 93 m.ColUpdatedAt().ValueBy(types.Timestamp{Time: time.Now()}), 94 ).Where(m.ColChainTxID().In(ids)) 95 96 if _, err := d.Exec(expr); err != nil { 97 return status.DatabaseError.StatusErr().WithDesc(err.Error()) 98 } 99 return nil 100 }