github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/mw/app/coin/description/create.go (about) 1 package description 2 3 import ( 4 "context" 5 "fmt" 6 7 descriptioncrud "github.com/NpoolPlatform/chain-middleware/pkg/crud/app/coin/description" 8 basetypes "github.com/NpoolPlatform/message/npool/basetypes/v1" 9 npool "github.com/NpoolPlatform/message/npool/chain/mw/v1/app/coin/description" 10 11 "github.com/NpoolPlatform/chain-middleware/pkg/db" 12 "github.com/NpoolPlatform/chain-middleware/pkg/db/ent" 13 redis2 "github.com/NpoolPlatform/go-service-framework/pkg/redis" 14 "github.com/NpoolPlatform/libent-cruder/pkg/cruder" 15 16 "github.com/google/uuid" 17 ) 18 19 func (h *Handler) CreateCoinDescription(ctx context.Context) (*npool.CoinDescription, error) { 20 lockKey := fmt.Sprintf( 21 "%v:%v:%v:%v", 22 basetypes.Prefix_PrefixCreateAppCoinDescription, 23 *h.AppID, 24 *h.CoinTypeID, 25 *h.UsedFor, 26 ) 27 if err := redis2.TryLock(lockKey, 0); err != nil { 28 return nil, err 29 } 30 defer func() { 31 _ = redis2.Unlock(lockKey) 32 }() 33 34 h.Conds = &descriptioncrud.Conds{ 35 AppID: &cruder.Cond{Op: cruder.EQ, Val: *h.AppID}, 36 CoinTypeID: &cruder.Cond{Op: cruder.EQ, Val: *h.CoinTypeID}, 37 UsedFor: &cruder.Cond{Op: cruder.EQ, Val: *h.UsedFor}, 38 } 39 exist, err := h.ExistCoinDescriptionConds(ctx) 40 if err != nil { 41 return nil, err 42 } 43 if exist { 44 return nil, fmt.Errorf("description exist") 45 } 46 47 id := uuid.New() 48 if h.EntID == nil { 49 h.EntID = &id 50 } 51 52 err = db.WithClient(ctx, func(_ctx context.Context, cli *ent.Client) error { 53 if _, err := descriptioncrud.CreateSet( 54 cli.CoinDescription.Create(), 55 &descriptioncrud.Req{ 56 ID: h.ID, 57 EntID: h.EntID, 58 AppID: h.AppID, 59 CoinTypeID: h.CoinTypeID, 60 UsedFor: h.UsedFor, 61 Title: h.Title, 62 Message: h.Message, 63 }, 64 ).Save(_ctx); err != nil { 65 return err 66 } 67 return nil 68 }) 69 if err != nil { 70 return nil, err 71 } 72 return h.GetCoinDescription(ctx) 73 }