github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/crud/crud.go (about) 1 package crud 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "os" 8 9 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" 11 ) 12 13 type crudValue interface { 14 base.Address | string 15 } 16 17 type Field[CV crudValue] struct { 18 Value CV 19 Updated bool 20 } 21 22 type NameCrud struct { 23 Address Field[base.Address] 24 Name Field[string] 25 Tags Field[string] 26 Source Field[string] 27 Symbol Field[string] 28 Decimals Field[string] 29 } 30 31 type Operation string 32 33 const ( 34 Create Operation = "create" 35 Update Operation = "update" 36 Delete Operation = "delete" 37 Undelete Operation = "undelete" 38 Remove Operation = "remove" 39 Autoname Operation = "autoname" 40 ) 41 42 func (cd *NameCrud) Validate(requireName bool) error { 43 if cd.Address.Value.IsZero() { 44 return errors.New("address is required in crud.Validate") 45 } 46 if requireName && cd.Name.Value == "" { 47 return errors.New("address is required in crud.Validate") 48 } 49 return nil 50 } 51 52 func NewNameCrud(requireName bool, r *http.Request) (*NameCrud, error) { 53 cd := &NameCrud{} 54 if r != nil { 55 if err := r.ParseForm(); err != nil { 56 return nil, err 57 } 58 59 if len(r.PostForm) == 0 { 60 return nil, errors.New("empty form") 61 } 62 63 cd = &NameCrud{ 64 Address: Field[base.Address]{ 65 Value: base.HexToAddress(r.PostForm.Get("address")), 66 Updated: r.PostForm.Has("address"), 67 }, 68 Name: Field[string]{ 69 Value: r.PostForm.Get("name"), 70 Updated: r.PostForm.Has("name"), 71 }, 72 Tags: Field[string]{ 73 Value: r.PostForm.Get("tags"), 74 Updated: r.PostForm.Has("tags"), 75 }, 76 Source: Field[string]{ 77 Value: r.PostForm.Get("source"), 78 Updated: r.PostForm.Has("source"), 79 }, 80 Symbol: Field[string]{ 81 Value: r.PostForm.Get("symbol"), 82 Updated: r.PostForm.Has("symbol"), 83 }, 84 Decimals: Field[string]{ 85 Value: r.PostForm.Get("decimals"), 86 Updated: r.PostForm.Has("decimals"), 87 }, 88 } 89 } else { 90 address, addressUpdated := os.LookupEnv("TB_NAME_ADDRESS") 91 cd.Address = Field[base.Address]{ 92 Value: base.HexToAddress(address), 93 Updated: addressUpdated, 94 } 95 name, nameUpdated := os.LookupEnv("TB_NAME_NAME") 96 cd.Name = Field[string]{ 97 Value: name, 98 Updated: nameUpdated, 99 } 100 tags, tagsUpdated := os.LookupEnv("TB_NAME_TAGS") 101 cd.Tags = Field[string]{ 102 Value: tags, 103 Updated: tagsUpdated, 104 } 105 source, sourceUpdated := os.LookupEnv("TB_NAME_SOURCE") 106 cd.Source = Field[string]{ 107 Value: source, 108 Updated: sourceUpdated, 109 } 110 symbol, symbolUpdated := os.LookupEnv("TB_NAME_SYMBOL") 111 cd.Symbol = Field[string]{ 112 Value: symbol, 113 Updated: symbolUpdated, 114 } 115 decimals, decimalsUpdated := os.LookupEnv("TB_NAME_DECIMALS") 116 cd.Decimals = Field[string]{ 117 Value: decimals, 118 Updated: decimalsUpdated, 119 } 120 } 121 122 if err := cd.Validate(requireName); err != nil { 123 return nil, err 124 } 125 126 return cd, nil 127 } 128 129 func CrudFromAddress(addr base.Address) *NameCrud { 130 cd := NameCrud{} 131 cd.Address = Field[base.Address]{ 132 Value: addr, 133 Updated: true, 134 } 135 return &cd 136 } 137 138 func CrudFromName(name types.Name) *NameCrud { 139 cd := NameCrud{} 140 cd.Address = Field[base.Address]{ 141 Value: name.Address, 142 Updated: true, 143 } 144 cd.Name = Field[string]{ 145 Value: name.Name, 146 Updated: true, 147 } 148 cd.Tags = Field[string]{ 149 Value: name.Tags, 150 Updated: true, 151 } 152 cd.Source = Field[string]{ 153 Value: name.Source, 154 Updated: true, 155 } 156 cd.Symbol = Field[string]{ 157 Value: name.Symbol, 158 Updated: true, 159 } 160 cd.Decimals = Field[string]{ 161 Value: fmt.Sprintf("%d", name.Decimals), 162 Updated: true, 163 } 164 return &cd 165 } 166 167 func (cd *NameCrud) SetEnv() { 168 os.Setenv("TB_NAME_ADDRESS", cd.Address.Value.String()) 169 os.Setenv("TB_NAME_NAME", cd.Name.Value) 170 os.Setenv("TB_NAME_TAGS", cd.Tags.Value) 171 os.Setenv("TB_NAME_SOURCE", cd.Source.Value) 172 os.Setenv("TB_NAME_SYMBOL", cd.Symbol.Value) 173 os.Setenv("TB_NAME_DECIMALS", cd.Decimals.Value) 174 } 175 176 func (cd *NameCrud) Unsetenv() { 177 os.Unsetenv("TB_NAME_ADDRESS") 178 os.Unsetenv("TB_NAME_NAME") 179 os.Unsetenv("TB_NAME_TAGS") 180 os.Unsetenv("TB_NAME_SOURCE") 181 os.Unsetenv("TB_NAME_SYMBOL") 182 os.Unsetenv("TB_NAME_DECIMALS") 183 }