From e4863ce6c8e6d21e5b66f861e89c12fc8bc7b89f Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Tue, 15 Oct 2024 17:17:35 +0200 Subject: [PATCH 1/2] enhanced service dependency encapsulation --- api/internal/application/races/RacesByYear.go | 14 +++++++++----- .../application/races/RacesByYear_test.go | 16 ++++------------ api/internal/domain/service/DriversByRace.go | 10 ++++++++-- api/internal/domain/service/PodiumByRace.go | 14 +++++++++----- api/internal/infrastructure/controller/Races.go | 6 +++--- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/api/internal/application/races/RacesByYear.go b/api/internal/application/races/RacesByYear.go index f79edd5..b484ea0 100644 --- a/api/internal/application/races/RacesByYear.go +++ b/api/internal/application/races/RacesByYear.go @@ -16,21 +16,25 @@ type GetRacesByYearDependencies interface { } type RacesByYear struct { - Dependencies GetRacesByYearDependencies + dependencies GetRacesByYearDependencies +} + +func NewRacesByYear(dependencies GetRacesByYearDependencies) *RacesByYear { + return &RacesByYear{ + dependencies: dependencies, + } } func (r *RacesByYear) Get( year int, ) ([]Response, error) { - races, error := r.Dependencies.FetchRacesByYear(year) + races, error := r.dependencies.FetchRacesByYear(year) if error != nil { return nil, error } - podiumByRace := domain_service.PodiumByRace{ - Dependencies: r.Dependencies, - } + podiumByRace := domain_service.NewPodiumByRace(r.dependencies) podiumsByRace, error := podiumByRace.Get(races) if error != nil { diff --git a/api/internal/application/races/RacesByYear_test.go b/api/internal/application/races/RacesByYear_test.go index 714aa33..29b8a38 100644 --- a/api/internal/application/races/RacesByYear_test.go +++ b/api/internal/application/races/RacesByYear_test.go @@ -47,9 +47,7 @@ func TestGetRacesByYear(t *testing.T) { nil, ) - RacesByYear := RacesByYear{ - Dependencies: mockInfraDependencies, - } + RacesByYear := NewRacesByYear(mockInfraDependencies) result, error := RacesByYear.Get(2024) @@ -75,9 +73,7 @@ func TestFetchRacesByYearErrorHandling(t *testing.T) { errors.New("Unable to fetch races"), ) - RacesByYear := RacesByYear{ - Dependencies: mockInfraDependencies, - } + RacesByYear := NewRacesByYear(mockInfraDependencies) _, error := RacesByYear.Get(2024) assert.NotEqual(error, nil) @@ -108,9 +104,7 @@ func TestFetchDriversByRaceErrorHandling(t *testing.T) { errors.New("Unable to fetch drivers"), ) - RacesByYear := RacesByYear{ - Dependencies: mockInfraDependencies, - } + RacesByYear := NewRacesByYear(mockInfraDependencies) _, error := RacesByYear.Get(2024) assert.NotEqual(error, nil) @@ -149,9 +143,7 @@ func TestFetchPodiumByRaceErrorHandling(t *testing.T) { errors.New("Unable to fetch podiums"), ) - RacesByYear := RacesByYear{ - Dependencies: mockInfraDependencies, - } + RacesByYear := NewRacesByYear(mockInfraDependencies) _, error := RacesByYear.Get(2024) assert.NotEqual(error, nil) diff --git a/api/internal/domain/service/DriversByRace.go b/api/internal/domain/service/DriversByRace.go index 3189064..b1c15b4 100644 --- a/api/internal/domain/service/DriversByRace.go +++ b/api/internal/domain/service/DriversByRace.go @@ -17,7 +17,13 @@ type fetchDriversResponse struct { } type DriversByRace struct { - Dependencies GetDriversByRaceDependencies + dependencies GetDriversByRaceDependencies +} + +func NewDriversByRace(dependencies GetDriversByRaceDependencies) *DriversByRace { + return &DriversByRace{ + dependencies: dependencies, + } } func (d *DriversByRace) Get(races []domain_model.Race) (map[int][]domain_model.Driver, error) { @@ -60,7 +66,7 @@ func (d *DriversByRace) fetchDrivers( ) { defer waitGroup.Done() - result, error := d.Dependencies.FetchDriversByRace(raceId) + result, error := d.dependencies.FetchDriversByRace(raceId) if error != nil { respChan <- fetchDriversResponse{ diff --git a/api/internal/domain/service/PodiumByRace.go b/api/internal/domain/service/PodiumByRace.go index 35bb7dd..800c43e 100644 --- a/api/internal/domain/service/PodiumByRace.go +++ b/api/internal/domain/service/PodiumByRace.go @@ -18,7 +18,13 @@ type fetchPodiumsResponse struct { } type PodiumByRace struct { - Dependencies GetPodiumByRaceDependencies + dependencies GetPodiumByRaceDependencies +} + +func NewPodiumByRace(dependencies GetPodiumByRaceDependencies) *PodiumByRace { + return &PodiumByRace{ + dependencies: dependencies, + } } func (p *PodiumByRace) Get( @@ -27,9 +33,7 @@ func (p *PodiumByRace) Get( podiumsByRace := make(map[int][3]domain_model.Podium) - driversByRace := DriversByRace{ - Dependencies: p.Dependencies, - } + driversByRace := NewDriversByRace(p.dependencies) drivers, error := driversByRace.Get(races) if error != nil { @@ -73,7 +77,7 @@ func (p *PodiumByRace) fetchPodiums( ) { defer waitGroup.Done() - result, error := p.Dependencies.FetchPodiumByRace(raceId, drivers) + result, error := p.dependencies.FetchPodiumByRace(raceId, drivers) if error != nil { respChan <- fetchPodiumsResponse{ diff --git a/api/internal/infrastructure/controller/Races.go b/api/internal/infrastructure/controller/Races.go index 4c89416..7b20d2a 100644 --- a/api/internal/infrastructure/controller/Races.go +++ b/api/internal/infrastructure/controller/Races.go @@ -13,9 +13,9 @@ import ( func Races(w http.ResponseWriter, r *http.Request) { - racesByYear := application_races.RacesByYear{ - Dependencies: &infraDependencies{}, - } + racesByYear := application_races.NewRacesByYear( + &infraDependencies{}, + ) races, error := racesByYear.Get(2024) if error != nil { From e4a4d65ce667791d314573bf8ed074d649cd30d8 Mon Sep 17 00:00:00 2001 From: Mikel Madariaga Date: Tue, 15 Oct 2024 17:50:38 +0200 Subject: [PATCH 2/2] updated coverage report --- api/coverage/coverage.html | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/api/coverage/coverage.html b/api/coverage/coverage.html index ee51da4..8ace7de 100644 --- a/api/coverage/coverage.html +++ b/api/coverage/coverage.html @@ -100,21 +100,25 @@ } type RacesByYear struct { - Dependencies GetRacesByYearDependencies + dependencies GetRacesByYearDependencies } +func NewRacesByYear(dependencies GetRacesByYearDependencies) *RacesByYear { + return &RacesByYear{ + dependencies: dependencies, + } +} + func (r *RacesByYear) Get( year int, ) ([]Response, error) { - races, error := r.Dependencies.FetchRacesByYear(year) + races, error := r.dependencies.FetchRacesByYear(year) if error != nil { return nil, error } - podiumByRace := domain_service.PodiumByRace{ - Dependencies: r.Dependencies, - } + podiumByRace := domain_service.NewPodiumByRace(r.dependencies) podiumsByRace, error := podiumByRace.Get(races) if error != nil { @@ -164,9 +168,15 @@ } type DriversByRace struct { - Dependencies GetDriversByRaceDependencies + dependencies GetDriversByRaceDependencies } +func NewDriversByRace(dependencies GetDriversByRaceDependencies) *DriversByRace { + return &DriversByRace{ + dependencies: dependencies, + } +} + func (d *DriversByRace) Get(races []domain_model.Race) (map[int][]domain_model.Driver, error) { driversByRace := make(map[int][]domain_model.Driver) @@ -207,7 +217,7 @@ ) { defer waitGroup.Done() - result, error := d.Dependencies.FetchDriversByRace(raceId) + result, error := d.dependencies.FetchDriversByRace(raceId) if error != nil { respChan <- fetchDriversResponse{ @@ -246,18 +256,22 @@ } type PodiumByRace struct { - Dependencies GetPodiumByRaceDependencies + dependencies GetPodiumByRaceDependencies } +func NewPodiumByRace(dependencies GetPodiumByRaceDependencies) *PodiumByRace { + return &PodiumByRace{ + dependencies: dependencies, + } +} + func (p *PodiumByRace) Get( races []domain_model.Race, ) (map[int][3]domain_model.Podium, error) { podiumsByRace := make(map[int][3]domain_model.Podium) - driversByRace := DriversByRace{ - Dependencies: p.Dependencies, - } + driversByRace := NewDriversByRace(p.dependencies) drivers, error := driversByRace.Get(races) if error != nil { @@ -301,7 +315,7 @@ ) { defer waitGroup.Done() - result, error := p.Dependencies.FetchPodiumByRace(raceId, drivers) + result, error := p.dependencies.FetchPodiumByRace(raceId, drivers) if error != nil { respChan <- fetchPodiumsResponse{