317 lines
9.1 KiB
Go
317 lines
9.1 KiB
Go
package odds
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/config"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/repository"
|
|
)
|
|
|
|
type ServiceImpl struct {
|
|
store *repository.Store
|
|
config *config.Config
|
|
logger *slog.Logger
|
|
client *http.Client
|
|
}
|
|
|
|
func New(store *repository.Store, cfg *config.Config, logger *slog.Logger) *ServiceImpl {
|
|
return &ServiceImpl{
|
|
store: store,
|
|
config: cfg,
|
|
logger: logger,
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
|
}
|
|
}
|
|
|
|
// TODO Add the optimization to get 10 events at the same time
|
|
func (s *ServiceImpl) FetchNonLiveOdds(ctx context.Context) error {
|
|
eventIDs, err := s.store.GetAllUpcomingEvents(ctx)
|
|
if err != nil {
|
|
log.Printf("❌ Failed to fetch upcoming event IDs: %v", err)
|
|
return err
|
|
}
|
|
|
|
var errs []error
|
|
|
|
for index, event := range eventIDs {
|
|
|
|
eventID, err := strconv.ParseInt(event.ID, 10, 64)
|
|
if err != nil {
|
|
s.logger.Error("Failed to parse event id")
|
|
return err
|
|
}
|
|
|
|
url := fmt.Sprintf("https://api.b365api.com/v3/bet365/prematch?token=%s&FI=%d", s.config.Bet365Token, eventID)
|
|
|
|
log.Printf("📡 Fetching prematch odds for event ID: %d (%d/%d) ", eventID, index, len(eventIDs))
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
log.Printf("❌ Failed to create request for event %d: %v", eventID, err)
|
|
continue
|
|
}
|
|
|
|
resp, err := s.client.Do(req)
|
|
if err != nil {
|
|
log.Printf("❌ Failed to fetch prematch odds for event %d: %v", eventID, err)
|
|
continue
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Printf("❌ Failed to read response body for event %d: %v", eventID, err)
|
|
continue
|
|
}
|
|
var oddsData domain.BaseNonLiveOddResponse
|
|
|
|
if err := json.Unmarshal(body, &oddsData); err != nil || oddsData.Success != 1 || len(oddsData.Results) == 0 {
|
|
log.Printf("❌ Invalid prematch data for event %d", eventID)
|
|
continue
|
|
}
|
|
|
|
sportID, err := strconv.ParseInt(event.SportID, 10, 64)
|
|
|
|
switch sportID {
|
|
case domain.FOOTBALL:
|
|
if err := s.parseFootball(ctx, oddsData.Results[0]); err != nil {
|
|
s.logger.Error("Error while inserting football odd")
|
|
errs = append(errs, err)
|
|
}
|
|
case domain.BASKETBALL:
|
|
if err := s.parseBasketball(ctx, oddsData.Results[0]); err != nil {
|
|
s.logger.Error("Error while inserting basketball odd")
|
|
errs = append(errs, err)
|
|
}
|
|
case domain.ICE_HOCKEY:
|
|
if err := s.parseIceHockey(ctx, oddsData.Results[0]); err != nil {
|
|
s.logger.Error("Error while inserting ice hockey odd")
|
|
errs = append(errs, err)
|
|
}
|
|
|
|
}
|
|
|
|
// result := oddsData.Results[0]
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *ServiceImpl) parseFootball(ctx context.Context, res json.RawMessage) error {
|
|
var footballRes domain.FootballOddsResponse
|
|
if err := json.Unmarshal(res, &footballRes); err != nil {
|
|
s.logger.Error("Failed to unmarshal football result", "error", err)
|
|
return err
|
|
}
|
|
if footballRes.EventID == "" && footballRes.FI == "" {
|
|
s.logger.Error("Skipping football result with no valid Event ID", "eventID", footballRes.EventID, "fi", footballRes.FI)
|
|
return fmt.Errorf("Skipping football result with no valid Event ID Event ID %v", footballRes.EventID)
|
|
}
|
|
sections := map[string]domain.OddsSection{
|
|
"main": footballRes.Main,
|
|
"asian_lines": footballRes.AsianLines,
|
|
"goals": footballRes.Goals,
|
|
"half": footballRes.Half,
|
|
}
|
|
|
|
var errs []error
|
|
|
|
for oddCategory, section := range sections {
|
|
if err := s.storeSection(ctx, footballRes.EventID, footballRes.FI, oddCategory, section); err != nil {
|
|
s.logger.Error("Error storing football section", "eventID", footballRes.FI, "odd", oddCategory)
|
|
log.Printf("⚠️ Error when storing football %v", err)
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *ServiceImpl) parseBasketball(ctx context.Context, res json.RawMessage) error {
|
|
var basketballRes domain.BasketballOddsResponse
|
|
if err := json.Unmarshal(res, &basketballRes); err != nil {
|
|
s.logger.Error("Failed to unmarshal basketball result", "error", err)
|
|
return err
|
|
}
|
|
if basketballRes.EventID == "" && basketballRes.FI == "" {
|
|
s.logger.Error("Skipping basketball result with no valid Event ID")
|
|
return fmt.Errorf("Skipping basketball result with no valid Event ID")
|
|
}
|
|
sections := map[string]domain.OddsSection{
|
|
"main": basketballRes.Main,
|
|
"half_props": basketballRes.HalfProps,
|
|
"quarter_props": basketballRes.QuarterProps,
|
|
"team_props": basketballRes.TeamProps,
|
|
}
|
|
|
|
var errs []error
|
|
|
|
for oddCategory, section := range sections {
|
|
if err := s.storeSection(ctx, basketballRes.EventID, basketballRes.FI, oddCategory, section); err != nil {
|
|
s.logger.Error("Skipping result with no valid Event ID")
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
}
|
|
|
|
for _, section := range basketballRes.Others {
|
|
if err := s.storeSection(ctx, basketballRes.EventID, basketballRes.FI, "others", section); err != nil {
|
|
s.logger.Error("Skipping result with no valid Event ID")
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
func (s *ServiceImpl) parseIceHockey(ctx context.Context, res json.RawMessage) error {
|
|
var iceHockeyRes domain.IceHockeyOddsResponse
|
|
if err := json.Unmarshal(res, &iceHockeyRes); err != nil {
|
|
s.logger.Error("Failed to unmarshal ice hockey result", "error", err)
|
|
return err
|
|
}
|
|
if iceHockeyRes.EventID == "" && iceHockeyRes.FI == "" {
|
|
s.logger.Error("Skipping result with no valid Event ID")
|
|
return fmt.Errorf("Skipping result with no valid Event ID")
|
|
}
|
|
sections := map[string]domain.OddsSection{
|
|
"main": iceHockeyRes.Main,
|
|
"main_2": iceHockeyRes.Main2,
|
|
"1st_period": iceHockeyRes.FirstPeriod,
|
|
}
|
|
|
|
var errs []error
|
|
|
|
for oddCategory, section := range sections {
|
|
if err := s.storeSection(ctx, iceHockeyRes.EventID, iceHockeyRes.FI, oddCategory, section); err != nil {
|
|
s.logger.Error("Skipping result with no valid Event ID")
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
}
|
|
|
|
for _, section := range iceHockeyRes.Others {
|
|
if err := s.storeSection(ctx, iceHockeyRes.EventID, iceHockeyRes.FI, "others", section); err != nil {
|
|
s.logger.Error("Skipping result with no valid Event ID")
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *ServiceImpl) storeSection(ctx context.Context, eventID, fi, sectionName string, section domain.OddsSection) error {
|
|
if len(section.Sp) == 0 {
|
|
return nil
|
|
}
|
|
|
|
updatedAtUnix, _ := strconv.ParseInt(section.UpdatedAt, 10, 64)
|
|
updatedAt := time.Unix(updatedAtUnix, 0)
|
|
|
|
var errs []error
|
|
for marketType, market := range section.Sp {
|
|
if len(market.Odds) == 0 {
|
|
continue
|
|
}
|
|
|
|
// Check if the market id is a string
|
|
var marketIDstr string
|
|
err := json.Unmarshal(market.ID, &marketIDstr)
|
|
if err != nil {
|
|
// check if its int
|
|
var marketIDint int
|
|
err := json.Unmarshal(market.ID, &marketIDint)
|
|
if err != nil {
|
|
s.logger.Error("Invalid market id", "marketID", marketIDstr, "marketName", market.Name)
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
|
|
marketIDint, err := strconv.ParseInt(marketIDstr, 10, 64)
|
|
if err != nil {
|
|
s.logger.Error("Invalid market id", "marketID", marketIDstr, "marketName", market.Name)
|
|
errs = append(errs, err)
|
|
continue
|
|
}
|
|
|
|
isSupported, ok := domain.SupportedMarkets[marketIDint]
|
|
|
|
if !ok || !isSupported {
|
|
// s.logger.Info("Unsupported market_id", "marketID", marketIDint, "marketName", market.Name)
|
|
continue
|
|
}
|
|
|
|
marketRecord := domain.Market{
|
|
EventID: eventID,
|
|
FI: fi,
|
|
MarketCategory: sectionName,
|
|
MarketType: marketType,
|
|
MarketName: market.Name,
|
|
MarketID: marketIDstr,
|
|
UpdatedAt: updatedAt,
|
|
Odds: market.Odds,
|
|
}
|
|
|
|
err = s.store.SaveNonLiveMarket(ctx, marketRecord)
|
|
if err != nil {
|
|
s.logger.Error("failed to save market", "market_id", market.ID, "error", err)
|
|
errs = append(errs, fmt.Errorf("market %s: %w", market.ID, err))
|
|
continue
|
|
}
|
|
}
|
|
|
|
if len(errs) > 0 {
|
|
return errors.Join(errs...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *ServiceImpl) GetPrematchOdds(ctx context.Context, eventID string) ([]domain.Odd, error) {
|
|
return s.store.GetPrematchOdds(ctx, eventID)
|
|
}
|
|
|
|
func (s *ServiceImpl) GetALLPrematchOdds(ctx context.Context) ([]domain.Odd, error) {
|
|
return s.store.GetALLPrematchOdds(ctx)
|
|
}
|
|
|
|
func (s *ServiceImpl) GetRawOddsByMarketID(ctx context.Context, marketID string, upcomingID string) (domain.RawOddsByMarketID, error) {
|
|
rows, err := s.store.GetRawOddsByMarketID(ctx, marketID, upcomingID)
|
|
if err != nil {
|
|
return domain.RawOddsByMarketID{}, err
|
|
}
|
|
|
|
return rows, nil
|
|
}
|
|
|
|
func (s *ServiceImpl) GetPrematchOddsByUpcomingID(ctx context.Context, upcomingID string) ([]domain.Odd, error) {
|
|
return s.store.GetPrematchOddsByUpcomingID(ctx, upcomingID)
|
|
}
|
|
|
|
func (s *ServiceImpl) GetPaginatedPrematchOddsByUpcomingID(ctx context.Context, upcomingID string, limit, offset domain.ValidInt64) ([]domain.Odd, error) {
|
|
return s.store.GetPaginatedPrematchOddsByUpcomingID(ctx, upcomingID, limit, offset)
|
|
}
|