Skip to content

Commit

Permalink
Clean up the handler code
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Jul 7, 2018
1 parent 058f672 commit fd30233
Show file tree
Hide file tree
Showing 34 changed files with 342 additions and 674 deletions.
2 changes: 1 addition & 1 deletion src/app/webapi/component/auth/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ type Endpoint struct {

// Routes will set up the endpoints.
func (p *Endpoint) Routes(router component.IRouter) {
router.Get("/v1/auth", component.H(p.Index))
router.Get("/v1/auth", p.Index)
}
2 changes: 1 addition & 1 deletion src/app/webapi/component/auth/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ func TestIndexError(t *testing.T) {
mux.ServeHTTP(w, r)

assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Equal(t, `{"status":"Internal Server Error","message":"generate error"}`+"\n", w.Body.String())
assert.Equal(t, `generate error`+"\n", w.Body.String())
}
26 changes: 1 addition & 25 deletions src/app/webapi/component/core_mock.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
package component

import (
"log"

"app/webapi/internal/bind"
"app/webapi/internal/response"
"app/webapi/internal/testutil"
"app/webapi/pkg/database"
"app/webapi/pkg/query"
)

// TestDatabase returns a test database.
func TestDatabase(dbSpecificDB bool) *database.DBW {
dbc := new(database.Connection)
dbc.Hostname = "127.0.0.1"
dbc.Port = 3306
dbc.Username = "root"
dbc.Password = ""
dbc.Database = "webapitest"
dbc.Parameter = "parseTime=true&allowNativePasswords=true"

connection, err := dbc.Connect(dbSpecificDB)
if err != nil {
log.Println("DB Error:", err)
}

dbw := database.New(connection)

return dbw
}

// NewCoreMock returns all mocked dependencies.
func NewCoreMock() (Core, *CoreMock) {
ml := new(testutil.MockLogger)
//md := new(testutil.MockDatabase)
md := TestDatabase(true)
md := testutil.ConnectDatabase(true)
mq := query.New(md)
mt := new(testutil.MockToken)
resp := response.New()
Expand Down
41 changes: 0 additions & 41 deletions src/app/webapi/component/handler.go

This file was deleted.

64 changes: 10 additions & 54 deletions src/app/webapi/component/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package component

import (
"app/webapi/pkg/router"
"database/sql"
"net/http"
"time"
Expand All @@ -10,20 +11,9 @@ import (

// IDatabase provides data query capabilities.
type IDatabase interface {
Select(dest interface{}, query string, args ...interface{}) error
Get(dest interface{}, query string, args ...interface{}) error
Exec(query string, args ...interface{}) (sql.Result, error)
QueryRowScan(dest interface{}, query string, args ...interface{}) error

ExistsString(err error, s string) (bool, string, error)
Error(err error) error
AffectedRows(result sql.Result) int

/*PaginatedResults(results interface{}, fn func() (results interface{}, total int, err error)) (total int, err error)
RecordExistsInt(fn func() (exists bool, ID int64, err error)) (exists bool, ID int64, err error)
RecordExistsString(fn func() (exists bool, ID string, err error)) (exists bool, ID string, err error)
AddRecordInt(fn func() (ID int64, err error)) (ID int64, err error)
AddRecordString(fn func() (ID string, err error)) (ID string, err error)*/
Get(dest interface{}, query string, args ...interface{}) error
Select(dest interface{}, query string, args ...interface{}) error
}

// IQuery provides default queries.
Expand All @@ -36,40 +26,6 @@ type IQuery interface {
DeleteAll(dest query.IRecord) (affected int, err error)
}

// IRecord provides table information.
type IRecord interface {
Table() string
PrimaryKey() string
}

/*
// IQuery provides data query capabilities.
type IQuery interface {
Select(dest interface{}, query string, args ...interface{}) error
Get(dest interface{}, query string, args ...interface{}) error
Exec(query string, args ...interface{}) (sql.Result, error)
ExistsString(err error, s string) (bool, string, error)
Error(err error) error
AffectedRows(result sql.Result) int
}*/

// IDatabase provides data query capabilities.
/*type IDatabase interface {
//LastInsertID(r sql.Result, err error) (int64, error)
//MySQLTimestamp(t time.Time) string
//GoTimestamp(s string) (time.Time, error)
//ExistsID(err error, ID int64) (bool, int64, error)
PaginatedResults(results interface{}, fn func() (results interface{}, total int, err error)) (total int, err error)
RecordExistsInt(fn func() (exists bool, ID int64, err error)) (exists bool, ID int64, err error)
RecordExistsString(fn func() (exists bool, ID string, err error)) (exists bool, ID string, err error)
AddRecordInt(fn func() (ID int64, err error)) (ID int64, err error)
AddRecordString(fn func() (ID string, err error)) (ID string, err error)
//ExecQuery(fn func() (err error)) (err error)
}*/

// ILogger provides logging capabilities.
type ILogger interface {
//ControllerError(r *http.Request, err error, a ...interface{})
Expand All @@ -79,13 +35,13 @@ type ILogger interface {

// IRouter provides routing capabilities.
type IRouter interface {
Delete(path string, fn http.Handler)
Get(path string, fn http.Handler)
Head(path string, fn http.Handler)
Options(path string, fn http.Handler)
Patch(path string, fn http.Handler)
Post(path string, fn http.Handler)
Put(path string, fn http.Handler)
Delete(path string, fn router.Handler)
Get(path string, fn router.Handler)
Head(path string, fn router.Handler)
Options(path string, fn router.Handler)
Patch(path string, fn router.Handler)
Post(path string, fn router.Handler)
Put(path string, fn router.Handler)
}

// IBind provides bind and validation for requests.
Expand Down
2 changes: 1 addition & 1 deletion src/app/webapi/component/root/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ type Endpoint struct {

// Routes will set up the endpoints.
func (p *Endpoint) Routes(router component.IRouter) {
router.Get("/v1", component.H(p.Index))
router.Get("/v1", p.Index)
}
12 changes: 6 additions & 6 deletions src/app/webapi/component/user/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ type Endpoint struct {

// Routes will set up the endpoints.
func (p *Endpoint) Routes(router component.IRouter) {
router.Post("/v1/user", component.H(p.Create))
router.Get("/v1/user/:user_id", component.H(p.Show))
router.Get("/v1/user", component.H(p.Index))
router.Put("/v1/user/:user_id", component.H(p.Update))
router.Delete("/v1/user/:user_id", component.H(p.Destroy))
router.Delete("/v1/user", component.H(p.DestroyAll))
router.Post("/v1/user", p.Create)
router.Get("/v1/user/:user_id", p.Show)
router.Get("/v1/user", p.Index)
router.Put("/v1/user/:user_id", p.Update)
router.Delete("/v1/user/:user_id", p.Destroy)
router.Delete("/v1/user", p.DestroyAll)
}
10 changes: 5 additions & 5 deletions src/app/webapi/component/user/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (

"app/webapi/component"
"app/webapi/component/user"
"app/webapi/internal/testdb"
"app/webapi/internal/testutil"
"app/webapi/pkg/router"
"app/webapi/store"

"github.com/stretchr/testify/assert"
)

func TestCreate(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -39,7 +39,7 @@ func TestCreate(t *testing.T) {
}

func TestCreateUserAlreadyExists(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -61,11 +61,11 @@ func TestCreateUserAlreadyExists(t *testing.T) {
mux.ServeHTTP(w, r)

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), `{"status":"Bad Request","message":"user already exists"}`)
assert.Contains(t, w.Body.String(), `user already exists`)
}

func TestCreateBadEmail(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand Down
8 changes: 4 additions & 4 deletions src/app/webapi/component/user/destroy_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

"app/webapi/component"
"app/webapi/component/user"
"app/webapi/internal/testdb"
"app/webapi/internal/testutil"
"app/webapi/pkg/router"
"app/webapi/store"

"github.com/stretchr/testify/assert"
)

func TestDestroyAll(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -35,7 +35,7 @@ func TestDestroyAll(t *testing.T) {
}

func TestDestroyAllNoUsers(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -47,5 +47,5 @@ func TestDestroyAllNoUsers(t *testing.T) {
mux.ServeHTTP(w, r)

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), `{"status":"Bad Request","message":"no users to delete"}`)
assert.Contains(t, w.Body.String(), `no users to delete`)
}
4 changes: 2 additions & 2 deletions src/app/webapi/component/user/destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (

"app/webapi/component"
"app/webapi/component/user"
"app/webapi/internal/testdb"
"app/webapi/internal/testutil"
"app/webapi/pkg/router"
"app/webapi/store"

"github.com/stretchr/testify/assert"
)

func TestDestroy(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand Down
6 changes: 3 additions & 3 deletions src/app/webapi/component/user/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

"app/webapi/component"
"app/webapi/component/user"
"app/webapi/internal/testdb"
"app/webapi/internal/testutil"
"app/webapi/pkg/router"
"app/webapi/store"

"github.com/stretchr/testify/assert"
)

func TestIndexEmpty(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -30,7 +30,7 @@ func TestIndexEmpty(t *testing.T) {
}

func TestIndexOne(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand Down
4 changes: 2 additions & 2 deletions src/app/webapi/component/user/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"app/webapi/component"
"app/webapi/component/user"
"app/webapi/internal/testdb"
"app/webapi/internal/testutil"
"app/webapi/pkg/router"

"github.com/stretchr/testify/assert"
Expand All @@ -21,7 +21,7 @@ func TestRequestValidation(t *testing.T) {
} {
arr := strings.Split(v, " ")

testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand Down
8 changes: 4 additions & 4 deletions src/app/webapi/component/user/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import (

"app/webapi/component"
"app/webapi/component/user"
"app/webapi/internal/testdb"
"app/webapi/internal/testutil"
"app/webapi/pkg/router"
"app/webapi/store"

"github.com/stretchr/testify/assert"
)

func TestShowOne(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -34,7 +34,7 @@ func TestShowOne(t *testing.T) {
}

func TestShowNotFound(t *testing.T) {
testdb.SetupTest(t)
testutil.LoadDatabase(t)
core, _ := component.NewCoreMock()

mux := router.New()
Expand All @@ -45,5 +45,5 @@ func TestShowNotFound(t *testing.T) {
mux.ServeHTTP(w, r)

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), `{"status":"Bad Request","message":"item not found"}`)
assert.Contains(t, w.Body.String(), `item not found`)
}
Loading

0 comments on commit fd30233

Please sign in to comment.