github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/modules/wasmlog/wasm_log.go (about)

     1  package wasmlog
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/sqlx"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/statusx"
    10  	"github.com/machinefi/w3bstream/pkg/errors/status"
    11  	"github.com/machinefi/w3bstream/pkg/models"
    12  	"github.com/machinefi/w3bstream/pkg/types"
    13  )
    14  
    15  func RemoveBySFID(ctx context.Context, id types.SFID) error {
    16  	d := types.MustMgrDBExecutorFromContext(ctx)
    17  	m := &models.WasmLog{RelWasmLog: models.RelWasmLog{WasmLogID: id}}
    18  
    19  	if err := m.DeleteByWasmLogID(d); err != nil {
    20  		return status.DatabaseError.StatusErr().
    21  			WithDesc(errors.Wrap(err, id.String()).Error())
    22  	}
    23  	return nil
    24  }
    25  
    26  func Remove(ctx context.Context, r *CondArgs) error {
    27  	var (
    28  		d = types.MustMgrDBExecutorFromContext(ctx)
    29  		m = &models.WasmLog{}
    30  
    31  		err error
    32  		lst []models.WasmLog
    33  	)
    34  
    35  	return sqlx.NewTasks(d).With(
    36  		func(d sqlx.DBExecutor) error {
    37  			lst, err = m.List(d, r.Condition())
    38  			if err != nil {
    39  				return status.DatabaseError.StatusErr().WithDesc(err.Error())
    40  			}
    41  			return nil
    42  		},
    43  		func(d sqlx.DBExecutor) error {
    44  			ctx := types.WithMgrDBExecutor(ctx, d)
    45  			summary := statusx.ErrorFields{}
    46  			for i := range lst {
    47  				v := &lst[i]
    48  				if err = RemoveBySFID(ctx, v.WasmLogID); err != nil {
    49  					se := statusx.FromErr(err)
    50  					summary = append(summary, &statusx.ErrorField{
    51  						In:    v.WasmLogID.String(),
    52  						Field: se.Key,
    53  						Msg:   se.Desc,
    54  					})
    55  				}
    56  			}
    57  			if len(summary) > 0 {
    58  				return status.BatchRemoveWasmLogFailed.StatusErr().
    59  					AppendErrorFields(summary...)
    60  			}
    61  			return nil
    62  		},
    63  	).Do()
    64  }