Files
novatask/internal/model/nh_wallet_model.go
2024-12-27 18:06:13 +08:00

58 lines
1.7 KiB
Go
Executable File

package model
import (
"context"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"nova_task/internal/pkg/aptos"
)
var _ NhWalletModel = (*customNhWalletModel)(nil)
type (
// NhWalletModel is an interface to be customized, add more methods here,
// and implement the added methods in customNhWalletModel.
NhWalletModel interface {
nhWalletModel
withSession(session sqlx.Session) NhWalletModel
FindUidByAddress(ctx context.Context, address string) (uint, error)
FindAddressByUid(ctx context.Context, uid uint) (string, error)
}
customNhWalletModel struct {
*defaultNhWalletModel
}
)
func (m *customNhWalletModel) FindAddressByUid(ctx context.Context, uid uint) (string, error) {
query := fmt.Sprintf("select `address` from %s where `uid` = ? and `status` = 0 limit 1", m.table)
var address string
err := m.conn.QueryRowCtx(ctx, &address, query, uid)
if errors.Is(err, sqlx.ErrNotFound) {
return "", ErrNotFound
}
return address, err
}
func (m *customNhWalletModel) FindUidByAddress(ctx context.Context, address string) (uint, error) {
query := fmt.Sprintf("select `uid` from %s where `address` = ? or `address` = ? limit 1", m.table)
var uid uint
err := m.conn.QueryRowCtx(ctx, &uid, query, address, aptos.StrPadAptosAddress(address))
if errors.Is(err, sqlx.ErrNotFound) {
return 0, ErrNotFound
}
return uid, err
}
// NewNhWalletModel returns a model for the database table.
func NewNhWalletModel(conn sqlx.SqlConn) NhWalletModel {
return &customNhWalletModel{
defaultNhWalletModel: newNhWalletModel(conn),
}
}
func (m *customNhWalletModel) withSession(session sqlx.Session) NhWalletModel {
return NewNhWalletModel(sqlx.NewSqlConnFromSession(session))
}