github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/handle_crud.go (about)

     1  package namesPkg
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/crud"
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/names"
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    10  )
    11  
    12  func (opts *NamesOptions) HandleCrud(rCtx *output.RenderCtx) (err error) {
    13  	chain := opts.Globals.Chain
    14  
    15  	parts := opts.getType()
    16  	// TODO: Why do we do this if we don't use the result?
    17  	if _, err = names.LoadNamesMap(chain, parts, nil); err != nil {
    18  		return err
    19  	}
    20  
    21  	var name *types.Name
    22  	if opts.Create || opts.Update {
    23  		name, err = handleCreate(chain, opts.crudData)
    24  		if err != nil {
    25  			return
    26  		}
    27  	}
    28  	if opts.Delete {
    29  		name, err = handleDelete(chain, opts.crudData)
    30  		if err != nil {
    31  			return
    32  		}
    33  	}
    34  	if opts.Undelete {
    35  		name, err = handleUndelete(chain, opts.crudData)
    36  		if err != nil {
    37  			return
    38  		}
    39  	}
    40  	if opts.Remove {
    41  		name, err = handleRemove(chain, opts.crudData)
    42  		// Remove doesn't print the removed item
    43  		return
    44  	}
    45  
    46  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    47  		modelChan <- name
    48  	}
    49  
    50  	extraOpts := map[string]any{
    51  		"crud": true,
    52  	}
    53  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOptsWithExtra(extraOpts))
    54  }
    55  
    56  func handleCreate(chain string, data *crud.NameCrud) (name *types.Name, err error) {
    57  	var decimals uint64
    58  	if data.Decimals.Updated {
    59  		decimals, err = strconv.ParseUint(data.Decimals.Value, 10, 64)
    60  		if err != nil {
    61  			return
    62  		}
    63  	}
    64  
    65  	name = &types.Name{
    66  		Address:  data.Address.Value,
    67  		Name:     data.Name.Value,
    68  		Tags:     data.Tags.Value,
    69  		Source:   data.Source.Value,
    70  		Symbol:   data.Symbol.Value,
    71  		Decimals: decimals,
    72  		Deleted:  false,
    73  	}
    74  
    75  	return name, names.CreateName(names.DatabaseCustom, chain, name)
    76  }
    77  
    78  func handleDelete(chain string, data *crud.NameCrud) (*types.Name, error) {
    79  	return names.SetDeleted(names.DatabaseCustom, chain, data.Address.Value, true)
    80  }
    81  
    82  func handleUndelete(chain string, data *crud.NameCrud) (*types.Name, error) {
    83  	return names.SetDeleted(names.DatabaseCustom, chain, data.Address.Value, false)
    84  }
    85  
    86  func handleRemove(chain string, data *crud.NameCrud) (*types.Name, error) {
    87  	return names.RemoveName(names.DatabaseCustom, chain, data.Address.Value)
    88  }