package repository import ( "context" "database/sql" "errors" dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db" "github.com/SamuelTariku/FortuneBet-Backend/internal/domain" "github.com/jackc/pgx/v5/pgtype" ) type BankRepository interface { CreateBank(ctx context.Context, bank *domain.Bank) error GetBankByID(ctx context.Context, id int) (*domain.Bank, error) GetAllBanks(ctx context.Context, countryID *int, isActive *int) ([]domain.Bank, error) UpdateBank(ctx context.Context, bank *domain.Bank) error DeleteBank(ctx context.Context, id int) error } type BankRepo struct { store *Store } func NewBankRepository(store *Store) BankRepository { return &BankRepo{store: store} } func (r *BankRepo) CreateBank(ctx context.Context, bank *domain.Bank) error { params := dbgen.CreateBankParams{ Slug: bank.Slug, Swift: bank.Swift, Name: bank.Name, AcctLength: int32(bank.AcctLength), CountryID: int32(bank.CountryID), IsMobilemoney: pgtype.Int4{Int32: int32(bank.IsMobileMoney), Valid: true}, IsActive: int32(bank.IsActive), IsRtgs: int32(bank.IsRTGS), Active: int32(bank.Active), Is24hrs: pgtype.Int4{Int32: int32(bank.Is24Hrs), Valid: true}, Currency: bank.Currency, BankLogo: pgtype.Text{String: bank.BankLogo, Valid: true}, } createdBank, err := r.store.queries.CreateBank(ctx, params) if err != nil { return err } // Update the ID and timestamps on the passed struct bank.ID = int(createdBank.ID) bank.CreatedAt = createdBank.CreatedAt.Time bank.UpdatedAt = createdBank.UpdatedAt.Time return nil } func (r *BankRepo) GetBankByID(ctx context.Context, id int) (*domain.Bank, error) { dbBank, err := r.store.queries.GetBankByID(ctx, int64(id)) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, nil } return nil, err } return mapDBBankToDomain(&dbBank), nil } func (r *BankRepo) GetAllBanks(ctx context.Context, countryID *int, isActive *int) ([]domain.Bank, error) { params := dbgen.GetAllBanksParams{ CountryID: pgtype.Int4{}, IsActive: pgtype.Int4{}, } if countryID != nil { params.CountryID = pgtype.Int4{Int32: int32(*countryID), Valid: true} } if isActive != nil { params.IsActive = pgtype.Int4{Int32: int32(*isActive), Valid: true} } dbBanks, err := r.store.queries.GetAllBanks(ctx, params) if err != nil { return nil, err } banks := make([]domain.Bank, len(dbBanks)) for i, b := range dbBanks { banks[i] = *mapDBBankToDomain(&b) } return banks, nil } func (r *BankRepo) UpdateBank(ctx context.Context, bank *domain.Bank) error { params := dbgen.UpdateBankParams{ ID: int64(bank.ID), Slug: pgtype.Text{String: bank.Slug, Valid: true}, Swift: pgtype.Text{String: bank.Swift, Valid: true}, Name: pgtype.Text{String: bank.Name, Valid: true}, AcctLength: pgtype.Int4{Int32: int32(bank.AcctLength), Valid: true}, CountryID: pgtype.Int4{Int32: int32(bank.CountryID), Valid: true}, IsMobilemoney: pgtype.Int4{Int32: int32(bank.IsMobileMoney), Valid: true}, IsActive: pgtype.Int4{Int32: int32(bank.IsActive), Valid: true}, IsRtgs: pgtype.Int4{Int32: int32(bank.IsRTGS), Valid: true}, Active: pgtype.Int4{Int32: int32(bank.Active), Valid: true}, Is24hrs: pgtype.Int4{Int32: int32(bank.Is24Hrs), Valid: true}, Currency: pgtype.Text{String: bank.Currency, Valid: true}, BankLogo: pgtype.Text{String: bank.BankLogo, Valid: true}, } updatedBank, err := r.store.queries.UpdateBank(ctx, params) if err != nil { return err } // update timestamps in domain struct bank.UpdatedAt = updatedBank.UpdatedAt.Time return nil } func (r *BankRepo) DeleteBank(ctx context.Context, id int) error { return r.store.queries.DeleteBank(ctx, int64(id)) } // Helper to map DB struct to domain func mapDBBankToDomain(dbBank *dbgen.Bank) *domain.Bank { return &domain.Bank{ ID: int(dbBank.ID), Slug: dbBank.Slug, Swift: dbBank.Swift, Name: dbBank.Name, AcctLength: int(dbBank.AcctLength), CountryID: int(dbBank.CountryID), IsMobileMoney: int(dbBank.IsMobilemoney.Int32), IsActive: int(dbBank.IsActive), IsRTGS: int(dbBank.IsRtgs), Active: int(dbBank.Active), Is24Hrs: int(dbBank.Is24hrs.Int32), CreatedAt: dbBank.CreatedAt.Time, UpdatedAt: dbBank.UpdatedAt.Time, Currency: dbBank.Currency, BankLogo: dbBank.BankLogo.String, } }