-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84b0bfc
commit 8d1b14e
Showing
9 changed files
with
149 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.