Skip to content

Commit

Permalink
Merge pull request #8 from mmadariaga/improved-encapsulation
Browse files Browse the repository at this point in the history
Improved encapsulation
  • Loading branch information
mmadariaga authored Oct 15, 2024
2 parents b3e5f62 + e4a4d65 commit 7bb7c74
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
38 changes: 26 additions & 12 deletions api/coverage/coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,25 @@
}

type RacesByYear struct {
Dependencies GetRacesByYearDependencies
dependencies GetRacesByYearDependencies
}

func NewRacesByYear(dependencies GetRacesByYearDependencies) *RacesByYear <span class="cov8" title="1">{
return &amp;RacesByYear{
dependencies: dependencies,
}
}</span>

func (r *RacesByYear) Get(
year int,
) ([]Response, error) <span class="cov8" title="1">{

races, error := r.Dependencies.FetchRacesByYear(year)
races, error := r.dependencies.FetchRacesByYear(year)
if error != nil </span><span class="cov8" title="1">{
return nil, error
}</span>

<span class="cov8" title="1">podiumByRace := domain_service.PodiumByRace{
Dependencies: r.Dependencies,
}
<span class="cov8" title="1">podiumByRace := domain_service.NewPodiumByRace(r.dependencies)

podiumsByRace, error := podiumByRace.Get(races)
if error != nil </span><span class="cov8" title="1">{
Expand Down Expand Up @@ -164,9 +168,15 @@
}

type DriversByRace struct {
Dependencies GetDriversByRaceDependencies
dependencies GetDriversByRaceDependencies
}

func NewDriversByRace(dependencies GetDriversByRaceDependencies) *DriversByRace <span class="cov8" title="1">{
return &amp;DriversByRace{
dependencies: dependencies,
}
}</span>

func (d *DriversByRace) Get(races []domain_model.Race) (map[int][]domain_model.Driver, error) <span class="cov8" title="1">{

driversByRace := make(map[int][]domain_model.Driver)
Expand Down Expand Up @@ -207,7 +217,7 @@
) <span class="cov8" title="1">{
defer waitGroup.Done()

result, error := d.Dependencies.FetchDriversByRace(raceId)
result, error := d.dependencies.FetchDriversByRace(raceId)

if error != nil </span><span class="cov8" title="1">{
respChan &lt;- fetchDriversResponse{
Expand Down Expand Up @@ -246,18 +256,22 @@
}

type PodiumByRace struct {
Dependencies GetPodiumByRaceDependencies
dependencies GetPodiumByRaceDependencies
}

func NewPodiumByRace(dependencies GetPodiumByRaceDependencies) *PodiumByRace <span class="cov8" title="1">{
return &amp;PodiumByRace{
dependencies: dependencies,
}
}</span>

func (p *PodiumByRace) Get(
races []domain_model.Race,
) (map[int][3]domain_model.Podium, error) <span class="cov8" title="1">{

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 </span><span class="cov8" title="1">{
Expand Down Expand Up @@ -301,7 +315,7 @@
) <span class="cov8" title="1">{
defer waitGroup.Done()

result, error := p.Dependencies.FetchPodiumByRace(raceId, drivers)
result, error := p.dependencies.FetchPodiumByRace(raceId, drivers)

if error != nil </span><span class="cov8" title="1">{
respChan &lt;- fetchPodiumsResponse{
Expand Down
14 changes: 9 additions & 5 deletions api/internal/application/races/RacesByYear.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 4 additions & 12 deletions api/internal/application/races/RacesByYear_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ func TestGetRacesByYear(t *testing.T) {
nil,
)

RacesByYear := RacesByYear{
Dependencies: mockInfraDependencies,
}
RacesByYear := NewRacesByYear(mockInfraDependencies)

result, error := RacesByYear.Get(2024)

Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions api/internal/domain/service/DriversByRace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down
14 changes: 9 additions & 5 deletions api/internal/domain/service/PodiumByRace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions api/internal/infrastructure/controller/Races.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 7bb7c74

Please sign in to comment.