feat: refactoring
This commit is contained in:
@@ -21,9 +21,3 @@ type Repository interface {
|
||||
Reader
|
||||
Writer
|
||||
}
|
||||
|
||||
//UseCase use case interface
|
||||
type UseCase interface {
|
||||
Reader
|
||||
Writer
|
||||
}
|
||||
@@ -4,27 +4,29 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/pkg/password"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
)
|
||||
|
||||
//Service service interface
|
||||
type Service struct {
|
||||
//repository service interface
|
||||
type repository struct {
|
||||
repo Repository
|
||||
pwd password.UseCase
|
||||
}
|
||||
|
||||
//NewService create new use case
|
||||
func NewService(r Repository, pwd password.UseCase) *Service {
|
||||
return &Service{
|
||||
//NewRepository create new repository
|
||||
func NewRepository(r Repository, pwd password.UseCase) *repository {
|
||||
return &repository{
|
||||
repo: r,
|
||||
pwd: pwd,
|
||||
}
|
||||
}
|
||||
|
||||
//Create an user
|
||||
func (s *Service) Create(e *User) (entity.ID, error) {
|
||||
func (s *repository) Create(e *User) (entity.ID, error) {
|
||||
e.ID = entity.NewID()
|
||||
e.CreatedAt = time.Now()
|
||||
pwd, err := s.pwd.Generate(e.Password)
|
||||
@@ -36,31 +38,34 @@ func (s *Service) Create(e *User) (entity.ID, error) {
|
||||
}
|
||||
|
||||
//Get an user
|
||||
func (s *Service) Get(id entity.ID) (*User, error) {
|
||||
func (s *repository) Get(id entity.ID) (*User, error) {
|
||||
return s.repo.Get(id)
|
||||
}
|
||||
|
||||
//Search users
|
||||
func (s *Service) Search(query string) ([]*User, error) {
|
||||
func (s *repository) Search(query string) ([]*User, error) {
|
||||
return s.repo.Search(strings.ToLower(query))
|
||||
}
|
||||
|
||||
//List users
|
||||
func (s *Service) List() ([]*User, error) {
|
||||
func (s *repository) List() ([]*User, error) {
|
||||
return s.repo.List()
|
||||
}
|
||||
|
||||
//Delete an user
|
||||
func (s *Service) Delete(id entity.ID) error {
|
||||
_, err := s.Get(id)
|
||||
func (s *repository) Delete(id entity.ID) error {
|
||||
u, err := s.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(u.Books) > 0 {
|
||||
return domain.ErrCannotBeDeleted
|
||||
}
|
||||
return s.repo.Delete(id)
|
||||
}
|
||||
|
||||
//Update an user
|
||||
func (s *Service) Update(e *User) error {
|
||||
func (s *repository) Update(e *User) error {
|
||||
e.UpdatedAt = time.Now()
|
||||
return s.repo.Update(e)
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
func Test_Create(t *testing.T) {
|
||||
repo := NewInmemRepository()
|
||||
service := NewService(repo, password.NewFakeService())
|
||||
service := NewRepository(repo, password.NewFakeService())
|
||||
u := NewFixtureUser()
|
||||
id, err := service.Create(u)
|
||||
assert.Nil(t, err)
|
||||
@@ -24,7 +24,7 @@ func Test_Create(t *testing.T) {
|
||||
|
||||
func Test_SearchAndFind(t *testing.T) {
|
||||
repo := NewInmemRepository()
|
||||
service := NewService(repo, password.NewFakeService())
|
||||
service := NewRepository(repo, password.NewFakeService())
|
||||
u1 := NewFixtureUser()
|
||||
u2 := NewFixtureUser()
|
||||
u2.FirstName = "Lemmy"
|
||||
@@ -57,7 +57,7 @@ func Test_SearchAndFind(t *testing.T) {
|
||||
|
||||
func Test_Update(t *testing.T) {
|
||||
repo := NewInmemRepository()
|
||||
service := NewService(repo, password.NewFakeService())
|
||||
service := NewRepository(repo, password.NewFakeService())
|
||||
u := NewFixtureUser()
|
||||
id, err := service.Create(u)
|
||||
assert.Nil(t, err)
|
||||
@@ -74,7 +74,7 @@ func Test_Update(t *testing.T) {
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
repo := NewInmemRepository()
|
||||
service := NewService(repo, password.NewFakeService())
|
||||
service := NewRepository(repo, password.NewFakeService())
|
||||
u1 := NewFixtureUser()
|
||||
u2 := NewFixtureUser()
|
||||
u2ID, _ := service.Create(u2)
|
||||
Reference in New Issue
Block a user