forked from ebhomengo/niki
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/domain/wallet/entity"
|
|
"git.gocasts.ir/ebhomengo/niki/pkg/database/postgres"
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
|
)
|
|
|
|
func (db *DB) GetWalletByUserID(ctx context.Context, UserID uint64) (entity.Wallet, error) {
|
|
const op = richerror.Op("Wallet.repo.GetWalletByUserID")
|
|
query := `SELECT * FROM wallets WHERE user_id = $1`
|
|
stmt, stErr := db.conn.PrepareStatement(ctx, postgres.StatementKeyWalletGetUserWallet, query)
|
|
if stErr != nil {
|
|
return entity.Wallet{}, richerror.New(op).WithErr(stErr)
|
|
}
|
|
walletRow := db.conn.StmtQueryRowContext(ctx, stmt, UserID)
|
|
|
|
wallet, sErr := scanWallet(walletRow)
|
|
|
|
if sErr != nil {
|
|
return entity.Wallet{}, richerror.New(op).WithErr(sErr)
|
|
}
|
|
|
|
return wallet, nil
|
|
|
|
}
|
|
|
|
func scanWallet(scanner postgres.Scanner) (entity.Wallet, error) {
|
|
|
|
var wallet entity.Wallet
|
|
|
|
err := scanner.Scan(&wallet.ID, &wallet.Balance, &wallet.Currency, &wallet.Status, &wallet.UpdatedAt)
|
|
if err != nil {
|
|
return entity.Wallet{}, err
|
|
}
|
|
|
|
return wallet, nil
|
|
}
|