Skip to content

Commit

Permalink
imager: move all database operations into service layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jsec committed Nov 23, 2024
1 parent 6a3ea89 commit 5fa2080
Show file tree
Hide file tree
Showing 22 changed files with 263 additions and 44 deletions.
3 changes: 1 addition & 2 deletions internal/imager/circuit.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func (i Imager) loadCircuits(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveCircuits(ctx, records)
if err != nil {
if err = i.circuitService.SeedCircuits(ctx, tx, records); err != nil {
return fmt.Errorf("error saving circuits: %w", err)
}

Expand Down
9 changes: 3 additions & 6 deletions internal/imager/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ func (i Imager) loadConstructors(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveConstructors(ctx, records)
if err != nil {
if err = i.constructorService.SeedConstructors(ctx, tx, records); err != nil {
return fmt.Errorf("error saving constructors: %w", err)
}

Expand Down Expand Up @@ -85,8 +84,7 @@ func (i Imager) loadConstructorResults(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveConstructorResults(ctx, records)
if err != nil {
if err = i.constructorService.SeedConstructorResults(ctx, tx, records); err != nil {
return fmt.Errorf("error saving constructor results: %w", err)
}

Expand Down Expand Up @@ -131,8 +129,7 @@ func (i Imager) loadConstructorStandings(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveConstructorStandings(ctx, records)
if err != nil {
if err = i.constructorService.SeedConstructorStandings(ctx, tx, records); err != nil {
return fmt.Errorf("error saving constructor standings: %w", err)
}

Expand Down
6 changes: 2 additions & 4 deletions internal/imager/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func (i Imager) loadDrivers(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveDrivers(ctx, records)
if err != nil {
if err = i.driverService.SeedDrivers(ctx, tx, records); err != nil {
return fmt.Errorf("error saving drivers: %w", err)
}

Expand Down Expand Up @@ -98,8 +97,7 @@ func (i Imager) loadDriverStandings(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveDriverStandings(ctx, records)
if err != nil {
if err = i.driverService.SeedDriverStandings(ctx, tx, records); err != nil {
return fmt.Errorf("error saving driver standings: %w", err)
}

Expand Down
29 changes: 25 additions & 4 deletions internal/imager/imager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,51 @@ package imager
import (
"context"
"fmt"
"log"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
"github.com/jsec/f1-data-hub/internal/services"
)

type Imager struct {
db *database.Queries
circuitService services.CircuitService
constructorService services.ConstructorService
driverService services.DriverService
lapTimeService services.LapTimeService
pitStopService services.PitStopService
qualifyingService services.QualifyingService
raceService services.RaceService
resultService services.ResultsService
seasonService services.SeasonService
statusService services.StatusService
}

func Run(ctx context.Context) error {
pool, err := database.Connect(ctx)
if err != nil {
log.Fatal(err)
return fmt.Errorf("error acquiring database connection: %w", err)
}
defer pool.Close()

tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("error acquiring transaction: %w", err)
}
defer tx.Rollback(ctx)

db := database.New(pool)

imager := Imager{
db: database.New(pool),
circuitService: services.NewCircuitService(db),
constructorService: services.NewConstructorService(db),
driverService: services.NewDriverService(db),
lapTimeService: services.NewLapTimeService(db),
pitStopService: services.NewPitStopService(db),
qualifyingService: services.NewQualifyingService(db),
raceService: services.NewRaceService(db),
resultService: services.NewResultsService(db),
seasonService: services.NewSeasonService(db),
statusService: services.NewStatusService(db),
}

if err = imager.Seed(ctx, tx); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/imager/laptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func (i Imager) loadLapTimes(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveLapTimes(ctx, records)
if err != nil {
if err = i.lapTimeService.SeedLapTimes(ctx, tx, records); err != nil {
return fmt.Errorf("error saving lap times: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/imager/pitstop.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func (i Imager) loadPitStops(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SavePitStops(ctx, records)
if err != nil {
if err = i.pitStopService.SeedPitStops(ctx, tx, records); err != nil {
return fmt.Errorf("error saving pit stops: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/imager/qualifying.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func (i Imager) loadQualifying(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveQualifyingResults(ctx, records)
if err != nil {
if err = i.qualifyingService.SeedQualifyingResults(ctx, tx, records); err != nil {
return fmt.Errorf("error saving qualifying results: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/imager/race.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func (i Imager) loadRaces(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveRaces(ctx, records)
if err != nil {
if err = i.raceService.SeedRaces(ctx, tx, records); err != nil {
return fmt.Errorf("error saving races: %w", err)
}

Expand Down
6 changes: 2 additions & 4 deletions internal/imager/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ func (i Imager) loadResults(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveResults(ctx, records)
if err != nil {
if err = i.resultService.SeedResults(ctx, tx, records); err != nil {
return fmt.Errorf("error saving results: %w", err)
}

Expand Down Expand Up @@ -136,8 +135,7 @@ func (i Imager) loadSprintResults(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveSprintResults(ctx, records)
if err != nil {
if err = i.resultService.SeedSprintResults(ctx, tx, records); err != nil {
return fmt.Errorf("error saving sprint results: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/imager/season.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func (i Imager) loadSeasons(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveSeasons(ctx, records)
if err != nil {
if err = i.seasonService.SeedSeasons(ctx, tx, records); err != nil {
return fmt.Errorf("error saving seasons: %w", err)
}

Expand Down
3 changes: 1 addition & 2 deletions internal/imager/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func (i Imager) loadStatuses(ctx context.Context, tx pgx.Tx) error {
})
}

_, err = i.db.WithTx(tx).SaveStatuses(ctx, records)
if err != nil {
if err = i.statusService.SeedStatuses(ctx, tx, records); err != nil {
return fmt.Errorf("error saving statuses: %w", err)
}

Expand Down
7 changes: 0 additions & 7 deletions internal/repository/repository.go

This file was deleted.

21 changes: 21 additions & 0 deletions internal/services/circuit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type CircuitService struct {
db *database.Queries
}

func NewCircuitService(db *database.Queries) CircuitService {
return CircuitService{db}
}

func (c *CircuitService) SeedCircuits(ctx context.Context, tx pgx.Tx, records []database.SaveCircuitsParams) error {
_, err := c.db.SaveCircuits(ctx, records)
return err
}
31 changes: 31 additions & 0 deletions internal/services/constructor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type ConstructorService struct {
db *database.Queries
}

func NewConstructorService(db *database.Queries) ConstructorService {
return ConstructorService{db}
}

func (s *ConstructorService) SeedConstructors(ctx context.Context, tx pgx.Tx, records []database.SaveConstructorsParams) error {
_, err := s.db.WithTx(tx).SaveConstructors(ctx, records)
return err
}

func (s *ConstructorService) SeedConstructorResults(ctx context.Context, tx pgx.Tx, records []database.SaveConstructorResultsParams) error {
_, err := s.db.WithTx(tx).SaveConstructorResults(ctx, records)
return err
}

func (s *ConstructorService) SeedConstructorStandings(ctx context.Context, tx pgx.Tx, records []database.SaveConstructorStandingsParams) error {
_, err := s.db.WithTx(tx).SaveConstructorStandings(ctx, records)
return err
}
26 changes: 26 additions & 0 deletions internal/services/driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type DriverService struct {
db *database.Queries
}

func NewDriverService(db *database.Queries) DriverService {
return DriverService{db}
}

func (s *DriverService) SeedDrivers(ctx context.Context, tx pgx.Tx, records []database.SaveDriversParams) error {
_, err := s.db.WithTx(tx).SaveDrivers(ctx, records)
return err
}

func (s *DriverService) SeedDriverStandings(ctx context.Context, tx pgx.Tx, records []database.SaveDriverStandingsParams) error {
_, err := s.db.WithTx(tx).SaveDriverStandings(ctx, records)
return err
}
21 changes: 21 additions & 0 deletions internal/services/laptime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type LapTimeService struct {
db *database.Queries
}

func NewLapTimeService(db *database.Queries) LapTimeService {
return LapTimeService{db}
}

func (s *LapTimeService) SeedLapTimes(ctx context.Context, tx pgx.Tx, records []database.SaveLapTimesParams) error {
_, err := s.db.WithTx(tx).SaveLapTimes(ctx, records)
return err
}
21 changes: 21 additions & 0 deletions internal/services/pitstop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type PitStopService struct {
db *database.Queries
}

func NewPitStopService(db *database.Queries) PitStopService {
return PitStopService{db}
}

func (s *PitStopService) SeedPitStops(ctx context.Context, tx pgx.Tx, records []database.SavePitStopsParams) error {
_, err := s.db.WithTx(tx).SavePitStops(ctx, records)
return err
}
21 changes: 21 additions & 0 deletions internal/services/qualifying.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type QualifyingService struct {
db *database.Queries
}

func NewQualifyingService(db *database.Queries) QualifyingService {
return QualifyingService{db}
}

func (s *QualifyingService) SeedQualifyingResults(ctx context.Context, tx pgx.Tx, records []database.SaveQualifyingResultsParams) error {
_, err := s.db.WithTx(tx).SaveQualifyingResults(ctx, records)
return err
}
21 changes: 21 additions & 0 deletions internal/services/race.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type RaceService struct {
db *database.Queries
}

func NewRaceService(db *database.Queries) RaceService {
return RaceService{db}
}

func (s *RaceService) SeedRaces(ctx context.Context, tx pgx.Tx, records []database.SaveRacesParams) error {
_, err := s.db.WithTx(tx).SaveRaces(ctx, records)
return err
}
26 changes: 26 additions & 0 deletions internal/services/result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package services

import (
"context"

"github.com/jackc/pgx/v5"
"github.com/jsec/f1-data-hub/internal/database"
)

type ResultsService struct {
db *database.Queries
}

func NewResultsService(db *database.Queries) ResultsService {
return ResultsService{db}
}

func (s *ResultsService) SeedResults(ctx context.Context, tx pgx.Tx, records []database.SaveResultsParams) error {
_, err := s.db.WithTx(tx).SaveResults(ctx, records)
return err
}

func (s *ResultsService) SeedSprintResults(ctx context.Context, tx pgx.Tx, records []database.SaveSprintResultsParams) error {
_, err := s.db.WithTx(tx).SaveSprintResults(ctx, records)
return err
}
Loading

0 comments on commit 5fa2080

Please sign in to comment.