diff --git a/.gitignore b/.gitignore index c29bfa6..bdc8b3e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage .env tmp build +*.log diff --git a/db/migrations/000001_fortune.up.sql b/db/migrations/000001_fortune.up.sql index 9d3709e..610b920 100644 --- a/db/migrations/000001_fortune.up.sql +++ b/db/migrations/000001_fortune.up.sql @@ -176,7 +176,6 @@ CREATE TABLE IF NOT EXISTS branch_cashiers ( branch_id BIGINT NOT NULL, UNIQUE(user_id, branch_id) ); - CREATE TABLE events ( id TEXT PRIMARY KEY, sport_id TEXT, @@ -217,24 +216,14 @@ CREATE TABLE odds ( fetched_at TIMESTAMP DEFAULT now(), source TEXT DEFAULT 'b365api', is_active BOOLEAN DEFAULT true, + UNIQUE (market_id, name, handicap), UNIQUE (event_id, market_id, name, handicap) ); - - - ALTER TABLE refresh_tokens ADD CONSTRAINT fk_refresh_tokens_users FOREIGN KEY (user_id) REFERENCES users(id); ALTER TABLE bets ADD CONSTRAINT fk_bets_users FOREIGN KEY (user_id) REFERENCES users(id), ADD CONSTRAINT fk_bets_branches FOREIGN KEY (branch_id) REFERENCES branches(id); -ALTER TABLE bet_outcomes -ADD CONSTRAINT fk_bet_outcomes_bets FOREIGN KEY (bet_id) REFERENCES bets(id), - ADD CONSTRAINT fk_bet_outcomes_events FOREIGN KEY (event_id) REFERENCES supported_operations(id), - ADD CONSTRAINT fk_bet_outcomes_odds FOREIGN KEY (odd_id) REFERENCES supported_operations(id); -ALTER TABLE ticket_outcomes -ADD CONSTRAINT fk_ticket_outcomes_tickets FOREIGN KEY (ticket_id) REFERENCES tickets(id), - ADD CONSTRAINT fk_ticket_outcomes_events FOREIGN KEY (event_id) REFERENCES supported_operations(id), - ADD CONSTRAINT fk_ticket_outcomes_odds FOREIGN KEY (odd_id) REFERENCES supported_operations(id); ALTER TABLE wallets ADD CONSTRAINT fk_wallets_users FOREIGN KEY (user_id) REFERENCES users(id); ALTER TABLE customer_wallets @@ -341,5 +330,4 @@ VALUES ( CURRENT_TIMESTAMP, CURRENT_TIMESTAMP ); - ---------------------------------------------------Bet365 Data Fetching + Event Managment------------------------------------------------ +--------------------------------------------------Bet365 Data Fetching + Event Managment------------------------------------------------ \ No newline at end of file diff --git a/gen/db/models.go b/gen/db/models.go index 72f7dfc..9a9993c 100644 --- a/gen/db/models.go +++ b/gen/db/models.go @@ -148,6 +148,7 @@ type Odd struct { MarketCategory pgtype.Text `json:"market_category"` MarketID pgtype.Text `json:"market_id"` Name pgtype.Text `json:"name"` + Header pgtype.Text `json:"header"` Handicap pgtype.Text `json:"handicap"` OddsValue pgtype.Float8 `json:"odds_value"` Section string `json:"section"` diff --git a/gen/db/odds.sql.go b/gen/db/odds.sql.go index f7e88ab..5f966bf 100644 --- a/gen/db/odds.sql.go +++ b/gen/db/odds.sql.go @@ -12,8 +12,7 @@ import ( ) const GetALLPrematchOdds = `-- name: GetALLPrematchOdds :many -SELECT - event_id, +SELECT event_id, fi, market_type, market_name, @@ -29,7 +28,8 @@ SELECT source, is_active FROM odds -WHERE is_active = true AND source = 'b365api' +WHERE is_active = true + AND source = 'b365api' ` type GetALLPrematchOddsRow struct { @@ -87,8 +87,7 @@ func (q *Queries) GetALLPrematchOdds(ctx context.Context) ([]GetALLPrematchOddsR } const GetPrematchOdds = `-- name: GetPrematchOdds :many -SELECT - event_id, +SELECT event_id, fi, market_type, market_name, @@ -104,7 +103,8 @@ SELECT source, is_active FROM odds -WHERE is_active = true AND source = 'b365api' +WHERE is_active = true + AND source = 'b365api' ` type GetPrematchOddsRow struct { @@ -162,8 +162,7 @@ func (q *Queries) GetPrematchOdds(ctx context.Context) ([]GetPrematchOddsRow, er } const GetPrematchOddsByUpcomingID = `-- name: GetPrematchOddsByUpcomingID :many -SELECT - o.event_id, +SELECT o.event_id, o.fi, o.market_type, o.market_name, @@ -179,12 +178,12 @@ SELECT o.source, o.is_active FROM odds o -JOIN events e ON o.fi = e.id + JOIN events e ON o.fi = e.id WHERE e.id = $1 - AND e.is_live = false - AND e.status = 'upcoming' - AND o.is_active = true - AND o.source = 'b365api' + AND e.is_live = false + AND e.status = 'upcoming' + AND o.is_active = true + AND o.source = 'b365api' LIMIT $2 OFFSET $3 ` @@ -249,15 +248,13 @@ func (q *Queries) GetPrematchOddsByUpcomingID(ctx context.Context, arg GetPremat } const GetRawOddsByID = `-- name: GetRawOddsByID :one -SELECT - id, - raw_odds, +SELECT id, + raw_odds, fetched_at FROM odds -WHERE - raw_odds @> $1::jsonb AND - is_active = true AND - source = 'b365api' +WHERE raw_odds @> $1::jsonb + AND is_active = true + AND source = 'b365api' LIMIT 1 ` @@ -276,35 +273,49 @@ func (q *Queries) GetRawOddsByID(ctx context.Context, dollar_1 []byte) (GetRawOd const InsertNonLiveOdd = `-- name: InsertNonLiveOdd :exec INSERT INTO odds ( - event_id, - fi, - market_type, - market_name, - market_category, - market_id, - name, - handicap, - odds_value, - section, - category, - raw_odds, - is_active, - source, - fetched_at -) VALUES ( - $1, $2, $3, $4, $5, $6, $7, - $8, $9, $10, $11, $12, $13, $14, $15 -) -ON CONFLICT (market_id, name, handicap) DO UPDATE SET - odds_value = EXCLUDED.odds_value, - raw_odds = EXCLUDED.raw_odds, - market_type = EXCLUDED.market_type, - market_name = EXCLUDED.market_name, + event_id, + fi, + market_type, + market_name, + market_category, + market_id, + name, + handicap, + odds_value, + section, + category, + raw_odds, + is_active, + source, + fetched_at + ) +VALUES ( + $1, + $2, + $3, + $4, + $5, + $6, + $7, + $8, + $9, + $10, + $11, + $12, + $13, + $14, + $15 + ) ON CONFLICT (market_id, name, handicap) DO +UPDATE +SET odds_value = EXCLUDED.odds_value, + raw_odds = EXCLUDED.raw_odds, + market_type = EXCLUDED.market_type, + market_name = EXCLUDED.market_name, market_category = EXCLUDED.market_category, - fetched_at = EXCLUDED.fetched_at, - is_active = EXCLUDED.is_active, - source = EXCLUDED.source, - fi = EXCLUDED.fi + fetched_at = EXCLUDED.fetched_at, + is_active = EXCLUDED.is_active, + source = EXCLUDED.source, + fi = EXCLUDED.fi ` type InsertNonLiveOddParams struct { diff --git a/internal/web_server/app.go b/internal/web_server/app.go index a6ed559..c81a930 100644 --- a/internal/web_server/app.go +++ b/internal/web_server/app.go @@ -1,25 +1,24 @@ package httpserver import ( - "fmt" - "log/slog" + "fmt" + "log/slog" - "github.com/SamuelTariku/FortuneBet-Backend/internal/services/authentication" - "github.com/SamuelTariku/FortuneBet-Backend/internal/services/event" - "github.com/SamuelTariku/FortuneBet-Backend/internal/services/odds" + "github.com/SamuelTariku/FortuneBet-Backend/internal/services/authentication" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/bet" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/branch" + "github.com/SamuelTariku/FortuneBet-Backend/internal/services/event" + "github.com/SamuelTariku/FortuneBet-Backend/internal/services/odds" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/ticket" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/transaction" - "github.com/SamuelTariku/FortuneBet-Backend/internal/services/user" + "github.com/SamuelTariku/FortuneBet-Backend/internal/services/user" "github.com/SamuelTariku/FortuneBet-Backend/internal/services/wallet" - jwtutil "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/jwt" - customvalidator "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/validator" - + jwtutil "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/jwt" + customvalidator "github.com/SamuelTariku/FortuneBet-Backend/internal/web_server/validator" notificationservice "github.com/SamuelTariku/FortuneBet-Backend/internal/services/notfication" - "github.com/bytedance/sonic" - "github.com/gofiber/fiber/v2" + "github.com/bytedance/sonic" + "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" ) @@ -38,46 +37,47 @@ type App struct { validator *customvalidator.CustomValidator JwtConfig jwtutil.JwtConfig Logger *slog.Logger - prematchSvc *odds.ServiceImpl - eventSvc event.Service + prematchSvc *odds.ServiceImpl + eventSvc event.Service } func NewApp( - port int, validator *customvalidator.CustomValidator, - authSvc *authentication.Service, - logger *slog.Logger, - JwtConfig jwtutil.JwtConfig, - userSvc *user.Service, + port int, validator *customvalidator.CustomValidator, + authSvc *authentication.Service, + logger *slog.Logger, + JwtConfig jwtutil.JwtConfig, + userSvc *user.Service, ticketSvc *ticket.Service, betSvc *bet.Service, walletSvc *wallet.Service, transactionSvc *transaction.Service, branchSvc *branch.Service, notidicationStore notificationservice.NotificationStore, - prematchSvc *odds.ServiceImpl, - eventSvc event.Service, + prematchSvc *odds.ServiceImpl, + eventSvc event.Service, ) *App { - app := fiber.New(fiber.Config{ - CaseSensitive: true, - DisableHeaderNormalizing: true, - JSONEncoder: sonic.Marshal, - JSONDecoder: sonic.Unmarshal, - }) + app := fiber.New(fiber.Config{ + CaseSensitive: true, + DisableHeaderNormalizing: true, + JSONEncoder: sonic.Marshal, + JSONDecoder: sonic.Unmarshal, + }) app.Use(cors.New(cors.Config{ - AllowOrigins: "http://localhost:5173", // Specify your frontend's origin - AllowMethods: "GET,POST,PUT,DELETE", // Specify the allowed HTTP methods - AllowHeaders: "Content-Type,Authorization", // Specify the allowed headers + AllowOrigins: "http://localhost:8000", // Specify your frontend's origin + AllowMethods: "GET,POST,PUT,DELETE,OPTIONS", // Specify the allowed HTTP methods + AllowHeaders: "Content-Type,Authorization,platform", // Specify the allowed headers + AllowCredentials: true, })) - s := &App{ - fiber: app, - port: port, - authSvc: authSvc, - validator: validator, - logger: logger, - JwtConfig: JwtConfig, - userSvc: userSvc, + s := &App{ + fiber: app, + port: port, + authSvc: authSvc, + validator: validator, + logger: logger, + JwtConfig: JwtConfig, + userSvc: userSvc, ticketSvc: ticketSvc, betSvc: betSvc, walletSvc: walletSvc, @@ -85,15 +85,15 @@ func NewApp( branchSvc: branchSvc, NotidicationStore: notidicationStore, Logger: logger, - prematchSvc: prematchSvc, - eventSvc: eventSvc, - } + prematchSvc: prematchSvc, + eventSvc: eventSvc, + } - s.initAppRoutes() + s.initAppRoutes() - return s + return s } func (a *App) Run() error { - return a.fiber.Listen(fmt.Sprintf(":%d", a.port)) -} \ No newline at end of file + return a.fiber.Listen(fmt.Sprintf(":%d", a.port)) +}