Skip to content

Commit

Permalink
Add new usecase package.
Browse files Browse the repository at this point in the history
  • Loading branch information
josephspurrier committed Mar 9, 2017
1 parent 84b0bfc commit 8d1b14e
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 186 deletions.
16 changes: 10 additions & 6 deletions controller/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/josephspurrier/gocleanarchitecture/database"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
"github.com/josephspurrier/gocleanarchitecture/usecase"
)

// TestLoginIndex ensures the index function returns a 200 code.
Expand Down Expand Up @@ -41,8 +42,9 @@ func TestLoginStoreMissingRequiredFields(t *testing.T) {

// Call the handler.
h := new(controller.LoginHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(new(database.MockService)),
}
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

Expand All @@ -67,8 +69,9 @@ func TestLoginStoreAuthenticateOK(t *testing.T) {

// Call the handler.
h := new(controller.LoginHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(new(database.MockService)),
}
h.ViewService = view.New("../view", "tmpl")

// Create a new user.
Expand Down Expand Up @@ -100,8 +103,9 @@ func TestLoginStoreAuthenticateFail(t *testing.T) {

// Call the handler.
h := new(controller.LoginHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(new(database.MockService)),
}
h.ViewService = view.New("../view", "tmpl")

// Create a new user.
Expand Down
16 changes: 10 additions & 6 deletions controller/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/josephspurrier/gocleanarchitecture/controller"
"github.com/josephspurrier/gocleanarchitecture/database"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
"github.com/josephspurrier/gocleanarchitecture/usecase"
)

// TestRegisterIndex ensures the index function returns a 200 code.
Expand Down Expand Up @@ -48,8 +49,9 @@ func TestRegisterStoreCreateOK(t *testing.T) {

// Call the handler.
h := new(controller.RegisterHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(new(database.MockService)),
}
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

Expand All @@ -73,8 +75,9 @@ func TestRegisterStoreCreateNoFieldFail(t *testing.T) {

// Call the handler.
h := new(controller.RegisterHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(new(database.MockService)),
}
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

Expand Down Expand Up @@ -102,8 +105,9 @@ func TestRegisterStoreCreateOneMissingFieldFail(t *testing.T) {

// Call the handler.
h := new(controller.RegisterHandler)
db := new(database.MockService)
h.UserService = database.NewUserService(db)
h.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(new(database.MockService)),
}
h.ViewService = view.New("../view", "tmpl")
h.Index(w, r)

Expand Down
50 changes: 50 additions & 0 deletions database/user_repo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package database

import "github.com/josephspurrier/gocleanarchitecture/domain"

// UserRepo represents a service for managing users in a database.
type UserRepo struct {
client Service
}

// NewUserRepo returns the service for managing users in a database.
func NewUserRepo(client Service) *UserRepo {
s := new(UserRepo)
s.client = client
return s
}

// Store adds a user to the database.
func (s *UserRepo) Store(item *domain.User) error {
// Load the data.
err := s.client.Read()
if err != nil {
return err
}

// Add the record.
s.client.AddRecord(*item)

// Save the record to the database.
return s.client.Write()
}

// FindByEmail returns a user by an email.
func (s *UserRepo) FindByEmail(email string) (*domain.User, error) {
item := new(domain.User)

// Load the data.
err := s.client.Read()
if err != nil {
return item, err
}

// Determine if the record exists.
for _, v := range s.client.Records() {
if v.Email == email {
return &v, nil
}
}

return item, domain.ErrUserNotFound
}
26 changes: 26 additions & 0 deletions database/user_repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package database_test

import (
"testing"

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

// TestUserRepo tests the user repo.
func TestUserRepo(t *testing.T) {
db := new(database.MockService)
s := database.NewUserRepo(db)

_, err := s.FindByEmail("bad@example.com")
AssertEqual(t, err, domain.ErrUserNotFound)

u := new(domain.User)
u.Email = "jdoe@example.com"
u.Password = "Pa$$w0rd"
err = s.Store(u)
AssertEqual(t, err, nil)

_, err = s.FindByEmail("jdoe@example.com")
AssertEqual(t, err, nil)
}
83 changes: 0 additions & 83 deletions database/user_service.go

This file was deleted.

88 changes: 0 additions & 88 deletions database/user_service_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ type User struct {
// UserService represents a service for managing users.
type UserService interface {
User(email string) (*User, error)
CreateUser(user *User) error
Authenticate(user *User) error
CreateUser(item *User) error
Authenticate(item *User) error
}
5 changes: 4 additions & 1 deletion lib/boot/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/josephspurrier/gocleanarchitecture/database"
"github.com/josephspurrier/gocleanarchitecture/domain"
"github.com/josephspurrier/gocleanarchitecture/lib/view"
"github.com/josephspurrier/gocleanarchitecture/usecase"
)

// Service represents all the services that the application uses.
Expand All @@ -21,7 +22,9 @@ func RegisterServices() *Service {
db := database.NewClient("db.json")

// Store all the services for the application.
s.UserService = database.NewUserService(db)
s.UserService = &usecase.UserService{
UserRepo: database.NewUserRepo(db),
}
s.ViewService = view.New("../../view", "tmpl")

return s
Expand Down

0 comments on commit 8d1b14e

Please sign in to comment.