-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add fetch all facilities api (#24)
Signed-off-by: Otieno Calvine <nyarangaotieno@gmail.com>
- Loading branch information
Showing
14 changed files
with
434 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,18 @@ | ||
package dto | ||
|
||
import ( | ||
"github.com/savannahghi/firebasetools" | ||
"github.com/savannahghi/onboarding-service/pkg/onboarding/domain" | ||
) | ||
|
||
// FacilityEdge is used to serialize GraphQL Relay edges for healthcare facilities | ||
type FacilityEdge struct { | ||
Cursor *string `json:"cursor"` | ||
Node *domain.Facility `json:"node"` | ||
} | ||
|
||
// FacilityConnection is used to serialize GraphQL Relay connections for healthcare facilities | ||
type FacilityConnection struct { | ||
Edges []*FacilityEdge `json:"edges"` | ||
PageInfo *firebasetools.PageInfo `json:"pageInfo"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 16 additions & 1 deletion
17
pkg/onboarding/infrastructure/database/postgres/gorm/query.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,34 @@ | ||
package gorm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
) | ||
|
||
// Query contains all the db query methods | ||
type Query interface { | ||
Retrieve(id *int64) (*Facility, error) | ||
GetFacilities(ctx context.Context) ([]Facility, error) | ||
} | ||
|
||
// Retrieve ... | ||
// Retrieve fetches a single facility | ||
func (db *PGInstance) Retrieve(id *int64) (*Facility, error) { | ||
var facility Facility | ||
if err := db.DB.Where(&Facility{FacilityID: id}).First(&facility).Error; err != nil { | ||
return nil, fmt.Errorf("failed to get facility by ID %v: %v", id, err) | ||
} | ||
return &facility, nil | ||
} | ||
|
||
// GetFacilities fetches all the healthcare facilities in the platform. | ||
func (db *PGInstance) GetFacilities(ctx context.Context) ([]Facility, error) { | ||
var facility []Facility | ||
facilities := db.DB.Find(&facility).Error | ||
log.Printf("these are the facilities %v", facilities) | ||
// if err != nil { | ||
// return nil, fmt.Errorf("failed to query all facilities %v", err) | ||
// } | ||
log.Printf("these are the facilities %v", facility) | ||
return facility, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
pkg/onboarding/infrastructure/database/postgres/pg_query.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/savannahghi/onboarding-service/pkg/onboarding/domain" | ||
) | ||
|
||
//GetFacilities returns a slice of healthcare facilities in the platform. | ||
func (d *OnboardingDb) GetFacilities(ctx context.Context) ([]*domain.Facility, error) { | ||
var facility []*domain.Facility | ||
facilities, err := d.query.GetFacilities(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get facilities: %v", err) | ||
} | ||
|
||
if len(facilities) == 0 { | ||
return facility, nil | ||
} | ||
for _, m := range facilities { | ||
singleFacility := domain.Facility{ | ||
ID: int64(m.ID), | ||
Name: m.Name, | ||
Code: m.Code, | ||
Active: m.Active, | ||
County: m.County, | ||
Description: m.Description, | ||
} | ||
|
||
facility = append(facility, &singleFacility) | ||
} | ||
|
||
return facility, nil | ||
} |
98 changes: 98 additions & 0 deletions
98
pkg/onboarding/infrastructure/database/postgres/pg_query_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package postgres | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/savannahghi/onboarding-service/pkg/onboarding/domain" | ||
"github.com/savannahghi/onboarding-service/pkg/onboarding/infrastructure/database/postgres/gorm" | ||
gormMock "github.com/savannahghi/onboarding-service/pkg/onboarding/infrastructure/database/postgres/gorm/mock" | ||
) | ||
|
||
func TestOnboardingDb_GetFacilities(t *testing.T) { | ||
ctx := context.Background() | ||
id := int64(1) | ||
name := "Kanairo One" | ||
code := "KN001" | ||
county := "Kanairo" | ||
description := "This is just for mocking" | ||
|
||
facility := &domain.Facility{ | ||
ID: id, | ||
Name: name, | ||
Code: code, | ||
Active: true, | ||
County: county, | ||
Description: description, | ||
} | ||
|
||
facilityData := []*domain.Facility{} | ||
facilityData = append(facilityData, facility) | ||
type args struct { | ||
ctx context.Context | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []*domain.Facility | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "happy case - valid payload", | ||
args: args{ctx: ctx}, | ||
want: facilityData, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "sad case - facility want data not given", | ||
args: args{ctx: ctx}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var fakeGorm = gormMock.NewGormMock() | ||
d := NewOnboardingDb(fakeGorm, fakeGorm) | ||
|
||
if tt.name == "sad case - facility want data not given" { | ||
fakeGorm.GetFacilitiesFn = func(ctx context.Context) ([]gorm.Facility, error) { | ||
return nil, fmt.Errorf("failed to get facilities") | ||
} | ||
} | ||
if tt.name == "happy case - valid payload" { | ||
fakeGorm.GetFacilitiesFn = func(ctx context.Context) ([]gorm.Facility, error) { | ||
var facilities []gorm.Facility | ||
facilityID := int64(1) | ||
name := "Kanairo One" | ||
code := "KN001" | ||
county := "Kanairo" | ||
description := "This is just for mocking" | ||
facilities = append(facilities, gorm.Facility{ | ||
FacilityID: &facilityID, | ||
Name: name, | ||
Code: code, | ||
Active: true, | ||
County: county, | ||
Description: description, | ||
}) | ||
return facilities, nil | ||
} | ||
} | ||
got, err := d.GetFacilities(tt.args.ctx) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("OnboardingDb.GetFacilities() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if tt.wantErr && got != nil { | ||
t.Errorf("expected facilities to be nil for %v", tt.name) | ||
return | ||
} | ||
|
||
if !tt.wantErr && got == nil { | ||
t.Errorf("expected facilities not to be nil for %v", tt.name) | ||
return | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
|
||
extend type Mutation { | ||
createFacility(input: FacilityInput!): Facility! | ||
} | ||
|
||
extend type Query { | ||
fetchFacilities: [Facility] | ||
} |
Oops, something went wrong.