fix: move some logic to domain entities
This commit is contained in:
@@ -29,7 +29,7 @@ type UseCase interface {
|
||||
GetBook(id entity.ID) (*entity.Book, error)
|
||||
SearchBooks(query string) ([]*entity.Book, error)
|
||||
ListBooks() ([]*entity.Book, error)
|
||||
CreateBook(e *entity.Book) (entity.ID, error)
|
||||
CreateBook(title string, author string, pages int, quantity int) (entity.ID, error)
|
||||
UpdateBook(e *entity.Book) error
|
||||
DeleteBook(id entity.ID) error
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
reflect "reflect"
|
||||
|
||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockReader is a mock of Reader interface
|
||||
@@ -324,18 +325,18 @@ func (mr *MockUseCaseMockRecorder) ListBooks() *gomock.Call {
|
||||
}
|
||||
|
||||
// CreateBook mocks base method
|
||||
func (m *MockUseCase) CreateBook(e *entity.Book) (entity.ID, error) {
|
||||
func (m *MockUseCase) CreateBook(title, author string, pages, quantity int) (entity.ID, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "CreateBook", e)
|
||||
ret := m.ctrl.Call(m, "CreateBook", title, author, pages, quantity)
|
||||
ret0, _ := ret[0].(entity.ID)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// CreateBook indicates an expected call of CreateBook
|
||||
func (mr *MockUseCaseMockRecorder) CreateBook(e interface{}) *gomock.Call {
|
||||
func (mr *MockUseCaseMockRecorder) CreateBook(title, author, pages, quantity interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateBook", reflect.TypeOf((*MockUseCase)(nil).CreateBook), e)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateBook", reflect.TypeOf((*MockUseCase)(nil).CreateBook), title, author, pages, quantity)
|
||||
}
|
||||
|
||||
// UpdateBook mocks base method
|
||||
|
||||
@@ -22,10 +22,16 @@ func NewService(r Repository) *Service {
|
||||
}
|
||||
|
||||
//CreateBook create a book
|
||||
func (s *Service) CreateBook(e *entity.Book) (entity.ID, error) {
|
||||
e.ID = entity.NewID()
|
||||
e.CreatedAt = time.Now()
|
||||
return s.repo.Create(e)
|
||||
func (s *Service) CreateBook(title string, author string, pages int, quantity int) (entity.ID, error) {
|
||||
b, err := entity.NewBook(title, author, pages, quantity)
|
||||
if err != nil {
|
||||
return b.ID, err
|
||||
}
|
||||
err = b.Validate()
|
||||
if err != nil {
|
||||
return b.ID, err
|
||||
}
|
||||
return s.repo.Create(b)
|
||||
}
|
||||
|
||||
//GetBook get a book
|
||||
@@ -76,5 +82,10 @@ func (s *Service) DeleteBook(id entity.ID) error {
|
||||
|
||||
//UpdateBook Update a book
|
||||
func (s *Service) UpdateBook(e *entity.Book) error {
|
||||
err := e.Validate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
e.UpdatedAt = time.Now()
|
||||
return s.repo.Update(e)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package book
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
||||
|
||||
@@ -12,25 +13,34 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func newFixtureBook() *entity.Book {
|
||||
return &entity.Book{
|
||||
Title: "I Am Ozzy",
|
||||
Author: "Ozzy Osbourne",
|
||||
Pages: 294,
|
||||
Quantity: 1,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Create(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u := entity.NewFixtureBook()
|
||||
id, err := m.CreateBook(u)
|
||||
u := newFixtureBook()
|
||||
_, err := m.CreateBook(u.Title, u.Author, u.Pages, u.Quantity)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, u.ID, id)
|
||||
assert.False(t, u.CreatedAt.IsZero())
|
||||
}
|
||||
|
||||
func Test_SearchAndFind(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u1 := entity.NewFixtureBook()
|
||||
u2 := entity.NewFixtureBook()
|
||||
u1 := newFixtureBook()
|
||||
u2 := newFixtureBook()
|
||||
u2.Title = "Lemmy: Biography"
|
||||
|
||||
uID, _ := m.CreateBook(u1)
|
||||
_, _ = m.CreateBook(u2)
|
||||
uID, _ := m.CreateBook(u1.Title, u1.Author, u1.Pages, u1.Quantity)
|
||||
_, _ = m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
|
||||
|
||||
t.Run("search", func(t *testing.T) {
|
||||
c, err := m.SearchBooks("ozzy")
|
||||
@@ -58,8 +68,8 @@ func Test_SearchAndFind(t *testing.T) {
|
||||
func Test_Update(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u := entity.NewFixtureBook()
|
||||
id, err := m.CreateBook(u)
|
||||
u := newFixtureBook()
|
||||
id, err := m.CreateBook(u.Title, u.Author, u.Pages, u.Quantity)
|
||||
assert.Nil(t, err)
|
||||
saved, _ := m.GetBook(id)
|
||||
saved.Title = "Lemmy: Biography"
|
||||
@@ -72,9 +82,9 @@ func Test_Update(t *testing.T) {
|
||||
func TestDelete(t *testing.T) {
|
||||
repo := book.NewInmemRepository()
|
||||
m := NewService(repo)
|
||||
u1 := entity.NewFixtureBook()
|
||||
u2 := entity.NewFixtureBook()
|
||||
u2ID, _ := m.CreateBook(u2)
|
||||
u1 := newFixtureBook()
|
||||
u2 := newFixtureBook()
|
||||
u2ID, _ := m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
|
||||
|
||||
err := m.DeleteBook(u1.ID)
|
||||
assert.Equal(t, domain.ErrNotFound, err)
|
||||
|
||||
Reference in New Issue
Block a user