Yimaru-BackEnd/internal/domain/event.go
Samuel Tariku 18689ea124 feat: Add EventWithSettings domain model and related conversion functions
- Introduced EventWithSettings and EventWithSettingsRes structs for enhanced event data handling.
- Implemented conversion functions for creating and updating event settings.
- Added support for fetching events with settings from the database.
- Created new report data structures for comprehensive reporting capabilities.
- Implemented event statistics retrieval and filtering by league and sport.
- Added handlers for event statistics endpoints in the web server.
- Introduced DateInterval type for managing time intervals in reports.
2025-10-18 11:50:17 +03:00

242 lines
8.7 KiB
Go

package domain
import (
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
"github.com/jackc/pgx/v5/pgtype"
"time"
)
type BaseEvent struct {
ID int64
SourceEventID string
SportID int32
MatchName string
HomeTeam string
AwayTeam string
HomeTeamID int64
AwayTeamID int64
HomeTeamImage string
AwayTeamImage string
LeagueID int64
LeagueName string
LeagueCC ValidString
StartTime time.Time
Source EventSource
Status EventStatus
IsMonitored bool
DefaultIsFeatured bool
DefaultIsActive bool
DefaultWinningUpperLimit int64
Score ValidString
MatchMinute ValidInt
TimerStatus ValidString
AddedTime ValidInt
MatchPeriod ValidInt
IsLive bool
FetchedAt time.Time
TotalOddOutcomes int64
NumberOfBets int64
TotalAmount Currency
AvgBetAmount Currency
TotalPotentialWinnings Currency
}
type BaseEventRes struct {
ID int64 `json:"id"`
SourceEventID string `json:"source_event_id"`
SportID int32 `json:"sport_id"`
MatchName string `json:"match_name"`
HomeTeam string `json:"home_team"`
AwayTeam string `json:"away_team"`
HomeTeamID int64 `json:"home_team_id"`
AwayTeamID int64 `json:"away_team_id"`
HomeTeamImage string `json:"home_team_image"`
AwayTeamImage string `json:"away_team_image"`
LeagueID int64 `json:"league_id"`
LeagueName string `json:"league_name"`
LeagueCC string `json:"league_cc"`
StartTime time.Time `json:"start_time"`
Source EventSource `json:"source"`
Status EventStatus `json:"status"`
IsMonitored bool `json:"is_monitored"`
DefaultIsFeatured bool `json:"default_is_featured"`
DefaultIsActive bool `json:"default_is_active"`
DefaultWinningUpperLimit int64 `json:"default_winning_upper_limit"`
Score string `json:"score"`
MatchMinute int `json:"match_minute"`
TimerStatus string `json:"timer_status"`
AddedTime int `json:"added_time"`
MatchPeriod int `json:"match_period"`
IsLive bool `json:"is_live"`
FetchedAt time.Time `json:"fetched_at"`
TotalOddOutcomes int64 `json:"total_odd_outcomes"`
NumberOfBets int64 `json:"number_of_bets"`
TotalAmount float32 `json:"total_amount"`
AvgBetAmount float32 `json:"average_bet_amount"`
TotalPotentialWinnings float32 `json:"total_potential_winnings"`
}
type CreateEvent struct {
SourceEventID string
SportID int32
MatchName string
HomeTeam string
AwayTeam string
HomeTeamID int64
AwayTeamID int64
HomeTeamImage string
AwayTeamImage string
LeagueID int64
LeagueName string
StartTime time.Time
IsLive bool
Status EventStatus
Source EventSource
DefaultWinningUpperLimit int64
}
type EventFilter struct {
Query ValidString
SportID ValidInt32
LeagueID ValidInt64
CountryCode ValidString
FirstStartTime ValidTime
LastStartTime ValidTime
Limit ValidInt32
Offset ValidInt32
Featured ValidBool
Active ValidBool
IsLive ValidBool
Status ValidEventStatus
Source ValidEventSource
}
func ConvertDBEvent(event dbgen.EventDetailed) BaseEvent {
return BaseEvent{
ID: event.ID,
SourceEventID: event.SourceEventID,
SportID: event.SportID,
MatchName: event.MatchName,
HomeTeam: event.HomeTeam,
AwayTeam: event.AwayTeam,
HomeTeamID: event.HomeTeamID,
AwayTeamID: event.AwayTeamID,
HomeTeamImage: event.HomeKitImage,
AwayTeamImage: event.AwayKitImage,
LeagueID: event.LeagueID,
LeagueName: event.LeagueName,
LeagueCC: ValidString{
Value: event.LeagueCc.String,
Valid: event.LeagueCc.Valid,
},
StartTime: event.StartTime.Time.UTC(),
Source: EventSource(event.Source),
Status: EventStatus(event.Status),
DefaultIsFeatured: event.DefaultIsFeatured,
IsMonitored: event.IsMonitored,
DefaultIsActive: event.DefaultIsActive,
DefaultWinningUpperLimit: event.DefaultWinningUpperLimit,
Score: ValidString{
Value: event.Score.String,
Valid: event.Score.Valid,
},
MatchMinute: ValidInt{
Value: int(event.MatchMinute.Int32),
Valid: event.MatchMinute.Valid,
},
TimerStatus: ValidString{
Value: event.TimerStatus.String,
Valid: event.TimerStatus.Valid,
},
AddedTime: ValidInt{
Value: int(event.AddedTime.Int32),
Valid: event.AddedTime.Valid,
},
MatchPeriod: ValidInt{
Value: int(event.MatchPeriod.Int32),
Valid: event.MatchPeriod.Valid,
},
IsLive: event.IsLive,
FetchedAt: event.FetchedAt.Time,
TotalOddOutcomes: event.TotalOutcomes,
NumberOfBets: event.NumberOfBets,
TotalAmount: Currency(event.TotalAmount),
AvgBetAmount: Currency(event.AvgBetAmount),
TotalPotentialWinnings: Currency(event.TotalPotentialWinnings),
}
}
func ConvertDBEvents(events []dbgen.EventDetailed) []BaseEvent {
result := make([]BaseEvent, len(events))
for i, e := range events {
result[i] = ConvertDBEvent(e)
}
return result
}
func ConvertCreateEvent(e CreateEvent) dbgen.InsertEventParams {
return dbgen.InsertEventParams{
SourceEventID: e.SourceEventID,
SportID: e.SportID,
MatchName: e.MatchName,
HomeTeam: e.HomeTeam,
AwayTeam: e.AwayTeam,
HomeTeamID: e.HomeTeamID,
AwayTeamID: e.AwayTeamID,
HomeKitImage: e.HomeTeamImage,
AwayKitImage: e.AwayTeamImage,
LeagueID: e.LeagueID,
LeagueName: e.LeagueName,
StartTime: pgtype.Timestamp{Time: e.StartTime, Valid: true},
IsLive: e.IsLive,
Status: string(e.Status),
Source: string(e.Source),
DefaultWinningUpperLimit: e.DefaultWinningUpperLimit,
}
}
func ConvertEventRes(event BaseEvent) BaseEventRes {
return BaseEventRes{
ID: event.ID,
SourceEventID: event.SourceEventID,
SportID: event.SportID,
MatchName: event.MatchName,
HomeTeam: event.HomeTeam,
AwayTeam: event.AwayTeam,
HomeTeamID: event.HomeTeamID,
AwayTeamID: event.AwayTeamID,
HomeTeamImage: event.HomeTeamImage,
AwayTeamImage: event.AwayTeamImage,
LeagueID: event.LeagueID,
LeagueName: event.LeagueName,
LeagueCC: event.LeagueCC.Value,
StartTime: event.StartTime.UTC(),
Source: EventSource(event.Source),
Status: EventStatus(event.Status),
DefaultIsFeatured: event.DefaultIsFeatured,
IsMonitored: event.IsMonitored,
DefaultIsActive: event.DefaultIsActive,
DefaultWinningUpperLimit: event.DefaultWinningUpperLimit,
Score: event.Score.Value,
MatchMinute: event.MatchMinute.Value,
TimerStatus: event.TimerStatus.Value,
AddedTime: event.AddedTime.Value,
MatchPeriod: event.MatchPeriod.Value,
IsLive: event.IsLive,
FetchedAt: event.FetchedAt.UTC(),
TotalOddOutcomes: event.TotalOddOutcomes,
NumberOfBets: event.NumberOfBets,
TotalAmount: event.TotalAmount.Float32(),
AvgBetAmount: event.AvgBetAmount.Float32(),
TotalPotentialWinnings: event.TotalPotentialWinnings.Float32(),
}
}
func ConvertEventResList(events []BaseEvent) []BaseEventRes {
result := make([]BaseEventRes, 0, len(events))
for _, event := range events {
result = append(result, ConvertEventRes(event))
}
return result
}