164 lines
4.8 KiB
Go
164 lines
4.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func convertDBShopBet(bet dbgen.ShopBet) domain.ShopBet {
|
|
return domain.ShopBet{
|
|
ID: bet.ID,
|
|
ShopTransactionID: bet.ShopTransactionID,
|
|
CashoutID: bet.CashoutID,
|
|
CashedOut: bet.CashedOut,
|
|
BetID: bet.BetID,
|
|
NumberOfOutcomes: bet.NumberOfOutcomes,
|
|
}
|
|
}
|
|
|
|
func convertDBShopBetDetail(bet dbgen.ShopBetDetail) domain.ShopBetDetail {
|
|
var outcomes []domain.BetOutcome = make([]domain.BetOutcome, 0, len(bet.Outcomes))
|
|
|
|
for _, outcome := range bet.Outcomes {
|
|
outcomes = append(outcomes, convertDBBetOutcomes(outcome))
|
|
}
|
|
return domain.ShopBetDetail{
|
|
ID: bet.ID,
|
|
ShopTransactionID: bet.ShopTransactionID,
|
|
TotalOdds: bet.TotalOdds,
|
|
BranchID: bet.BranchID,
|
|
CompanyID: bet.CompanyID,
|
|
FullName: bet.CustomerFullName,
|
|
PhoneNumber: bet.CustomerPhoneNumber,
|
|
CashoutID: bet.CashoutID,
|
|
CashedOut: bet.CashedOut,
|
|
BetID: bet.BetID,
|
|
NumberOfOutcomes: bet.NumberOfOutcomes,
|
|
Status: domain.OutcomeStatus(bet.Status),
|
|
Amount: domain.Currency(bet.Amount),
|
|
Outcomes: outcomes,
|
|
TransactionVerified: bet.TransactionVerified,
|
|
UpdatedAt: bet.UpdatedAt.Time,
|
|
CreatedAt: bet.CreatedAt.Time,
|
|
}
|
|
}
|
|
|
|
func convertCreateShopBet(bet domain.CreateShopBet) dbgen.CreateShopBetParams {
|
|
return dbgen.CreateShopBetParams{
|
|
ShopTransactionID: bet.ShopTransactionID,
|
|
CashoutID: bet.CashoutID,
|
|
BetID: bet.BetID,
|
|
NumberOfOutcomes: bet.NumberOfOutcomes,
|
|
}
|
|
}
|
|
|
|
func (s *Store) CreateShopBet(ctx context.Context, bet domain.CreateShopBet) (domain.ShopBet, error) {
|
|
newShopBet, err := s.queries.CreateShopBet(ctx, convertCreateShopBet(bet))
|
|
|
|
if err != nil {
|
|
return domain.ShopBet{}, err
|
|
}
|
|
|
|
return convertDBShopBet(newShopBet), err
|
|
}
|
|
|
|
func (s *Store) GetAllShopBet(ctx context.Context, filter domain.ShopBetFilter) ([]domain.ShopBetDetail, error) {
|
|
bets, err := s.queries.GetAllShopBets(ctx, dbgen.GetAllShopBetsParams{
|
|
BranchID: pgtype.Int8{
|
|
Int64: filter.BranchID.Value,
|
|
Valid: filter.BranchID.Valid,
|
|
},
|
|
CompanyID: pgtype.Int8{
|
|
Int64: filter.CompanyID.Value,
|
|
Valid: filter.CompanyID.Valid,
|
|
},
|
|
Query: pgtype.Text{
|
|
String: filter.Query.Value,
|
|
Valid: filter.Query.Valid,
|
|
},
|
|
CreatedBefore: pgtype.Timestamp{
|
|
Time: filter.CreatedBefore.Value,
|
|
Valid: filter.CreatedBefore.Valid,
|
|
},
|
|
CreatedAfter: pgtype.Timestamp{
|
|
Time: filter.CreatedAfter.Value,
|
|
Valid: filter.CreatedAfter.Valid,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var result []domain.ShopBetDetail = make([]domain.ShopBetDetail, 0, len(bets))
|
|
for _, bet := range bets {
|
|
result = append(result, convertDBShopBetDetail(bet))
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (s *Store) GetShopBetByID(ctx context.Context, id int64) (domain.ShopBetDetail, error) {
|
|
bet, err := s.queries.GetShopBetByID(ctx, id)
|
|
if err != nil {
|
|
return domain.ShopBetDetail{}, err
|
|
}
|
|
return convertDBShopBetDetail(bet), nil
|
|
}
|
|
|
|
func (s *Store) GetShopBetByBetID(ctx context.Context, betID int64) (domain.ShopBetDetail, error) {
|
|
bet, err := s.queries.GetShopBetByBetID(ctx, betID)
|
|
if err != nil {
|
|
return domain.ShopBetDetail{}, err
|
|
}
|
|
return convertDBShopBetDetail(bet), nil
|
|
}
|
|
|
|
func (s *Store) GetShopBetByCashoutID(ctx context.Context, cashoutID string) (domain.ShopBetDetail, error) {
|
|
bet, err := s.queries.GetShopBetByCashoutID(ctx, cashoutID)
|
|
if err != nil {
|
|
return domain.ShopBetDetail{}, err
|
|
}
|
|
return convertDBShopBetDetail(bet), nil
|
|
}
|
|
func (s *Store) GetShopBetByShopTransactionID(ctx context.Context, shopTransactionID int64) (domain.ShopBetDetail, error) {
|
|
bet, err := s.queries.GetShopBetByShopTransactionID(ctx, shopTransactionID)
|
|
if err != nil {
|
|
return domain.ShopBetDetail{}, err
|
|
}
|
|
return convertDBShopBetDetail(bet), nil
|
|
}
|
|
|
|
func (s *Store) UpdateShopBetCashOut(ctx context.Context, id int64, cashedOut bool) error {
|
|
err := s.queries.UpdateShopBetCashOut(ctx, dbgen.UpdateShopBetCashOutParams{
|
|
ID: id,
|
|
CashedOut: cashedOut,
|
|
})
|
|
if err != nil {
|
|
domain.MongoDBLogger.Error("failed to update cashout",
|
|
zap.Int64("id", id),
|
|
zap.Bool("cashed_out", cashedOut),
|
|
zap.Error(err),
|
|
)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *Store) UpdateShopBetCashoutID(ctx context.Context, id int64, cashoutID string) error {
|
|
err := s.queries.UpdateShopBetCashoutID(ctx, dbgen.UpdateShopBetCashoutIDParams{
|
|
ID: id,
|
|
CashoutID: cashoutID,
|
|
})
|
|
if err != nil {
|
|
domain.MongoDBLogger.Error("failed to update cashout_id",
|
|
zap.Int64("id", id),
|
|
zap.String("cashout_id", cashoutID),
|
|
zap.Error(err),
|
|
)
|
|
}
|
|
return err
|
|
}
|