Skip to content

Commit

Permalink
Merge pull request #3 from josephspurrier/same-domain-folder
Browse files Browse the repository at this point in the history
Simplify structure
  • Loading branch information
josephspurrier committed Mar 8, 2017
2 parents 08773d9 + 676174a commit 84b0bfc
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 141 deletions.
16 changes: 8 additions & 8 deletions controller/login/login.go → controller/login.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package login
package controller

import (
"fmt"
"net/http"

"github.com/josephspurrier/gocleanarchitecture/domain/user"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
)

// Handler represents the services required for this controller.
type Handler struct {
UserService user.Service
// 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 All @@ -43,7 +43,7 @@ func (h *Handler) Store(w http.ResponseWriter, r *http.Request) {
}
}

u := new(user.Item)
u := new(domain.User)
u.Email = r.FormValue("email")
u.Password = r.FormValue("password")

Expand Down
49 changes: 21 additions & 28 deletions controller/login/login_test.go → controller/login_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
package login_test
package controller_test

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

"github.com/josephspurrier/gocleanarchitecture/controller/login"
"github.com/josephspurrier/gocleanarchitecture/controller"
"github.com/josephspurrier/gocleanarchitecture/database"
"github.com/josephspurrier/gocleanarchitecture/domain/user"
"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 +22,16 @@ func TestIndex(t *testing.T) {
}

// Call the handler.
h := new(login.Handler)
h.ViewService = view.New("../../view", "tmpl")
h := new(controller.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 +40,18 @@ func TestStoreMissingRequiredFields(t *testing.T) {
}

// Call the handler.
h := new(login.Handler)
h := new(controller.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,13 +66,13 @@ func TestStoreAuthenticateOK(t *testing.T) {
r.Form.Add("password", "Pa$$w0rd")

// Call the handler.
h := new(login.Handler)
h := new(controller.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(user.Item)
u := new(domain.User)
u.Email = "jdoe@example.com"
u.Password = "Pa$$w0rd"
h.UserService.CreateUser(u)
Expand All @@ -90,8 +83,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,13 +99,13 @@ func TestStoreAuthenticateFail(t *testing.T) {
r.Form.Add("password", "BadPa$$w0rd")

// Call the handler.
h := new(login.Handler)
h := new(controller.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(user.Item)
u := new(domain.User)
u.Email = "jdoe2@example.com"
u.Password = "Pa$$w0rd"
h.UserService.CreateUser(u)
Expand Down
16 changes: 8 additions & 8 deletions controller/register/register.go → controller/register.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package register
package controller

import (
"fmt"
"net/http"

"github.com/josephspurrier/gocleanarchitecture/domain/user"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
)

// Handler represents the services required for this controller.
type Handler struct {
UserService user.Service
// 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 All @@ -37,7 +37,7 @@ func (h *Handler) Store(w http.ResponseWriter, r *http.Request) {
}

// Build the user from the form values.
u := new(user.Item)
u := new(domain.User)
u.FirstName = r.FormValue("firstname")
u.LastName = r.FormValue("lastname")
u.Email = r.FormValue("email")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package register_test
package controller_test

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

"github.com/josephspurrier/gocleanarchitecture/controller/register"
"github.com/josephspurrier/gocleanarchitecture/controller"
"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) {
// TestRegisterIndex 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 +21,16 @@ func TestIndex(t *testing.T) {
}

// Call the handler.
h := new(register.Handler)
h.ViewService = view.New("../../view", "tmpl")
h := new(controller.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) {
// TestRegisterStoreCreateOK ensures register can be successful.
func TestRegisterStoreCreateOK(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -54,10 +47,10 @@ func TestStoreCreateOK(t *testing.T) {
r.Form.Add("password", "Pa$$w0rd")

// Call the handler.
h := new(register.Handler)
h := new(controller.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 +62,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 TestRegisterStoreCreateNoFieldFail(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -79,19 +72,19 @@ func TestStoreCreateNoFieldFail(t *testing.T) {
}

// Call the handler.
h := new(register.Handler)
h := new(controller.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
// TestRegisterStoreCreateOneMissingFieldFail ensures register can fail with one missing
// field.
func TestStoreCreateOneMissingFieldFail(t *testing.T) {
func TestRegisterStoreCreateOneMissingFieldFail(t *testing.T) {
// Set up the request.
w := httptest.NewRecorder()
r, err := http.NewRequest("POST", "/", nil)
Expand All @@ -108,10 +101,10 @@ func TestStoreCreateOneMissingFieldFail(t *testing.T) {
r.Form.Add("password", "Pa$$w0rd")

// Call the handler.
h := new(register.Handler)
h := new(controller.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_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package controller_test

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)
}
}
12 changes: 6 additions & 6 deletions database/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"os"
"sync"

"github.com/josephspurrier/gocleanarchitecture/domain/user"
"github.com/josephspurrier/gocleanarchitecture/domain"
)

// Schema represents the database structure.
type Schema struct {
Records []user.Item
Records []domain.User
}

// Client represents a client to the data store.
Expand All @@ -27,8 +27,8 @@ type Client struct {
type Service interface {
Read() error
Write() error
Records() []user.Item
AddRecord(user.Item)
Records() []domain.User
AddRecord(domain.User)
}

// NewClient returns a new database client.
Expand Down Expand Up @@ -95,11 +95,11 @@ func (c *Client) Write() error {
}

// AddRecord adds a record to the database.
func (c *Client) AddRecord(rec user.Item) {
func (c *Client) AddRecord(rec domain.User) {
c.data.Records = append(c.data.Records, rec)
}

// Records retrieves all records from the database.
func (c *Client) Records() []user.Item {
func (c *Client) Records() []domain.User {
return c.data.Records
}
Loading

0 comments on commit 84b0bfc

Please sign in to comment.