mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-23 07:35:04 +01:00
32 lines
938 B
Go
32 lines
938 B
Go
package lib
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/getAlby/lndhub.go/db/models"
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type LndhubService struct {
|
|
DB *bun.DB
|
|
LndClient *lnrpc.LightningClient
|
|
}
|
|
|
|
func (svc *LndhubService) CurrentBalance(ctx context.Context, userId int64) (int64, error) {
|
|
var balance int64
|
|
|
|
account, err := svc.AccountFor(ctx, "current", userId)
|
|
if err != nil {
|
|
return balance, err
|
|
}
|
|
err = svc.DB.NewSelect().Table("account_ledgers").ColumnExpr("sum(account_ledgers.amount) as balance").Where("account_ledgers.account_id = ?", account.ID).Scan(context.TODO(), &balance)
|
|
return balance, err
|
|
}
|
|
|
|
func (svc *LndhubService) AccountFor(ctx context.Context, accountType string, userId int64) (models.Account, error) {
|
|
account := models.Account{}
|
|
err := svc.DB.NewSelect().Model(&account).Where("user_id = ? AND type= ?", userId, accountType).Limit(1).Scan(ctx)
|
|
return account, err
|
|
}
|