Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

cleanup

goenning7a60e51a1889
diff --git a/handlers/router_test.go b/handlers/router_test.go
index 87b4c67cc..06625702c 100644
--- a/handlers/router_test.go
+++ b/handlers/router_test.go
@@ -13,11 +13,11 @@ import (
func makeRequest(method, url string) (int, *jsonq.JsonQuery) {
ctx := context.WchyContext{
- Health: services.NewInMemoryHealthCheckService(false),
- Tenant: services.NewInMemoryTenantService([]*models.Tenant{
+ Health: &services.InMemoryHealthCheckService{Status: false},
+ Tenant: &services.InMemoryTenantService{Tenants: []*models.Tenant{
&models.Tenant{ID: 1, Name: "Orange Inc.", Domain: "orange"},
&models.Tenant{ID: 2, Name: "The Triathlon Shop", Domain: "trishop"},
- }),
+ }},
Settings: context.WchySettings{
BuildTime: "today",
},
diff --git a/main.go b/main.go
index 412c11e74..97a846d5a 100644
--- a/main.go
+++ b/main.go
@@ -24,8 +24,8 @@ func init() {
db, _ = sql.Open("postgres", os.Getenv("DATABASE_URL"))
ctx = context.WchyContext{
- Health: services.NewPostgresHealthCheckService(db),
- Tenant: services.NewPostgresTenantService(db),
+ Health: &services.PostgresHealthCheckService{DB: db},
+ Tenant: &services.PostgresTenantService{DB: db},
Settings: context.WchySettings{
BuildTime: buildtime,
},
diff --git a/services/health.go b/services/health.go
index 6ce03939d..b160223da 100644
--- a/services/health.go
+++ b/services/health.go
@@ -1,6 +1,33 @@
package services
+import "database/sql"
+
// HealthCheckService is a simple general purpose health check service
type HealthCheckService interface {
IsDatabaseOnline() bool
}
+
+// PostgresHealthCheckService is a simple general purpose health check service
+type PostgresHealthCheckService struct {
+ DB *sql.DB
+}
+
+// IsDatabaseOnline checks if database is online
+func (svc PostgresHealthCheckService) IsDatabaseOnline() bool {
+ _, err := svc.DB.Query("SELECT now()")
+ if err != nil {
+ return false
+ }
+
+ return true
+}
+
+// InMemoryHealthCheckService is a simple general purpose health check service
+type InMemoryHealthCheckService struct {
+ Status bool
+}
+
+// IsDatabaseOnline checks if database is online
+func (svc InMemoryHealthCheckService) IsDatabaseOnline() bool {
+ return svc.Status
+}
diff --git a/services/health_inmemory.go b/services/health_inmemory.go
deleted file mode 100644
index a7d540fe6..000000000
--- a/services/health_inmemory.go
+++ /dev/null
@@ -1,16 +0,0 @@
-package services
-
-// InMemoryHealthCheckService is a simple general purpose health check service
-type InMemoryHealthCheckService struct {
- status bool
-}
-
-// NewInMemoryHealthCheckService creates a new InMemoryHealthCheckService
-func NewInMemoryHealthCheckService(status bool) *InMemoryHealthCheckService {
- return &InMemoryHealthCheckService{status: status}
-}
-
-// IsDatabaseOnline checks if database is online
-func (svc InMemoryHealthCheckService) IsDatabaseOnline() bool {
- return svc.status
-}
diff --git a/services/health_postgres.go b/services/health_postgres.go
deleted file mode 100644
index d458c6a92..000000000
--- a/services/health_postgres.go
+++ /dev/null
@@ -1,30 +0,0 @@
-package services
-
-import (
- "database/sql"
-
- logging "github.com/op/go-logging"
-)
-
-var log = logging.MustGetLogger("services/health")
-
-// PostgresHealthCheckService is a simple general purpose health check service
-type PostgresHealthCheckService struct {
- db *sql.DB
-}
-
-// NewPostgresHealthCheckService creates a new PostgresHealthCheckService
-func NewPostgresHealthCheckService(db *sql.DB) *PostgresHealthCheckService {
- return &PostgresHealthCheckService{db}
-}
-
-// IsDatabaseOnline checks if database is online
-func (svc PostgresHealthCheckService) IsDatabaseOnline() bool {
- _, err := svc.db.Query("SELECT now()")
- if err != nil {
- log.Error(err)
- return false
- }
-
- return true
-}
diff --git a/services/health_postgres_test.go b/services/health_test.go
similarity index 91%
rename from services/health_postgres_test.go
rename to services/health_test.go
index d0307f613..c44015605 100644
--- a/services/health_postgres_test.go
+++ b/services/health_test.go
@@ -13,7 +13,7 @@ func TestIsDatabaseOnline_ShouldReturnFalseIfQueryFails(t *testing.T) {
defer db.Close()
mock.ExpectQuery("SELECT now()").WillReturnError(fmt.Errorf("some error"))
- svc := NewPostgresHealthCheckService(db)
+ svc := &PostgresHealthCheckService{DB: db}
isOnline := svc.IsDatabaseOnline()
assert.Equal(t, false, isOnline)
@@ -29,7 +29,7 @@ func TestIsDatabaseOnline_ShouldReturnTrueIfQuerySucceed(t *testing.T) {
mock.ExpectQuery("SELECT now()").WillReturnRows(sqlmock.NewRows([]string{}))
- svc := NewPostgresHealthCheckService(db)
+ svc := &PostgresHealthCheckService{DB: db}
isOnline := svc.IsDatabaseOnline()
assert.Equal(t, true, isOnline)
diff --git a/services/tenant.go b/services/tenant.go
index fedc04c46..2aa856cbf 100644
--- a/services/tenant.go
+++ b/services/tenant.go
@@ -1,8 +1,45 @@
package services
-import "github.com/WeCanHearYou/wchy-api/models"
+import (
+ "database/sql"
+
+ "github.com/WeCanHearYou/wchy-api/models"
+)
// TenantService contains read and write operations for tenants
type TenantService interface {
GetByDomain(domain string) (*models.Tenant, error)
}
+
+// InMemoryTenantService contains read and write operations for tenants
+type InMemoryTenantService struct {
+ Tenants []*models.Tenant
+}
+
+// GetByDomain returns a tenant based on its domain
+func (svc InMemoryTenantService) GetByDomain(domain string) (*models.Tenant, error) {
+ for _, tenant := range svc.Tenants {
+ if tenant.Domain == domain {
+ return tenant, nil
+ }
+ }
+ return nil, ErrNotFound
+}
+
+// PostgresTenantService contains read and write operations for tenants
+type PostgresTenantService struct {
+ DB *sql.DB
+}
+
+// GetByDomain returns a tenant based on its domain
+func (svc PostgresTenantService) GetByDomain(domain string) (*models.Tenant, error) {
+ tenant := &models.Tenant{}
+
+ row := svc.DB.QueryRow("SELECT id, name, domain FROM tenants WHERE domain = $1", domain)
+ err := row.Scan(&tenant.ID, &tenant.Name, &tenant.Domain)
+ if err == sql.ErrNoRows {
+ return nil, ErrNotFound
+ }
+
+ return tenant, nil
+}
diff --git a/services/tenant_inmemory.go b/services/tenant_inmemory.go
deleted file mode 100644
index 3013b8e6e..000000000
--- a/services/tenant_inmemory.go
+++ /dev/null
@@ -1,23 +0,0 @@
-package services
-
-import "github.com/WeCanHearYou/wchy-api/models"
-
-// InMemoryTenantService contains read and write operations for tenants
-type InMemoryTenantService struct {
- tenants []*models.Tenant
-}
-
-// NewInMemoryTenantService creates a new InMemoryTenantService
-func NewInMemoryTenantService(tenants []*models.Tenant) *InMemoryTenantService {
- return &InMemoryTenantService{tenants}
-}
-
-// GetByDomain returns a tenant based on its domain
-func (svc InMemoryTenantService) GetByDomain(domain string) (*models.Tenant, error) {
- for _, tenant := range svc.tenants {
- if tenant.Domain == domain {
- return tenant, nil
- }
- }
- return nil, ErrNotFound
-}
diff --git a/services/tenant_postgres.go b/services/tenant_postgres.go
deleted file mode 100644
index 5f10f78b8..000000000
--- a/services/tenant_postgres.go
+++ /dev/null
@@ -1,30 +0,0 @@
-package services
-
-import (
- "database/sql"
-
- "github.com/WeCanHearYou/wchy-api/models"
-)
-
-// PostgresTenantService contains read and write operations for tenants
-type PostgresTenantService struct {
- db *sql.DB
-}
-
-// NewPostgresTenantService creates a new PostgresTenantService
-func NewPostgresTenantService(db *sql.DB) *PostgresTenantService {
- return &PostgresTenantService{db}
-}
-
-// GetByDomain returns a tenant based on its domain
-func (svc PostgresTenantService) GetByDomain(domain string) (*models.Tenant, error) {
- tenant := &models.Tenant{}
-
- row := svc.db.QueryRow("SELECT id, name, domain FROM tenants WHERE domain = $1", domain)
- err := row.Scan(&tenant.ID, &tenant.Name, &tenant.Domain)
- if err == sql.ErrNoRows {
- return nil, ErrNotFound
- }
-
- return tenant, nil
-}
diff --git a/services/tenant_postgres_test.go b/services/tenant_test.go
similarity index 93%
rename from services/tenant_postgres_test.go
rename to services/tenant_test.go
index 689b9e256..1c2f254e5 100644
--- a/services/tenant_postgres_test.go
+++ b/services/tenant_test.go
@@ -15,7 +15,7 @@ func TestTenantByDomain_WhenDomainIsUnknown_ShouldReturnError(t *testing.T) {
mock.ExpectQuery("SELECT id, name, domain FROM tenants WHERE domain = \\$1").WithArgs("mydomain").WillReturnError(sql.ErrNoRows)
- svc := NewPostgresTenantService(db)
+ svc := &PostgresTenantService{DB: db}
tenant, err := svc.GetByDomain("mydomain")
assert.Nil(t, tenant)
@@ -29,7 +29,7 @@ func TestTenantByDomain_WhenDomainIsKnown_ShouldReturnTenantInformation(t *testi
rows := sqlmock.NewRows([]string{"id", "name", "domain"}).AddRow(234, "My Domain Inc.", "mydomain")
mock.ExpectQuery("SELECT id, name, domain FROM tenants WHERE domain = \\$1").WithArgs("mydomain").WillReturnRows(rows)
- svc := NewPostgresTenantService(db)
+ svc := &PostgresTenantService{DB: db}
tenant, err := svc.GetByDomain("mydomain")
assert.Equal(t, tenant.ID, 234)