61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
dbgen "github.com/SamuelTariku/FortuneBet-Backend/gen/db"
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/domain"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func (s *Store) InsertResult(ctx context.Context, result domain.Result) error {
|
|
scoresJSON, err := json.Marshal(result.Scores)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal Scores: %w", err)
|
|
}
|
|
|
|
_, err = s.queries.InsertResult(ctx, dbgen.InsertResultParams{
|
|
EventID: pgtype.Text{String: result.EventID, Valid: true},
|
|
FullTimeScore: pgtype.Text{String: result.FullTimeScore, Valid: true},
|
|
HalfTimeScore: pgtype.Text{String: result.HalfTimeScore, Valid: true},
|
|
Ss: pgtype.Text{String: result.SS, Valid: true},
|
|
Scores: scoresJSON,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to insert result: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) GetResultByEventID(ctx context.Context, eventID string) (domain.Result, error) {
|
|
eventIDText := pgtype.Text{String: eventID, Valid: true}
|
|
|
|
result, err := s.queries.GetResultByEventID(ctx, eventIDText)
|
|
if err != nil {
|
|
return domain.Result{}, fmt.Errorf("failed to get result by event ID: %w", err)
|
|
}
|
|
|
|
var rawScores map[string]map[string]string
|
|
if err := json.Unmarshal(result.Scores, &rawScores); err != nil {
|
|
return domain.Result{}, fmt.Errorf("failed to unmarshal scores: %w", err)
|
|
}
|
|
|
|
scores := make(map[string]domain.Score)
|
|
for key, value := range rawScores {
|
|
scores[key] = domain.Score{
|
|
Home: value["home"],
|
|
Away: value["away"],
|
|
}
|
|
}
|
|
|
|
return domain.Result{
|
|
EventID: result.EventID.String,
|
|
FullTimeScore: result.FullTimeScore.String,
|
|
HalfTimeScore: result.HalfTimeScore.String,
|
|
SS: result.Ss.String,
|
|
Scores: scores,
|
|
}, nil
|
|
} |