Skip to content

Commit

Permalink
Consolidate controllers to one package
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Mar 8, 2017
1 parent f36591c commit 1797af9
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 64 deletions.
10 changes: 5 additions & 5 deletions controller/login/login.go → controller/login.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package login
package controller

import (
"fmt"
Expand All @@ -8,14 +8,14 @@ import (
"github.com/josephspurrier/gocleanarchitecture/lib/view"
)

// Handler represents the services required for this controller.
type Handler struct {
// LoginHandler represents the services required for this controller.
type LoginHandler struct {
UserService domain.UserService
ViewService view.Service
}

// Index displays the logon screen.
func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
func (h *LoginHandler) Index(w http.ResponseWriter, r *http.Request) {
// Handle 404.
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -32,7 +32,7 @@ func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
}

// Store handles the submission of the login information.
func (h *Handler) Store(w http.ResponseWriter, r *http.Request) {
func (h *LoginHandler) Store(w http.ResponseWriter, r *http.Request) {
// Don't continue if required fields are missing.
for _, v := range []string{"email", "password"} {
if len(r.FormValue(v)) == 0 {
Expand Down
42 changes: 17 additions & 25 deletions controller/login/login_test.go → controller/login_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
package login_test
package controller

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/josephspurrier/gocleanarchitecture/controller/login"
"github.com/josephspurrier/gocleanarchitecture/database"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
)

// AssertEqual throws an error if the two values are not equal.
func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) {
if actualValue != expectedValue {
t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue)
}
}

// TestIndex ensures the index function returns a 200 code.
func TestIndex(t *testing.T) {
// TestLoginIndex ensures the index function returns a 200 code.
func TestLoginIndex(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/", nil)
Expand All @@ -29,16 +21,16 @@ func TestIndex(t *testing.T) {
}

// Call the handler.
h := new(login.Handler)
h.ViewService = view.New("../../view", "tmpl")
h := new(LoginHandler)
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

// Check the output.
AssertEqual(t, w.Code, http.StatusOK)
}

// TestStoreMissingRequiredField ensures required fields should be entered.
func TestStoreMissingRequiredFields(t *testing.T) {
// TestLoginStoreMissingRequiredField ensures required fields should be entered.
func TestLoginStoreMissingRequiredFields(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -47,18 +39,18 @@ func TestStoreMissingRequiredFields(t *testing.T) {
}

// Call the handler.
h := new(login.Handler)
h := new(LoginHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.ViewService = view.New("../../view", "tmpl")
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

// Check the output.
AssertEqual(t, w.Code, http.StatusBadRequest)
}

// TestStoreAuthenticateOK ensures login can be successful.
func TestStoreAuthenticateOK(t *testing.T) {
// TestLoginStoreAuthenticateOK ensures login can be successful.
func TestLoginStoreAuthenticateOK(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -73,10 +65,10 @@ func TestStoreAuthenticateOK(t *testing.T) {
r.Form.Add("password", "Pa$$w0rd")

// Call the handler.
h := new(login.Handler)
h := new(LoginHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.ViewService = view.New("../../view", "tmpl")
h.ViewService = view.New("../view", "tmpl")

// Create a new user.
u := new(domain.User)
Expand All @@ -90,8 +82,8 @@ func TestStoreAuthenticateOK(t *testing.T) {
AssertEqual(t, w.Code, http.StatusOK)
}

// TestStoreAuthenticateFail ensures login can fail.
func TestStoreAuthenticateFail(t *testing.T) {
// TestLoginStoreAuthenticateFail ensures login can fail.
func TestLoginStoreAuthenticateFail(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -106,10 +98,10 @@ func TestStoreAuthenticateFail(t *testing.T) {
r.Form.Add("password", "BadPa$$w0rd")

// Call the handler.
h := new(login.Handler)
h := new(LoginHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.ViewService = view.New("../../view", "tmpl")
h.ViewService = view.New("../view", "tmpl")

// Create a new user.
u := new(domain.User)
Expand Down
10 changes: 5 additions & 5 deletions controller/register/register.go → controller/register.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package register
package controller

import (
"fmt"
Expand All @@ -8,14 +8,14 @@ import (
"github.com/josephspurrier/gocleanarchitecture/lib/view"
)

// Handler represents the services required for this controller.
type Handler struct {
// RegisterHandler represents the services required for this controller.
type RegisterHandler struct {
UserService domain.UserService
ViewService view.Service
}

// Index displays the register screen.
func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
func (h *RegisterHandler) Index(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
h.Store(w, r)
return
Expand All @@ -25,7 +25,7 @@ func (h *Handler) Index(w http.ResponseWriter, r *http.Request) {
}

// Store adds a user to the database.
func (h *Handler) Store(w http.ResponseWriter, r *http.Request) {
func (h *RegisterHandler) Store(w http.ResponseWriter, r *http.Request) {
// Don't continue if required fields are missing.
for _, v := range []string{"firstname", "lastname", "email", "password"} {
if len(r.FormValue(v)) == 0 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
package register_test
package controller

import (
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/josephspurrier/gocleanarchitecture/controller/register"
"github.com/josephspurrier/gocleanarchitecture/database"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
)

// AssertEqual throws an error if the two values are not equal.
func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) {
if actualValue != expectedValue {
t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue)
}
}

// TestIndex ensures the index function returns a 200 code.
func TestIndex(t *testing.T) {
// TesttRegisterIndex ensures the index function returns a 200 code.
func TestRegisterIndex(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "/", nil)
Expand All @@ -28,16 +20,16 @@ func TestIndex(t *testing.T) {
}

// Call the handler.
h := new(register.Handler)
h.ViewService = view.New("../../view", "tmpl")
h := new(RegisterHandler)
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

// Check the output.
AssertEqual(t, w.Code, http.StatusOK)
}

// TestStoreCreateOK ensures register can be successful.
func TestStoreCreateOK(t *testing.T) {
// TesttRegisterStoreCreateOK ensures register can be successful.
func TesttRegisterStoreCreateOK(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -54,10 +46,10 @@ func TestStoreCreateOK(t *testing.T) {
r.Form.Add("password", "Pa$$w0rd")

// Call the handler.
h := new(register.Handler)
h := new(RegisterHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.ViewService = view.New("../../view", "tmpl")
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

// Check the output.
Expand All @@ -69,8 +61,8 @@ func TestStoreCreateOK(t *testing.T) {
AssertEqual(t, w.Code, http.StatusInternalServerError)
}

// TestStoreCreateNoFieldFail ensures register can fail with no fields.
func TestStoreCreateNoFieldFail(t *testing.T) {
// TestRegisterStoreCreateNoFieldFail ensures register can fail with no fields.
func TesttRegisterStoreCreateNoFieldFail(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -79,19 +71,19 @@ func TestStoreCreateNoFieldFail(t *testing.T) {
}

// Call the handler.
h := new(register.Handler)
h := new(RegisterHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.ViewService = view.New("../../view", "tmpl")
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

// Check the output.
AssertEqual(t, w.Code, http.StatusBadRequest)
}

// TestStoreCreateOneMissingFieldFail ensures register can fail with one missing
// TesttRegisterStoreCreateOneMissingFieldFail ensures register can fail with one missing
// field.
func TestStoreCreateOneMissingFieldFail(t *testing.T) {
func TesttRegisterStoreCreateOneMissingFieldFail(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -108,10 +100,10 @@ func TestStoreCreateOneMissingFieldFail(t *testing.T) {
r.Form.Add("password", "Pa$$w0rd")

// Call the handler.
h := new(register.Handler)
h := new(RegisterHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.ViewService = view.New("../../view", "tmpl")
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

// Check the output.
Expand Down
10 changes: 10 additions & 0 deletions controller/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controller

import "testing"

// AssertEqual throws an error if the two values are not equal.
func AssertEqual(t *testing.T, actualValue interface{}, expectedValue interface{}) {
if actualValue != expectedValue {
t.Errorf("\n got: %v\nwant: %v", actualValue, expectedValue)
}
}
7 changes: 3 additions & 4 deletions lib/boot/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package boot
import (
"net/http"

"github.com/josephspurrier/gocleanarchitecture/controller/login"
"github.com/josephspurrier/gocleanarchitecture/controller/register"
"github.com/josephspurrier/gocleanarchitecture/controller"
)

// LoadRoutes returns a handler with all the routes.
Expand All @@ -23,7 +22,7 @@ func (s *Service) LoadRoutes() http.Handler {
// AddLogin registers the login handlers.
func (s *Service) AddLogin(mux *http.ServeMux) {
// Create handler.
h := new(login.Handler)
h := new(controller.LoginHandler)

// Assign services.
h.UserService = s.UserService
Expand All @@ -36,7 +35,7 @@ func (s *Service) AddLogin(mux *http.ServeMux) {
// AddRegister registers the register handlers.
func (s *Service) AddRegister(mux *http.ServeMux) {
// Create handler.
h := new(register.Handler)
h := new(controller.RegisterHandler)

// Assign services.
h.UserService = s.UserService
Expand Down

0 comments on commit 1797af9

Please sign in to comment.