Open platform to collect and prioritize feedback
Services -> Storage
goenningbbd04185769c
diff --git a/app/feedback/handler.go b/app/feedback/handler.goindex b82666745..e6e2da575 100644--- a/app/feedback/handler.go+++ b/app/feedback/handler.go@@ -4,23 +4,24 @@ import ("strings""github.com/WeCanHearYou/wechy/app"+ "github.com/WeCanHearYou/wechy/app/storage""github.com/labstack/echo")// AllHandlers contains multiple feedback HTTP handlerstype AllHandlers struct {- ideaService IdeaService+ ideas storage.Idea}// Handlers handles feedback based page-func Handlers(ideaService IdeaService) AllHandlers {- return AllHandlers{ideaService}+func Handlers(ideas storage.Idea) AllHandlers {+ return AllHandlers{ideas}}// List all tenant ideasfunc (h AllHandlers) List() app.HandlerFunc {return func(c app.Context) error {- ideas, err := h.ideaService.GetAll(c.Tenant().ID)+ ideas, err := h.ideas.GetAll(c.Tenant().ID)if err != nil {return c.Failure(err)}@@ -50,7 +51,7 @@ func (h AllHandlers) PostIdea() app.HandlerFunc {})}- idea, err := h.ideaService.Save(c.Tenant().ID, c.User().ID, input.Title, input.Description)+ idea, err := h.ideas.Save(c.Tenant().ID, c.User().ID, input.Title, input.Description)if err != nil {return c.Failure(err)}@@ -71,7 +72,7 @@ func (h AllHandlers) Details() app.HandlerFunc {return c.Failure(err)}- idea, err := h.ideaService.GetByNumber(tenant.ID, number)+ idea, err := h.ideas.GetByNumber(tenant.ID, number)if err != nil {if err == app.ErrNotFound {return c.NotFound()@@ -79,7 +80,7 @@ func (h AllHandlers) Details() app.HandlerFunc {return c.Failure(err)}- comments, err := h.ideaService.GetCommentsByIdeaID(tenant.ID, idea.ID)+ comments, err := h.ideas.GetCommentsByIdeaID(tenant.ID, idea.ID)if err != nil {return c.Failure(err)}@@ -114,7 +115,7 @@ func (h AllHandlers) PostComment() app.HandlerFunc {return c.Failure(err)}- id, err := h.ideaService.AddComment(c.User().ID, ideaID, input.Content)+ id, err := h.ideas.AddComment(c.User().ID, ideaID, input.Content)if err != nil {return c.Failure(err)}diff --git a/app/feedback/handler_test.go b/app/feedback/handler_test.goindex e608184be..8b91cc29d 100644--- a/app/feedback/handler_test.go+++ b/app/feedback/handler_test.go@@ -10,12 +10,12 @@ import (. "github.com/onsi/gomega")-type mockIdeaService struct {+type mockIdeaStorage struct {ideas []*models.Idea}-func NewMockIdeaService() *mockIdeaService {- return &mockIdeaService{+func NewMockIdeaStorage() *mockIdeaStorage {+ return &mockIdeaStorage{ideas: []*models.Idea{&models.Idea{ID: 1, Number: 1, Title: "Idea #1"},&models.Idea{ID: 2, Number: 2, Title: "Idea #2"},@@ -23,7 +23,7 @@ func NewMockIdeaService() *mockIdeaService {}}-func (svc mockIdeaService) GetByID(tenantID, ideaID int) (*models.Idea, error) {+func (svc mockIdeaStorage) GetByID(tenantID, ideaID int) (*models.Idea, error) {for _, idea := range svc.ideas {if idea.ID == ideaID {return idea, nil@@ -32,7 +32,7 @@ func (svc mockIdeaService) GetByID(tenantID, ideaID int) (*models.Idea, error) {return nil, app.ErrNotFound}-func (svc mockIdeaService) GetByNumber(tenantID, number int) (*models.Idea, error) {+func (svc mockIdeaStorage) GetByNumber(tenantID, number int) (*models.Idea, error) {for _, idea := range svc.ideas {if idea.Number == number {return idea, nil@@ -41,19 +41,19 @@ func (svc mockIdeaService) GetByNumber(tenantID, number int) (*models.Idea, erroreturn nil, app.ErrNotFound}-func (svc mockIdeaService) GetAll(tenantID int) ([]*models.Idea, error) {+func (svc mockIdeaStorage) GetAll(tenantID int) ([]*models.Idea, error) {return svc.ideas, nil}-func (svc mockIdeaService) GetCommentsByIdeaID(tenantID, ideaID int) ([]*models.Comment, error) {+func (svc mockIdeaStorage) GetCommentsByIdeaID(tenantID, ideaID int) ([]*models.Comment, error) {return make([]*models.Comment, 0), nil}-func (svc mockIdeaService) Save(tenantID, userID int, title, description string) (*models.Idea, error) {+func (svc mockIdeaStorage) Save(tenantID, userID int, title, description string) (*models.Idea, error) {return nil, nil}-func (svc mockIdeaService) AddComment(userID, ideaID int, content string) (int, error) {+func (svc mockIdeaStorage) AddComment(userID, ideaID int, content string) (int, error) {return 0, nil}@@ -62,7 +62,7 @@ func TestListHandler(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &models.Tenant{ID: 2, Name: "Any Tenant"})- code, _ := server.Execute(feedback.Handlers(NewMockIdeaService()).List())+ code, _ := server.Execute(feedback.Handlers(NewMockIdeaStorage()).List())Expect(code).To(Equal(200))}@@ -75,7 +75,7 @@ func TestDetailsHandler(t *testing.T) {server.Context.SetParamNames("number")server.Context.SetParamValues("1")- code, _ := server.Execute(feedback.Handlers(NewMockIdeaService()).Details())+ code, _ := server.Execute(feedback.Handlers(NewMockIdeaStorage()).Details())Expect(code).To(Equal(200))}@@ -88,7 +88,7 @@ func TestDetailsHandler_NotFound(t *testing.T) {server.Context.SetParamNames("number")server.Context.SetParamValues("99")- code, _ := server.Execute(feedback.Handlers(NewMockIdeaService()).Details())+ code, _ := server.Execute(feedback.Handlers(NewMockIdeaStorage()).Details())Expect(code).To(Equal(404))}@@ -99,7 +99,7 @@ func TestDetailsHandler_AddIdea(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &models.Tenant{ID: 2, Name: "Any Tenant"})server.Context.Set("__CTX_USER", &models.User{ID: 1, Name: "Jon"})- handler := feedback.Handlers(NewMockIdeaService()).PostIdea()+ handler := feedback.Handlers(NewMockIdeaStorage()).PostIdea()code, _ := server.ExecutePost(handler, `{ "title": "My newest idea :)" }`)Expect(code).To(Equal(200))@@ -111,7 +111,7 @@ func TestDetailsHandler_AddIdea_WithoutTitle(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &models.Tenant{ID: 2, Name: "Any Tenant"})server.Context.Set("__CTX_USER", &models.User{ID: 1, Name: "Jon"})- handler := feedback.Handlers(NewMockIdeaService()).PostIdea()+ handler := feedback.Handlers(NewMockIdeaStorage()).PostIdea()code, _ := server.ExecutePost(handler, `{ "title": "" }`)Expect(code).To(Equal(400))@@ -125,7 +125,7 @@ func TestDetailsHandler_AddComment(t *testing.T) {server.Context.Set("__CTX_USER", &models.User{ID: 1, Name: "Jon"})server.Context.SetParamNames("id")server.Context.SetParamValues("1")- handler := feedback.Handlers(NewMockIdeaService()).PostComment()+ handler := feedback.Handlers(NewMockIdeaStorage()).PostComment()code, _ := server.ExecutePost(handler, `{ "content": "This is a comment!" }`)Expect(code).To(Equal(200))@@ -139,7 +139,7 @@ func TestDetailsHandler_AddComment_WithoutContent(t *testing.T) {server.Context.Set("__CTX_USER", &models.User{ID: 1, Name: "Jon"})server.Context.SetParamNames("id")server.Context.SetParamValues("1")- handler := feedback.Handlers(NewMockIdeaService()).PostComment()+ handler := feedback.Handlers(NewMockIdeaStorage()).PostComment()code, _ := server.ExecutePost(handler, `{ "content": "" }`)Expect(code).To(Equal(400))diff --git a/app/identity/handler.go b/app/identity/handler.goindex 418daaafc..0c3d07bcc 100644--- a/app/identity/handler.go+++ b/app/identity/handler.go@@ -9,6 +9,7 @@ import ("github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/infra""github.com/WeCanHearYou/wechy/app/models"+ "github.com/WeCanHearYou/wechy/app/storage")type oauthUserProfile struct {@@ -19,14 +20,14 @@ type oauthUserProfile struct {// OAuthHandlers contains multiple oauth HTTP handlerstype OAuthHandlers struct {- tenantService TenantService+ tenantStorage storage.TenantoauthService OAuthService- userService UserService+ userStorage storage.User}// OAuth creates a new OAuthHandlers-func OAuth(tenantService TenantService, oauthService OAuthService, userService UserService) OAuthHandlers {- return OAuthHandlers{tenantService, oauthService, userService}+func OAuth(tenantStorage storage.Tenant, oauthService OAuthService, userStorage storage.User) OAuthHandlers {+ return OAuthHandlers{tenantStorage, oauthService, userStorage}}// Callback handles OAuth callbacks@@ -44,7 +45,7 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {return c.Redirect(http.StatusTemporaryRedirect, redirect)}- tenant, err := h.tenantService.GetByDomain(stripPort(redirectURL.Host))+ tenant, err := h.tenantStorage.GetByDomain(stripPort(redirectURL.Host))if err != nil {return c.Failure(err)}@@ -54,7 +55,7 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {return c.Failure(err)}- user, err := h.userService.GetByEmail(tenant.ID, oauthUser.Email)+ user, err := h.userStorage.GetByEmail(tenant.ID, oauthUser.Email)if err != nil {if err == app.ErrNotFound {user = &models.User{@@ -70,7 +71,7 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {},}- err = h.userService.Register(user)+ err = h.userStorage.Register(user)if err != nil {return c.Failure(err)}@@ -78,7 +79,7 @@ func (h OAuthHandlers) Callback(provider string) app.HandlerFunc {return c.Failure(err)}} else if !user.HasProvider(provider) {- err = h.userService.RegisterProvider(user.ID, &models.UserProvider{+ err = h.userStorage.RegisterProvider(user.ID, &models.UserProvider{UID: oauthUser.ID,Name: provider,})diff --git a/app/identity/handler_test.go b/app/identity/handler_test.goindex 956c8d396..15cbd8324 100644--- a/app/identity/handler_test.go+++ b/app/identity/handler_test.go@@ -51,7 +51,7 @@ func (p OAuthService) GetProfile(provider string, code string) (*identity.OAuthUfunc handlers() identity.OAuthHandlers {setup()- return identity.OAuth(tenantService, oauthService, userService)+ return identity.OAuth(TenantStorage, oauthService, UserStorage)}func TestLoginHandler(t *testing.T) {diff --git a/app/identity/setup_test.go b/app/identity/setup_test.goindex 8c444167d..7f2e86128 100644--- a/app/identity/setup_test.go+++ b/app/identity/setup_test.go@@ -2,20 +2,20 @@ package identity_testimport ("github.com/WeCanHearYou/wechy/app/pkg/dbx"- "github.com/WeCanHearYou/wechy/app/postgres"+ "github.com/WeCanHearYou/wechy/app/storage/postgres")var (db *dbx.DatabaseoauthService *OAuthService- tenantService *postgres.TenantService- userService *postgres.UserService+ TenantStorage *postgres.TenantStorage+ UserStorage *postgres.UserStorage)func setup() {db, _ = dbx.New()oauthService = &OAuthService{}- tenantService = &postgres.TenantService{DB: db}- userService = &postgres.UserService{DB: db}+ TenantStorage = &postgres.TenantStorage{DB: db}+ UserStorage = &postgres.UserStorage{DB: db}}diff --git a/app/infra/handler.go b/app/infra/handler.goindex 2ff3369cd..5691ef63d 100644--- a/app/infra/handler.go+++ b/app/infra/handler.go@@ -12,24 +12,20 @@ import ()type statusHandler struct {- healthService HealthCheckService- settings *models.WechySettings+ settings *models.WechySettings}// Status creates a new Status HTTP handler-func Status(healthService HealthCheckService, settings *models.WechySettings) app.HandlerFunc {- return statusHandler{healthService, settings}.get()+func Status(settings *models.WechySettings) app.HandlerFunc {+ return statusHandler{settings}.get()}func (h statusHandler) get() app.HandlerFunc {return func(c app.Context) error {return c.JSON(http.StatusOK, echo.Map{- "build": h.settings.BuildTime,- "version": h.settings.Version,- "env": env.Current(),- "healthy": echo.Map{- "database": h.healthService.IsDatabaseOnline(),- },+ "build": h.settings.BuildTime,+ "version": h.settings.Version,+ "env": env.Current(),"compiler": runtime.Version(),"now": time.Now().Format("2006.01.02.150405"),})diff --git a/app/infra/handler_test.go b/app/infra/handler_test.goindex 429d7a2f4..7c49a29af 100644--- a/app/infra/handler_test.go+++ b/app/infra/handler_test.go@@ -9,24 +9,16 @@ import (. "github.com/onsi/gomega")-type falsyHealthCheckService struct{}--func (svc falsyHealthCheckService) IsDatabaseOnline() bool {- return false-}-func TestStatusHandler(t *testing.T) {RegisterTestingT(t)- service := &falsyHealthCheckService{}settings := &models.WechySettings{BuildTime: "today",}server := mock.NewServer()- status, query := server.Execute(infra.Status(service, settings))+ status, query := server.Execute(infra.Status(settings))Expect(query.String("build")).To(Equal("today"))- Expect(query.Bool("healthy", "database")).To(Equal(false))Expect(status).To(Equal(200))}diff --git a/app/infra/service.go b/app/infra/service.godeleted file mode 100644index c7619a9dc..000000000--- a/app/infra/service.go+++ /dev/null@@ -1,6 +0,0 @@-package infra--// HealthCheckService is a simple general purpose health check service-type HealthCheckService interface {- IsDatabaseOnline() bool-}diff --git a/app/middlewares/jwt.go b/app/middlewares/jwt.goindex 38240144c..604e92bc9 100644--- a/app/middlewares/jwt.go+++ b/app/middlewares/jwt.go@@ -5,16 +5,16 @@ import ("strings""github.com/WeCanHearYou/wechy/app"- "github.com/WeCanHearYou/wechy/app/identity""github.com/WeCanHearYou/wechy/app/infra"+ "github.com/WeCanHearYou/wechy/app/storage")// MultiTenant extract tenant information from hostname and inject it into current context-func MultiTenant(tenantService identity.TenantService) app.MiddlewareFunc {+func MultiTenant(tenants storage.Tenant) app.MiddlewareFunc {return func(next app.HandlerFunc) app.HandlerFunc {return func(c app.Context) error {hostname := stripPort(c.Request().Host)- tenant, err := tenantService.GetByDomain(hostname)+ tenant, err := tenants.GetByDomain(hostname)if err == nil {c.SetTenant(tenant)return next(c)@@ -27,13 +27,13 @@ func MultiTenant(tenantService identity.TenantService) app.MiddlewareFunc {}// JwtGetter gets JWT token from cookie and add into context-func JwtGetter(userService identity.UserService) app.MiddlewareFunc {+func JwtGetter(users storage.User) app.MiddlewareFunc {return func(next app.HandlerFunc) app.HandlerFunc {return func(c app.Context) error {if cookie, err := c.Cookie("auth"); err == nil {if claims, err := infra.Decode(cookie.Value); err == nil {- if user, err := userService.GetByID(claims.UserID); err == nil {+ if user, err := users.GetByID(claims.UserID); err == nil {if user.Tenant.ID == c.Tenant().ID {c.SetUser(user)}diff --git a/app/middlewares/jwt_test.go b/app/middlewares/jwt_test.goindex 06b8a1723..234ecf6b3 100644--- a/app/middlewares/jwt_test.go+++ b/app/middlewares/jwt_test.go@@ -11,7 +11,7 @@ import ("github.com/WeCanHearYou/wechy/app/mock""github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/dbx"- "github.com/WeCanHearYou/wechy/app/postgres"+ "github.com/WeCanHearYou/wechy/app/storage/postgres""github.com/labstack/echo". "github.com/onsi/gomega")@@ -42,20 +42,20 @@ var testCases = []struct {var (db *dbx.Database- tenantService *postgres.TenantService- userService *postgres.UserService+ TenantStorage *postgres.TenantStorage+ UserStorage *postgres.UserStorage)func setup() {db, _ = dbx.New()- tenantService = &postgres.TenantService{DB: db}- userService = &postgres.UserService{DB: db}+ TenantStorage = &postgres.TenantStorage{DB: db}+ UserStorage = &postgres.UserStorage{DB: db}}-type mockTenantService struct{}+type mockTenantStorage struct{}-func (svc mockTenantService) GetByDomain(domain string) (*models.Tenant, error) {+func (svc mockTenantStorage) GetByDomain(domain string) (*models.Tenant, error) {for _, testCase := range testCases {if testCase.domain == domain {return testCase.tenant, nil@@ -76,7 +76,7 @@ func TestMultiTenant(t *testing.T) {c := server.NewContext(req, rec)c.Request().Host = host- mw := middlewares.MultiTenant(&mockTenantService{})+ mw := middlewares.MultiTenant(&mockTenantStorage{})mw(func(c app.Context) error {return c.String(http.StatusOK, c.Tenant().Name)})(c)@@ -96,7 +96,7 @@ func TestMultiTenant_UnknownDomain(t *testing.T) {c := server.NewContext(req, rec)c.Request().Host = "somedomain.com"- mw := middlewares.MultiTenant(&mockTenantService{})+ mw := middlewares.MultiTenant(&mockTenantStorage{})mw(func(c app.Context) error {return c.String(http.StatusOK, c.Tenant().Name)})(c)@@ -113,7 +113,7 @@ func TestJwtGetter_NoCookie(t *testing.T) {rec := httptest.NewRecorder()c := server.NewContext(req, rec)- mw := middlewares.JwtGetter(userService)+ mw := middlewares.JwtGetter(UserStorage)mw(func(c app.Context) error {if c.IsAuthenticated() {return c.NoContent(http.StatusOK)@@ -144,7 +144,7 @@ func TestJwtGetter_WithCookie(t *testing.T) {Value: token,})- mw := middlewares.JwtGetter(userService)+ mw := middlewares.JwtGetter(UserStorage)mw(func(c app.Context) error {return c.String(http.StatusOK, c.User().Name)})(c)@@ -172,7 +172,7 @@ func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {Value: token,})- mw := middlewares.JwtGetter(userService)+ mw := middlewares.JwtGetter(UserStorage)mw(func(c app.Context) error {if c.User() == nil {return c.NoContent(http.StatusNoContent)diff --git a/app/postgres/infra.go b/app/postgres/infra.godeleted file mode 100644index b3aa53ddb..000000000--- a/app/postgres/infra.go+++ /dev/null@@ -1,19 +0,0 @@-package postgres--import "github.com/WeCanHearYou/wechy/app/pkg/dbx"--// HealthCheckService is a simple general purpose health check service-type HealthCheckService struct {- DB *dbx.Database-}--// IsDatabaseOnline checks if database is online-func (svc *HealthCheckService) IsDatabaseOnline() bool {- rows, err := svc.DB.Query("SELECT now()")- if err != nil {- return false- }-- defer rows.Close()- return true-}diff --git a/app/postgres/infra_test.go b/app/postgres/infra_test.godeleted file mode 100644index 1dcbdd6a8..000000000--- a/app/postgres/infra_test.go+++ /dev/null@@ -1,19 +0,0 @@-package postgres_test--import (- "testing"-- "github.com/WeCanHearYou/wechy/app/pkg/dbx"- "github.com/WeCanHearYou/wechy/app/postgres"- . "github.com/onsi/gomega"-)--func TestHealthCheckService_IsDatabaseOnline_Success(t *testing.T) {- RegisterTestingT(t)- db, _ := dbx.New()- defer db.Close()-- svc := &postgres.HealthCheckService{DB: db}-- Expect(svc.IsDatabaseOnline()).To(BeTrue())-}diff --git a/app/feedback/service.go b/app/storage/feedback.gosimilarity index 79%rename from app/feedback/service.gorename to app/storage/feedback.goindex fbe1e5c38..6cc18137e 100644--- a/app/feedback/service.go+++ b/app/storage/feedback.go@@ -1,9 +1,9 @@-package feedback+package storageimport "github.com/WeCanHearYou/wechy/app/models"-// IdeaService contains read and write operations for ideas-type IdeaService interface {+// Idea contains read and write operations for ideas+type Idea interface {GetByID(tenantID, ideaID int) (*models.Idea, error)GetByNumber(tenantID, number int) (*models.Idea, error)GetCommentsByIdeaID(tenantID, ideaID int) ([]*models.Comment, error)diff --git a/app/identity/service.go b/app/storage/identity.gosimilarity index 63%rename from app/identity/service.gorename to app/storage/identity.goindex 659b80a00..0eb4d07c7 100644--- a/app/identity/service.go+++ b/app/storage/identity.go@@ -1,16 +1,16 @@-package identity+package storageimport "github.com/WeCanHearYou/wechy/app/models"-// UserService is used for user operations-type UserService interface {+// User is used for user operations+type User interface {GetByID(userID int) (*models.User, error)GetByEmail(tenantID int, email string) (*models.User, error)Register(user *models.User) errorRegisterProvider(userID int, provider *models.UserProvider) error}-// TenantService contains read and write operations for tenants-type TenantService interface {+// Tenant contains read and write operations for tenants+type Tenant interface {GetByDomain(domain string) (*models.Tenant, error)}diff --git a/app/postgres/feedback.go b/app/storage/postgres/feedback.gosimilarity index 89%rename from app/postgres/feedback.gorename to app/storage/postgres/feedback.goindex c80f36d9f..dc0f16ca3 100644--- a/app/postgres/feedback.go+++ b/app/storage/postgres/feedback.go@@ -8,13 +8,13 @@ import ("github.com/WeCanHearYou/wechy/app/pkg/dbx")-// IdeaService contains read and write operations for ideas-type IdeaService struct {+// IdeaStorage contains read and write operations for ideas+type IdeaStorage struct {DB *dbx.Database}// GetAll returns all tenant ideas-func (svc *IdeaService) GetAll(tenantID int) ([]*models.Idea, error) {+func (svc *IdeaStorage) GetAll(tenantID int) ([]*models.Idea, error) {rows, err := svc.DB.Query(`SELECT i.id, i.number, i.title, i.description, i.created_on, u.id, u.name, u.emailFROM ideas iINNER JOIN users u@@ -37,7 +37,7 @@ func (svc *IdeaService) GetAll(tenantID int) ([]*models.Idea, error) {}// GetByID returns idea by given id-func (svc *IdeaService) GetByID(tenantID, ideaID int) (*models.Idea, error) {+func (svc *IdeaStorage) GetByID(tenantID, ideaID int) (*models.Idea, error) {rows, err := svc.DB.Query(`SELECT i.id, i.number, i.title, i.description, i.created_on, u.id, u.name, u.emailFROM ideas iINNER JOIN users u@@ -59,7 +59,7 @@ func (svc *IdeaService) GetByID(tenantID, ideaID int) (*models.Idea, error) {}// GetByNumber returns idea by tenant and number-func (svc *IdeaService) GetByNumber(tenantID, number int) (*models.Idea, error) {+func (svc *IdeaStorage) GetByNumber(tenantID, number int) (*models.Idea, error) {rows, err := svc.DB.Query(`SELECT i.id, i.number, i.title, i.description, i.created_on, u.id, u.name, u.emailFROM ideas iINNER JOIN users u@@ -81,7 +81,7 @@ func (svc *IdeaService) GetByNumber(tenantID, number int) (*models.Idea, error)}// GetCommentsByIdeaID returns all coments from given idea-func (svc *IdeaService) GetCommentsByIdeaID(tenantID, ideaID int) ([]*models.Comment, error) {+func (svc *IdeaStorage) GetCommentsByIdeaID(tenantID, ideaID int) ([]*models.Comment, error) {rows, err := svc.DB.Query(`SELECT c.id, c.content, c.created_on, u.id, u.name, u.emailFROM comments cINNER JOIN ideas i@@ -107,7 +107,7 @@ func (svc *IdeaService) GetCommentsByIdeaID(tenantID, ideaID int) ([]*models.Com}// Save a new idea in the database-func (svc *IdeaService) Save(tenantID, userID int, title, description string) (*models.Idea, error) {+func (svc *IdeaStorage) Save(tenantID, userID int, title, description string) (*models.Idea, error) {tx, err := svc.DB.Begin()if err != nil {return nil, err@@ -129,7 +129,7 @@ func (svc *IdeaService) Save(tenantID, userID int, title, description string) (*}// AddComment places a new comment on an idea-func (svc *IdeaService) AddComment(userID, ideaID int, content string) (int, error) {+func (svc *IdeaStorage) AddComment(userID, ideaID int, content string) (int, error) {tx, err := svc.DB.Begin()if err != nil {return 0, errdiff --git a/app/postgres/feedback_test.go b/app/storage/postgres/feedback_test.gosimilarity index 86%rename from app/postgres/feedback_test.gorename to app/storage/postgres/feedback_test.goindex c1615ee13..342c73ac6 100644--- a/app/postgres/feedback_test.go+++ b/app/storage/postgres/feedback_test.go@@ -6,11 +6,11 @@ import ("github.com/WeCanHearYou/wechy/app""github.com/WeCanHearYou/wechy/app/pkg/dbx"- "github.com/WeCanHearYou/wechy/app/postgres"+ "github.com/WeCanHearYou/wechy/app/storage/postgres". "github.com/onsi/gomega")-func TestIdeaService_GetAll(t *testing.T) {+func TestIdeaStorage_GetAll(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()@@ -20,7 +20,7 @@ func TestIdeaService_GetAll(t *testing.T) {db.Execute("INSERT INTO ideas (title, number, description, created_on, tenant_id, user_id) VALUES ('Idea #1', 1, 'Description #1', $1, 300, 300)", now)db.Execute("INSERT INTO ideas (title, number, description, created_on, tenant_id, user_id) VALUES ('Idea #2', 2, 'Description #2', $1, 300, 301)", now)- svc := &postgres.IdeaService{DB: db}+ svc := &postgres.IdeaStorage{DB: db}ideas, err := svc.GetAll(300)Expect(err).To(BeNil())@@ -37,7 +37,7 @@ func TestIdeaService_GetAll(t *testing.T) {Expect(ideas[1].User.Name).To(Equal("Arya Stark"))}-func TestIdeaService_SaveAndGet(t *testing.T) {+func TestIdeaStorage_SaveAndGet(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()@@ -45,7 +45,7 @@ func TestIdeaService_SaveAndGet(t *testing.T) {db.Execute("INSERT INTO tenants (name, subdomain, created_on) VALUES ('My Domain Inc.','mydomain', now())")db.Execute("INSERT INTO users (name, email, created_on, role) VALUES ('Jon Snow','[email protected]', now(), 2)")- svc := &postgres.IdeaService{DB: db}+ svc := &postgres.IdeaStorage{DB: db}idea, err := svc.Save(1, 1, "My new idea", "with this description")Expect(err).To(BeNil())Expect(idea.ID).To(Equal(1))@@ -62,19 +62,19 @@ func TestIdeaService_SaveAndGet(t *testing.T) {Expect(dbIdea.User.Email).To(Equal("[email protected]"))}-func TestIdeaService_GetInvalid(t *testing.T) {+func TestIdeaStorage_GetInvalid(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.IdeaService{DB: db}+ svc := &postgres.IdeaStorage{DB: db}dbIdea, err := svc.GetByID(1, 1)Expect(err).To(Equal(app.ErrNotFound))Expect(dbIdea).To(BeNil())}-func TestIdeaService_AddAndReturnComments(t *testing.T) {+func TestIdeaStorage_AddAndReturnComments(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()@@ -82,7 +82,7 @@ func TestIdeaService_AddAndReturnComments(t *testing.T) {db.Execute("INSERT INTO tenants (name, subdomain, created_on) VALUES ('My Domain Inc.','mydomain', now())")db.Execute("INSERT INTO users (name, email, created_on, role) VALUES ('Jon Snow','[email protected]', now(), 2)")- svc := &postgres.IdeaService{DB: db}+ svc := &postgres.IdeaStorage{DB: db}idea, _ := svc.Save(1, 1, "My new idea", "with this description")svc.AddComment(1, idea.ID, "Comment #1")svc.AddComment(1, idea.ID, "Comment #2")@@ -95,12 +95,12 @@ func TestIdeaService_AddAndReturnComments(t *testing.T) {Expect(comments[1].Content).To(Equal("Comment #1"))}-func TestIdeaService_SaveAndGet_DifferentTenants(t *testing.T) {+func TestIdeaStorage_SaveAndGet_DifferentTenants(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.IdeaService{DB: db}+ svc := &postgres.IdeaStorage{DB: db}svc.Save(300, 300, "My new idea", "with this description")svc.Save(400, 400, "My other idea", "with other description")diff --git a/app/postgres/identity.go b/app/storage/postgres/identity.gosimilarity index 86%rename from app/postgres/identity.gorename to app/storage/postgres/identity.goindex ab65b4f73..be9b43eb6 100644--- a/app/postgres/identity.go+++ b/app/storage/postgres/identity.go@@ -5,18 +5,18 @@ import ("time""github.com/WeCanHearYou/wechy/app"- "github.com/WeCanHearYou/wechy/app/pkg/dbx""github.com/WeCanHearYou/wechy/app/models"+ "github.com/WeCanHearYou/wechy/app/pkg/dbx""github.com/WeCanHearYou/wechy/app/pkg/env")-// UserService is used for user operations using a Postgres database-type UserService struct {+// UserStorage is used for user operations using a Postgres database+type UserStorage struct {DB *dbx.Database}// GetByID returns a user based on given id-func (svc *UserService) GetByID(userID int) (*models.User, error) {+func (svc *UserStorage) GetByID(userID int) (*models.User, error) {user := &models.User{}user.Tenant = &models.Tenant{}row := svc.DB.QueryRow("SELECT id, name, email, tenant_id, role FROM users WHERE id = $1", userID)@@ -41,7 +41,7 @@ func (svc *UserService) GetByID(userID int) (*models.User, error) {}// GetByEmail returns a user based on given email-func (svc *UserService) GetByEmail(tenantID int, email string) (*models.User, error) {+func (svc *UserStorage) GetByEmail(tenantID int, email string) (*models.User, error) {user := &models.User{}user.Tenant = &models.Tenant{}row := svc.DB.QueryRow("SELECT id, name, email, tenant_id, role FROM users WHERE email = $1 AND tenant_id = $2", email, tenantID)@@ -66,7 +66,7 @@ func (svc *UserService) GetByEmail(tenantID int, email string) (*models.User, er}// Register creates a new user based on given information-func (svc *UserService) Register(user *models.User) error {+func (svc *UserStorage) Register(user *models.User) error {tx, err := svc.DB.Begin()if err != nil {return err@@ -90,18 +90,18 @@ func (svc *UserService) Register(user *models.User) error {}// RegisterProvider adds given provider to userID-func (svc *UserService) RegisterProvider(userID int, provider *models.UserProvider) error {+func (svc *UserStorage) RegisterProvider(userID int, provider *models.UserProvider) error {now := time.Now()return svc.DB.Execute("INSERT INTO user_providers (user_id, provider, provider_uid, created_on) VALUES ($1, $2, $3, $4)", userID, provider.Name, provider.UID, now)}-// TenantService contains read and write operations for tenants-type TenantService struct {+// TenantStorage contains read and write operations for tenants+type TenantStorage struct {DB *dbx.Database}// GetByDomain returns a tenant based on its domain-func (svc *TenantService) GetByDomain(domain string) (*models.Tenant, error) {+func (svc *TenantStorage) GetByDomain(domain string) (*models.Tenant, error) {tenant := &models.Tenant{}row := svc.DB.QueryRow("SELECT id, name, subdomain FROM tenants WHERE subdomain = $1 OR cname = $2", extractSubdomain(domain), domain)diff --git a/app/postgres/identity_test.go b/app/storage/postgres/identity_test.gosimilarity index 80%rename from app/postgres/identity_test.gorename to app/storage/postgres/identity_test.goindex da6719918..dc32e8d17 100644--- a/app/postgres/identity_test.go+++ b/app/storage/postgres/identity_test.go@@ -3,19 +3,19 @@ package postgres_testimport ("testing"- "github.com/WeCanHearYou/wechy/app/pkg/dbx""github.com/WeCanHearYou/wechy/app/identity""github.com/WeCanHearYou/wechy/app/models"- "github.com/WeCanHearYou/wechy/app/postgres"+ "github.com/WeCanHearYou/wechy/app/pkg/dbx"+ "github.com/WeCanHearYou/wechy/app/storage/postgres". "github.com/onsi/gomega")-func TestUserService_GetByID(t *testing.T) {+func TestUserStorage_GetByID(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.UserService{DB: db}+ svc := &postgres.UserStorage{DB: db}user, err := svc.GetByID(300)Expect(err).To(BeNil())@@ -28,24 +28,24 @@ func TestUserService_GetByID(t *testing.T) {Expect(user.Tenant.ID).To(Equal(300))}-func TestUserService_GetByEmail_Error(t *testing.T) {+func TestUserStorage_GetByEmail_Error(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.UserService{DB: db}+ svc := &postgres.UserStorage{DB: db}user, err := svc.GetByEmail(300, "[email protected]")Expect(user).To(BeNil())Expect(err).NotTo(BeNil())}-func TestUserService_GetByEmail(t *testing.T) {+func TestUserStorage_GetByEmail(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.UserService{DB: db}+ svc := &postgres.UserStorage{DB: db}user, err := svc.GetByEmail(300, "[email protected]")Expect(err).To(BeNil())@@ -58,24 +58,24 @@ func TestUserService_GetByEmail(t *testing.T) {Expect(user.Tenant.ID).To(Equal(300))}-func TestUserService_GetByEmail_WrongTenant(t *testing.T) {+func TestUserStorage_GetByEmail_WrongTenant(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.UserService{DB: db}+ svc := &postgres.UserStorage{DB: db}user, err := svc.GetByEmail(400, "[email protected]")Expect(user).To(BeNil())Expect(err).NotTo(BeNil())}-func TestUserService_Register(t *testing.T) {+func TestUserStorage_Register(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.UserService{DB: db}+ svc := &postgres.UserStorage{DB: db}user := &models.User{Name: "Rob Stark",Email: "[email protected]",@@ -101,14 +101,14 @@ func TestUserService_Register(t *testing.T) {Expect(user.Email).To(Equal("[email protected]"))}-func TestUserService_Register_MultipleProviders(t *testing.T) {+func TestUserStorage_Register_MultipleProviders(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()db.Execute("INSERT INTO tenants (name, subdomain, created_on) VALUES ('My Domain Inc.','mydomain', now())")- svc := &postgres.UserService{DB: db}+ svc := &postgres.UserStorage{DB: db}user := &models.User{Name: "Jon Snow",Email: "[email protected]",@@ -135,26 +135,26 @@ func TestUserService_Register_MultipleProviders(t *testing.T) {Expect(user.Email).To(Equal("[email protected]"))}-func TestTenantService_GetByDomain_NotFound(t *testing.T) {+func TestTenantStorage_GetByDomain_NotFound(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()- svc := &postgres.TenantService{DB: db}+ svc := &postgres.TenantStorage{DB: db}tenant, err := svc.GetByDomain("mydomain")Expect(tenant).To(BeNil())Expect(err).NotTo(BeNil())}-func TestTenantService_GetByDomain_Subdomain(t *testing.T) {+func TestTenantStorage_GetByDomain_Subdomain(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()db.Execute("INSERT INTO tenants (name, subdomain, created_on) VALUES ('My Domain Inc.','mydomain', now())")- svc := &postgres.TenantService{DB: db}+ svc := &postgres.TenantStorage{DB: db}tenant, err := svc.GetByDomain("mydomain")Expect(tenant.ID).To(Equal(int(1)))@@ -163,14 +163,14 @@ func TestTenantService_GetByDomain_Subdomain(t *testing.T) {Expect(err).To(BeNil())}-func TestTenantService_GetByDomain_FullDomain(t *testing.T) {+func TestTenantStorage_GetByDomain_FullDomain(t *testing.T) {RegisterTestingT(t)db, _ := dbx.New()defer db.Close()db.Execute("INSERT INTO tenants (name, subdomain, cname, created_on) VALUES ('My Domain Inc.','mydomain', 'mydomain.anydomain.com', now())")- svc := &postgres.TenantService{DB: db}+ svc := &postgres.TenantStorage{DB: db}tenant, err := svc.GetByDomain("mydomain.anydomain.com")Expect(tenant.ID).To(Equal(int(1)))diff --git a/main.go b/main.goindex 6811b8a06..cca78c853 100644--- a/main.go+++ b/main.go@@ -5,7 +5,7 @@ import ("github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/dbx""github.com/WeCanHearYou/wechy/app/pkg/env"- "github.com/WeCanHearYou/wechy/app/postgres"+ "github.com/WeCanHearYou/wechy/app/storage/postgres"_ "github.com/mattes/migrate/driver/postgres"mig "github.com/mattes/migrate/migrate"@@ -43,10 +43,9 @@ func main() {ctx := &WechyServices{OAuth: &identity.HTTPOAuthService{},- Health: &postgres.HealthCheckService{DB: db},- Idea: &postgres.IdeaService{DB: db},- User: &postgres.UserService{DB: db},- Tenant: &postgres.TenantService{DB: db},+ Idea: &postgres.IdeaStorage{DB: db},+ User: &postgres.UserStorage{DB: db},+ Tenant: &postgres.TenantStorage{DB: db},Settings: &models.WechySettings{BuildTime: buildtime,Version: version,diff --git a/router.go b/router.goindex c78692b8f..903d3deaa 100644--- a/router.go+++ b/router.go@@ -12,6 +12,7 @@ import ("github.com/WeCanHearYou/wechy/app/middlewares""github.com/WeCanHearYou/wechy/app/models""github.com/WeCanHearYou/wechy/app/pkg/env"+ "github.com/WeCanHearYou/wechy/app/storage""github.com/labstack/echo""github.com/labstack/echo/middleware""github.com/labstack/gommon/log"@@ -20,10 +21,9 @@ import (// WechyServices holds reference to all Wechy servicestype WechyServices struct {OAuth identity.OAuthService- User identity.UserService- Tenant identity.TenantService- Idea feedback.IdeaService- Health infra.HealthCheckService+ User storage.User+ Tenant storage.Tenant+ Idea storage.IdeaSettings *models.WechySettings}@@ -117,7 +117,7 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {get(appGroup, "/", feedback.Handlers(ctx.Idea).List())get(appGroup, "/ideas/:number", feedback.Handlers(ctx.Idea).Details())get(appGroup, "/logout", oauthHandlers.Logout())- get(appGroup, "/api/status", infra.Status(ctx.Health, ctx.Settings))+ get(appGroup, "/api/status", infra.Status(ctx.Settings))}securedGroup := group(router, "/api")