125 lines
3.8 KiB
Go
125 lines
3.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
)
|
|
|
|
func convertDBWallet(wallet dbgen.Wallet) domain.Wallet {
|
|
return domain.Wallet{
|
|
ID: wallet.ID,
|
|
Balance: domain.Currency(wallet.Balance),
|
|
IsWithdraw: wallet.IsWithdraw,
|
|
IsBettable: wallet.IsBettable,
|
|
IsActive: wallet.IsActive,
|
|
UserID: wallet.UserID,
|
|
}
|
|
}
|
|
|
|
func convertCreateWallet(wallet domain.CreateWallet) dbgen.CreateWalletParams {
|
|
return dbgen.CreateWalletParams{
|
|
Balance: int64(wallet.Balance),
|
|
IsWithdraw: wallet.IsWithdraw,
|
|
IsBettable: wallet.IsBettable,
|
|
UserID: wallet.UserID,
|
|
}
|
|
}
|
|
|
|
func convertDBCustomerWallet(customerWallet dbgen.CustomerWallet) domain.CustomerWallet {
|
|
return domain.CustomerWallet{
|
|
ID: customerWallet.ID,
|
|
RegularID: customerWallet.RegularWalletID,
|
|
StaticID: customerWallet.StaticWalletID,
|
|
CustomerID: customerWallet.CustomerID,
|
|
CompanyID: customerWallet.CompanyID,
|
|
}
|
|
}
|
|
func convertCreateCustomerWallet(customerWallet domain.CreateCustomerWallet) dbgen.CreateCustomerWalletParams {
|
|
return dbgen.CreateCustomerWalletParams{
|
|
CustomerID: customerWallet.CustomerID,
|
|
CompanyID: customerWallet.CompanyID,
|
|
RegularWalletID: customerWallet.RegularWalletID,
|
|
StaticWalletID: customerWallet.StaticWalletID,
|
|
}
|
|
}
|
|
|
|
func convertDBGetCustomerWallet(customerWallet dbgen.GetCustomerWalletRow) domain.GetCustomerWallet {
|
|
return domain.GetCustomerWallet{
|
|
ID: customerWallet.ID,
|
|
RegularID: customerWallet.RegularID,
|
|
RegularBalance: domain.Currency(customerWallet.RegularBalance),
|
|
StaticID: customerWallet.StaticID,
|
|
StaticBalance: domain.Currency(customerWallet.StaticBalance),
|
|
CustomerID: customerWallet.CustomerID,
|
|
CompanyID: customerWallet.CompanyID,
|
|
}
|
|
}
|
|
|
|
func (s *Store) CreateWallet(ctx context.Context, wallet domain.CreateWallet) (domain.Wallet, error) {
|
|
newWallet, err := s.queries.CreateWallet(ctx, convertCreateWallet(wallet))
|
|
if err != nil {
|
|
return domain.Wallet{}, err
|
|
}
|
|
return convertDBWallet(newWallet), nil
|
|
}
|
|
|
|
func (s *Store) CreateCustomerWallet(ctx context.Context, customerWallet domain.CreateCustomerWallet) (domain.CustomerWallet, error) {
|
|
newCustomerWallet, err := s.queries.CreateCustomerWallet(ctx, convertCreateCustomerWallet(customerWallet))
|
|
if err != nil {
|
|
return domain.CustomerWallet{}, err
|
|
}
|
|
return convertDBCustomerWallet(newCustomerWallet), nil
|
|
}
|
|
|
|
func (s *Store) GetWalletByID(ctx context.Context, id int64) (domain.Wallet, error) {
|
|
wallet, err := s.queries.GetWalletByID(ctx, id)
|
|
|
|
if err != nil {
|
|
return domain.Wallet{}, err
|
|
}
|
|
return convertDBWallet(wallet), nil
|
|
}
|
|
|
|
func (s *Store) GetAllWallets(ctx context.Context) ([]domain.Wallet, error) {
|
|
wallets, err := s.queries.GetAllWallets(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []domain.Wallet
|
|
for _, wallet := range wallets {
|
|
result = append(result, convertDBWallet(wallet))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) GetCustomerWallet(ctx context.Context, customerID int64, companyID int64) (domain.GetCustomerWallet, error) {
|
|
customerWallet, err := s.queries.GetCustomerWallet(ctx, dbgen.GetCustomerWalletParams{
|
|
CustomerID: customerID,
|
|
CompanyID: companyID,
|
|
})
|
|
|
|
if err != nil {
|
|
return domain.GetCustomerWallet{}, err
|
|
}
|
|
return convertDBGetCustomerWallet(customerWallet), nil
|
|
}
|
|
|
|
func (s *Store) UpdateBalance(ctx context.Context, id int64, balance domain.Currency) error {
|
|
err := s.queries.UpdateBalance(ctx, dbgen.UpdateBalanceParams{
|
|
ID: id,
|
|
Balance: int64(balance),
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (s *Store) UpdateWalletActive(ctx context.Context, id int64, isActive bool) error {
|
|
err := s.queries.UpdateWalletActive(ctx, dbgen.UpdateWalletActiveParams{
|
|
ID: id,
|
|
IsActive: isActive,
|
|
})
|
|
return err
|
|
}
|