feat: move entity and usecase to root folder
This commit is contained in:
@@ -32,9 +32,9 @@ ci: dependencies test
|
|||||||
build-mocks:
|
build-mocks:
|
||||||
@go get github.com/golang/mock/gomock
|
@go get github.com/golang/mock/gomock
|
||||||
@go install github.com/golang/mock/mockgen
|
@go install github.com/golang/mock/mockgen
|
||||||
@~/go/bin/mockgen -source=domain/usecase/book/interface.go -destination=domain/usecase/book/mock/book.go -package=mock
|
@~/go/bin/mockgen -source=usecase/book/interface.go -destination=usecase/book/mock/book.go -package=mock
|
||||||
@~/go/bin/mockgen -source=domain/usecase/user/interface.go -destination=domain/usecase/user/mock/user.go -package=mock
|
@~/go/bin/mockgen -source=usecase/user/interface.go -destination=usecase/user/mock/user.go -package=mock
|
||||||
@~/go/bin/mockgen -source=domain/usecase/loan/interface.go -destination=domain/usecase/loan/mock/loan.go -package=mock
|
@~/go/bin/mockgen -source=usecase/loan/interface.go -destination=usecase/loan/mock/loan.go -package=mock
|
||||||
|
|
||||||
test:
|
test:
|
||||||
go test -tags testing ./...
|
go test -tags testing ./...
|
||||||
|
|||||||
+4
-6
@@ -5,14 +5,12 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
|
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,7 +27,7 @@ func listBooks(service book.UseCase) http.Handler {
|
|||||||
data, err = service.SearchBooks(title)
|
data, err = service.SearchBooks(title)
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
@@ -108,7 +106,7 @@ func getBook(service book.UseCase) http.Handler {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
data, err := service.GetBook(id)
|
data, err := service.GetBook(id)
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -8,11 +8,10 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book/mock"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/book/mock"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -49,7 +48,7 @@ func Test_listBooks_NotFound(t *testing.T) {
|
|||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
service.EXPECT().
|
service.EXPECT().
|
||||||
SearchBooks("book of books").
|
SearchBooks("book of books").
|
||||||
Return(nil, domain.ErrNotFound)
|
Return(nil, entity.ErrNotFound)
|
||||||
res, err := http.Get(ts.URL + "?title=book+of+books")
|
res, err := http.Get(ts.URL + "?title=book+of+books")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, http.StatusNotFound, res.StatusCode)
|
assert.Equal(t, http.StatusNotFound, res.StatusCode)
|
||||||
|
|||||||
+8
-10
@@ -4,15 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/loan"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/loan"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,7 +25,7 @@ func borrowBook(bookService book.UseCase, userService user.UseCase, loanService
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
b, err := bookService.GetBook(bID)
|
b, err := bookService.GetBook(bID)
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
@@ -44,7 +42,7 @@ func borrowBook(bookService book.UseCase, userService user.UseCase, loanService
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
u, err := userService.GetUser(uID)
|
u, err := userService.GetUser(uID)
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
@@ -76,7 +74,7 @@ func returnBook(bookService book.UseCase, loanService loan.UseCase) http.Handler
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
b, err := bookService.GetBook(bID)
|
b, err := bookService.GetBook(bID)
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
@@ -87,7 +85,7 @@ func returnBook(bookService book.UseCase, loanService loan.UseCase) http.Handler
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = loanService.Return(b)
|
err = loanService.Return(b)
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -6,13 +6,12 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
bmock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/book/mock"
|
bmock "github.com/eminetto/clean-architecture-go-v2/usecase/book/mock"
|
||||||
lmock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/loan/mock"
|
lmock "github.com/eminetto/clean-architecture-go-v2/usecase/loan/mock"
|
||||||
umock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/user/mock"
|
umock "github.com/eminetto/clean-architecture-go-v2/usecase/user/mock"
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -35,7 +34,7 @@ func Test_borrowBook(t *testing.T) {
|
|||||||
t.Run("book not found", func(t *testing.T) {
|
t.Run("book not found", func(t *testing.T) {
|
||||||
bID := entity.NewID()
|
bID := entity.NewID()
|
||||||
uID := entity.NewID()
|
uID := entity.NewID()
|
||||||
bMock.EXPECT().GetBook(bID).Return(nil, domain.ErrNotFound)
|
bMock.EXPECT().GetBook(bID).Return(nil, entity.ErrNotFound)
|
||||||
ts := httptest.NewServer(r)
|
ts := httptest.NewServer(r)
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
res, err := http.Get(fmt.Sprintf("%s/v1/loan/borrow/%s/%s", ts.URL, bID.String(), uID.String()))
|
res, err := http.Get(fmt.Sprintf("%s/v1/loan/borrow/%s/%s", ts.URL, bID.String(), uID.String()))
|
||||||
@@ -48,7 +47,7 @@ func Test_borrowBook(t *testing.T) {
|
|||||||
}
|
}
|
||||||
uID := entity.NewID()
|
uID := entity.NewID()
|
||||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||||
uMock.EXPECT().GetUser(uID).Return(nil, domain.ErrNotFound)
|
uMock.EXPECT().GetUser(uID).Return(nil, entity.ErrNotFound)
|
||||||
ts := httptest.NewServer(r)
|
ts := httptest.NewServer(r)
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
res, err := http.Get(fmt.Sprintf("%s/v1/loan/borrow/%s/%s", ts.URL, b.ID.String(), uID.String()))
|
res, err := http.Get(fmt.Sprintf("%s/v1/loan/borrow/%s/%s", ts.URL, b.ID.String(), uID.String()))
|
||||||
@@ -89,7 +88,7 @@ func Test_returnBook(t *testing.T) {
|
|||||||
r.Handle("/v1/loan/return/{book_id}", handler)
|
r.Handle("/v1/loan/return/{book_id}", handler)
|
||||||
t.Run("book not found", func(t *testing.T) {
|
t.Run("book not found", func(t *testing.T) {
|
||||||
bID := entity.NewID()
|
bID := entity.NewID()
|
||||||
bMock.EXPECT().GetBook(bID).Return(nil, domain.ErrNotFound)
|
bMock.EXPECT().GetBook(bID).Return(nil, entity.ErrNotFound)
|
||||||
ts := httptest.NewServer(r)
|
ts := httptest.NewServer(r)
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
res, err := http.Get(fmt.Sprintf("%s/v1/loan/return/%s", ts.URL, bID.String()))
|
res, err := http.Get(fmt.Sprintf("%s/v1/loan/return/%s", ts.URL, bID.String()))
|
||||||
|
|||||||
+4
-6
@@ -5,13 +5,11 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
|
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@@ -30,7 +28,7 @@ func listUsers(service user.UseCase) http.Handler {
|
|||||||
data, err = service.SearchUsers(name)
|
data, err = service.SearchUsers(name)
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
@@ -108,7 +106,7 @@ func getUser(service user.UseCase) http.Handler {
|
|||||||
}
|
}
|
||||||
data, err := service.GetUser(id)
|
data, err := service.GetUser(id)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if err != nil && err != domain.ErrNotFound {
|
if err != nil && err != entity.ErrNotFound {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(errorMessage))
|
w.Write([]byte(errorMessage))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -10,9 +10,8 @@ import (
|
|||||||
|
|
||||||
"github.com/codegangsta/negroni"
|
"github.com/codegangsta/negroni"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
|
"github.com/eminetto/clean-architecture-go-v2/api/presenter"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/user/mock"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user/mock"
|
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -49,7 +48,7 @@ func Test_listUsers_NotFound(t *testing.T) {
|
|||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
m.EXPECT().
|
m.EXPECT().
|
||||||
SearchUsers("dio").
|
SearchUsers("dio").
|
||||||
Return(nil, domain.ErrNotFound)
|
Return(nil, entity.ErrNotFound)
|
||||||
res, err := http.Get(ts.URL + "?name=dio")
|
res, err := http.Get(ts.URL + "?name=dio")
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, http.StatusNotFound, res.StatusCode)
|
assert.Equal(t, http.StatusNotFound, res.StatusCode)
|
||||||
|
|||||||
+3
-3
@@ -9,11 +9,11 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
||||||
|
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
|
||||||
|
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/loan"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/loan"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package presenter
|
package presenter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Book data
|
//Book data
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package presenter
|
package presenter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//User data
|
//User data
|
||||||
|
|||||||
+1
-1
@@ -7,8 +7,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
||||||
|
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/config"
|
"github.com/eminetto/clean-architecture-go-v2/config"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
|||||||
@@ -2,8 +2,6 @@ package entity
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//Book data
|
//Book data
|
||||||
@@ -29,7 +27,7 @@ func NewBook(title string, author string, pages int, quantity int) (*Book, error
|
|||||||
}
|
}
|
||||||
err := b.Validate()
|
err := b.Validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, domain.ErrInvalidEntity
|
return nil, ErrInvalidEntity
|
||||||
}
|
}
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
@@ -37,7 +35,7 @@ func NewBook(title string, author string, pages int, quantity int) (*Book, error
|
|||||||
//Validate validate book
|
//Validate validate book
|
||||||
func (b *Book) Validate() error {
|
func (b *Book) Validate() error {
|
||||||
if b.Title == "" || b.Author == "" || b.Pages <= 0 || b.Quantity <= 0 {
|
if b.Title == "" || b.Author == "" || b.Pages <= 0 || b.Quantity <= 0 {
|
||||||
return domain.ErrInvalidEntity
|
return ErrInvalidEntity
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,7 @@ package entity_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,28 +36,28 @@ func TestBookValidate(t *testing.T) {
|
|||||||
author: "Neil Gaiman",
|
author: "Neil Gaiman",
|
||||||
pages: 100,
|
pages: 100,
|
||||||
quantity: 0,
|
quantity: 0,
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "",
|
title: "",
|
||||||
author: "Neil Gaiman",
|
author: "Neil Gaiman",
|
||||||
pages: 100,
|
pages: 100,
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "American Gods",
|
title: "American Gods",
|
||||||
author: "",
|
author: "",
|
||||||
pages: 100,
|
pages: 100,
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "American Gods",
|
title: "American Gods",
|
||||||
author: "Neil Gaiman",
|
author: "Neil Gaiman",
|
||||||
pages: 0,
|
pages: 0,
|
||||||
quantity: 1,
|
quantity: 1,
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package domain
|
package entity
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
@@ -3,7 +3,6 @@ package entity
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,7 +34,7 @@ func NewUser(email, password, firstName, lastName string) (*User, error) {
|
|||||||
u.Password = pwd
|
u.Password = pwd
|
||||||
err = u.Validate()
|
err = u.Validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, domain.ErrInvalidEntity
|
return nil, ErrInvalidEntity
|
||||||
}
|
}
|
||||||
return u, nil
|
return u, nil
|
||||||
}
|
}
|
||||||
@@ -44,7 +43,7 @@ func NewUser(email, password, firstName, lastName string) (*User, error) {
|
|||||||
func (u *User) AddBook(id ID) error {
|
func (u *User) AddBook(id ID) error {
|
||||||
_, err := u.GetBook(id)
|
_, err := u.GetBook(id)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return domain.ErrBookAlreadyBorrowed
|
return ErrBookAlreadyBorrowed
|
||||||
}
|
}
|
||||||
u.Books = append(u.Books, id)
|
u.Books = append(u.Books, id)
|
||||||
return nil
|
return nil
|
||||||
@@ -58,7 +57,7 @@ func (u *User) RemoveBook(id ID) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return domain.ErrNotFound
|
return ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
//GetBook get a book
|
//GetBook get a book
|
||||||
@@ -68,13 +67,13 @@ func (u *User) GetBook(id ID) (ID, error) {
|
|||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return id, domain.ErrNotFound
|
return id, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
//Validate validate data
|
//Validate validate data
|
||||||
func (u *User) Validate() error {
|
func (u *User) Validate() error {
|
||||||
if u.Email == "" || u.FirstName == "" || u.LastName == "" || u.Password == "" {
|
if u.Email == "" || u.FirstName == "" || u.LastName == "" || u.Password == "" {
|
||||||
return domain.ErrInvalidEntity
|
return ErrInvalidEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -3,8 +3,7 @@ package entity_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -32,14 +31,14 @@ func TestAddBook(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, 1, len(u.Books))
|
assert.Equal(t, 1, len(u.Books))
|
||||||
err = u.AddBook(bID)
|
err = u.AddBook(bID)
|
||||||
assert.Equal(t, domain.ErrBookAlreadyBorrowed, err)
|
assert.Equal(t, entity.ErrBookAlreadyBorrowed, err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRemoveBook(t *testing.T) {
|
func TestRemoveBook(t *testing.T) {
|
||||||
u, _ := entity.NewUser("sjobs@apple.com", "new_password", "Steve", "Jobs")
|
u, _ := entity.NewUser("sjobs@apple.com", "new_password", "Steve", "Jobs")
|
||||||
err := u.RemoveBook(entity.NewID())
|
err := u.RemoveBook(entity.NewID())
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
bID := entity.NewID()
|
bID := entity.NewID()
|
||||||
_ = u.AddBook(bID)
|
_ = u.AddBook(bID)
|
||||||
err = u.RemoveBook(bID)
|
err = u.RemoveBook(bID)
|
||||||
@@ -54,7 +53,7 @@ func TestGetBook(t *testing.T) {
|
|||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
assert.Equal(t, id, bID)
|
assert.Equal(t, id, bID)
|
||||||
_, err = u.GetBook(entity.NewID())
|
_, err = u.GetBook(entity.NewID())
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUserValidate(t *testing.T) {
|
func TestUserValidate(t *testing.T) {
|
||||||
@@ -79,7 +78,7 @@ func TestUserValidate(t *testing.T) {
|
|||||||
password: "new_password",
|
password: "new_password",
|
||||||
firstName: "Steve",
|
firstName: "Steve",
|
||||||
lastName: "Jobs",
|
lastName: "Jobs",
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
email: "sjobs@apple.com",
|
email: "sjobs@apple.com",
|
||||||
@@ -93,14 +92,14 @@ func TestUserValidate(t *testing.T) {
|
|||||||
password: "new_password",
|
password: "new_password",
|
||||||
firstName: "",
|
firstName: "",
|
||||||
lastName: "Jobs",
|
lastName: "Jobs",
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
email: "sjobs@apple.com",
|
email: "sjobs@apple.com",
|
||||||
password: "new_password",
|
password: "new_password",
|
||||||
firstName: "Steve",
|
firstName: "Steve",
|
||||||
lastName: "",
|
lastName: "",
|
||||||
want: domain.ErrInvalidEntity,
|
want: entity.ErrInvalidEntity,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
@@ -3,8 +3,7 @@ package repository
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//BookInmem in memory repo
|
//BookInmem in memory repo
|
||||||
@@ -29,8 +28,7 @@ func (r *BookInmem) Create(e *entity.Book) (entity.ID, error) {
|
|||||||
//Get a book
|
//Get a book
|
||||||
func (r *BookInmem) Get(id entity.ID) (*entity.Book, error) {
|
func (r *BookInmem) Get(id entity.ID) (*entity.Book, error) {
|
||||||
if r.m[id] == nil {
|
if r.m[id] == nil {
|
||||||
// return nil, fmt.Errorf("not found")
|
return nil, entity.ErrNotFound
|
||||||
return nil, domain.ErrNotFound
|
|
||||||
}
|
}
|
||||||
return r.m[id], nil
|
return r.m[id], nil
|
||||||
}
|
}
|
||||||
@@ -68,8 +66,7 @@ func (r *BookInmem) List() ([]*entity.Book, error) {
|
|||||||
//Delete a book
|
//Delete a book
|
||||||
func (r *BookInmem) Delete(id entity.ID) error {
|
func (r *BookInmem) Delete(id entity.ID) error {
|
||||||
if r.m[id] == nil {
|
if r.m[id] == nil {
|
||||||
// return fmt.Errorf("not found")
|
return entity.ErrNotFound
|
||||||
return domain.ErrNotFound
|
|
||||||
}
|
}
|
||||||
r.m[id] = nil
|
r.m[id] = nil
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//BookMySQL mysql repo
|
//BookMySQL mysql repo
|
||||||
|
|||||||
@@ -4,8 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//UserInmem in memory repo
|
//UserInmem in memory repo
|
||||||
@@ -30,8 +29,7 @@ func (r *UserInmem) Create(e *entity.User) (entity.ID, error) {
|
|||||||
//Get an user
|
//Get an user
|
||||||
func (r *UserInmem) Get(id entity.ID) (*entity.User, error) {
|
func (r *UserInmem) Get(id entity.ID) (*entity.User, error) {
|
||||||
if r.m[id] == nil {
|
if r.m[id] == nil {
|
||||||
// return nil, fmt.Errorf("not found")
|
return nil, entity.ErrNotFound
|
||||||
return nil, domain.ErrNotFound
|
|
||||||
}
|
}
|
||||||
return r.m[id], nil
|
return r.m[id], nil
|
||||||
}
|
}
|
||||||
@@ -55,8 +53,7 @@ func (r *UserInmem) Search(query string) ([]*entity.User, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(d) == 0 {
|
if len(d) == 0 {
|
||||||
// return nil, fmt.Errorf("not found")
|
return nil, entity.ErrNotFound
|
||||||
return nil, domain.ErrNotFound
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return d, nil
|
return d, nil
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//UserMySQL mysql repo
|
//UserMySQL mysql repo
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package book
|
package book
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Reader interface
|
//Reader interface
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
// Source: domain/usecase/book/interface.go
|
// Source: usecase/book/interface.go
|
||||||
|
|
||||||
// Package mock is a generated GoMock package.
|
// Package mock is a generated GoMock package.
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
entity "github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
)
|
)
|
||||||
@@ -4,9 +4,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//Service book usecase
|
//Service book usecase
|
||||||
@@ -34,7 +32,7 @@ func (s *Service) CreateBook(title string, author string, pages int, quantity in
|
|||||||
func (s *Service) GetBook(id entity.ID) (*entity.Book, error) {
|
func (s *Service) GetBook(id entity.ID) (*entity.Book, error) {
|
||||||
b, err := s.repo.Get(id)
|
b, err := s.repo.Get(id)
|
||||||
if b == nil {
|
if b == nil {
|
||||||
return nil, domain.ErrNotFound
|
return nil, entity.ErrNotFound
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -50,7 +48,7 @@ func (s *Service) SearchBooks(query string) ([]*entity.Book, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(books) == 0 {
|
if len(books) == 0 {
|
||||||
return nil, domain.ErrNotFound
|
return nil, entity.ErrNotFound
|
||||||
}
|
}
|
||||||
return books, nil
|
return books, nil
|
||||||
}
|
}
|
||||||
@@ -62,7 +60,7 @@ func (s *Service) ListBooks() ([]*entity.Book, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(books) == 0 {
|
if len(books) == 0 {
|
||||||
return nil, domain.ErrNotFound
|
return nil, entity.ErrNotFound
|
||||||
}
|
}
|
||||||
return books, nil
|
return books, nil
|
||||||
}
|
}
|
||||||
@@ -4,11 +4,9 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,7 +46,7 @@ func Test_SearchAndFind(t *testing.T) {
|
|||||||
assert.Equal(t, "I Am Ozzy", c[0].Title)
|
assert.Equal(t, "I Am Ozzy", c[0].Title)
|
||||||
|
|
||||||
c, err = m.SearchBooks("dio")
|
c, err = m.SearchBooks("dio")
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
assert.Nil(t, c)
|
assert.Nil(t, c)
|
||||||
})
|
})
|
||||||
t.Run("list all", func(t *testing.T) {
|
t.Run("list all", func(t *testing.T) {
|
||||||
@@ -86,10 +84,10 @@ func TestDelete(t *testing.T) {
|
|||||||
u2ID, _ := m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
|
u2ID, _ := m.CreateBook(u2.Title, u2.Author, u2.Pages, u2.Quantity)
|
||||||
|
|
||||||
err := m.DeleteBook(u1.ID)
|
err := m.DeleteBook(u1.ID)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
|
|
||||||
err = m.DeleteBook(u2ID)
|
err = m.DeleteBook(u2ID)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
_, err = m.GetBook(u2ID)
|
_, err = m.GetBook(u2ID)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package loan
|
package loan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//UseCase use case interface
|
//UseCase use case interface
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
// Source: domain/usecase/loan/interface.go
|
// Source: usecase/loan/interface.go
|
||||||
|
|
||||||
// Package mock is a generated GoMock package.
|
// Package mock is a generated GoMock package.
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
entity "github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
)
|
)
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
package loan
|
package loan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/book"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/book"
|
"github.com/eminetto/clean-architecture-go-v2/usecase/user"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/usecase/user"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//Service loan usecase
|
//Service loan usecase
|
||||||
@@ -32,7 +31,7 @@ func (s *Service) Borrow(u *entity.User, b *entity.Book) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if b.Quantity <= 0 {
|
if b.Quantity <= 0 {
|
||||||
return domain.ErrNotEnoughBooks
|
return entity.ErrNotEnoughBooks
|
||||||
}
|
}
|
||||||
|
|
||||||
err = u.AddBook(b.ID)
|
err = u.AddBook(b.ID)
|
||||||
@@ -74,7 +73,7 @@ func (s *Service) Return(b *entity.Book) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if !borrowed {
|
if !borrowed {
|
||||||
return domain.ErrBookNotBorrowed
|
return entity.ErrBookNotBorrowed
|
||||||
}
|
}
|
||||||
u, err := s.userService.GetUser(borrowedBy)
|
u, err := s.userService.GetUser(borrowedBy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -3,11 +3,10 @@ package loan
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
bmock "github.com/eminetto/clean-architecture-go-v2/usecase/book/mock"
|
||||||
bmock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/book/mock"
|
umock "github.com/eminetto/clean-architecture-go-v2/usecase/user/mock"
|
||||||
umock "github.com/eminetto/clean-architecture-go-v2/domain/usecase/user/mock"
|
|
||||||
"github.com/golang/mock/gomock"
|
"github.com/golang/mock/gomock"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -25,9 +24,9 @@ func Test_Borrow(t *testing.T) {
|
|||||||
b := &entity.Book{
|
b := &entity.Book{
|
||||||
ID: entity.NewID(),
|
ID: entity.NewID(),
|
||||||
}
|
}
|
||||||
uMock.EXPECT().GetUser(u.ID).Return(nil, domain.ErrNotFound)
|
uMock.EXPECT().GetUser(u.ID).Return(nil, entity.ErrNotFound)
|
||||||
err := uc.Borrow(u, b)
|
err := uc.Borrow(u, b)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
})
|
})
|
||||||
t.Run("book not found", func(t *testing.T) {
|
t.Run("book not found", func(t *testing.T) {
|
||||||
u := &entity.User{
|
u := &entity.User{
|
||||||
@@ -37,9 +36,9 @@ func Test_Borrow(t *testing.T) {
|
|||||||
ID: entity.NewID(),
|
ID: entity.NewID(),
|
||||||
}
|
}
|
||||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||||
bMock.EXPECT().GetBook(b.ID).Return(nil, domain.ErrNotFound)
|
bMock.EXPECT().GetBook(b.ID).Return(nil, entity.ErrNotFound)
|
||||||
err := uc.Borrow(u, b)
|
err := uc.Borrow(u, b)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
})
|
})
|
||||||
t.Run("not enough books to borrow", func(t *testing.T) {
|
t.Run("not enough books to borrow", func(t *testing.T) {
|
||||||
u := &entity.User{
|
u := &entity.User{
|
||||||
@@ -52,7 +51,7 @@ func Test_Borrow(t *testing.T) {
|
|||||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||||
err := uc.Borrow(u, b)
|
err := uc.Borrow(u, b)
|
||||||
assert.Equal(t, domain.ErrNotEnoughBooks, err)
|
assert.Equal(t, entity.ErrNotEnoughBooks, err)
|
||||||
})
|
})
|
||||||
t.Run("book already borrowed", func(t *testing.T) {
|
t.Run("book already borrowed", func(t *testing.T) {
|
||||||
u := &entity.User{
|
u := &entity.User{
|
||||||
@@ -66,7 +65,7 @@ func Test_Borrow(t *testing.T) {
|
|||||||
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
uMock.EXPECT().GetUser(u.ID).Return(u, nil)
|
||||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||||
err := uc.Borrow(u, b)
|
err := uc.Borrow(u, b)
|
||||||
assert.Equal(t, domain.ErrBookAlreadyBorrowed, err)
|
assert.Equal(t, entity.ErrBookAlreadyBorrowed, err)
|
||||||
})
|
})
|
||||||
t.Run("sucess", func(t *testing.T) {
|
t.Run("sucess", func(t *testing.T) {
|
||||||
u := &entity.User{
|
u := &entity.User{
|
||||||
@@ -95,9 +94,9 @@ func Test_Return(t *testing.T) {
|
|||||||
b := &entity.Book{
|
b := &entity.Book{
|
||||||
ID: entity.NewID(),
|
ID: entity.NewID(),
|
||||||
}
|
}
|
||||||
bMock.EXPECT().GetBook(b.ID).Return(nil, domain.ErrNotFound)
|
bMock.EXPECT().GetBook(b.ID).Return(nil, entity.ErrNotFound)
|
||||||
err := uc.Return(b)
|
err := uc.Return(b)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
})
|
})
|
||||||
t.Run("book not borrowed", func(t *testing.T) {
|
t.Run("book not borrowed", func(t *testing.T) {
|
||||||
u := &entity.User{
|
u := &entity.User{
|
||||||
@@ -109,7 +108,7 @@ func Test_Return(t *testing.T) {
|
|||||||
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
bMock.EXPECT().GetBook(b.ID).Return(b, nil)
|
||||||
uMock.EXPECT().ListUsers().Return([]*entity.User{u}, nil)
|
uMock.EXPECT().ListUsers().Return([]*entity.User{u}, nil)
|
||||||
err := uc.Return(b)
|
err := uc.Return(b)
|
||||||
assert.Equal(t, domain.ErrBookNotBorrowed, err)
|
assert.Equal(t, entity.ErrBookNotBorrowed, err)
|
||||||
})
|
})
|
||||||
t.Run("success", func(t *testing.T) {
|
t.Run("success", func(t *testing.T) {
|
||||||
u := &entity.User{
|
u := &entity.User{
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package user
|
package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
)
|
)
|
||||||
|
|
||||||
//Reader interface
|
//Reader interface
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
// Code generated by MockGen. DO NOT EDIT.
|
||||||
// Source: domain/usecase/user/interface.go
|
// Source: usecase/user/interface.go
|
||||||
|
|
||||||
// Package mock is a generated GoMock package.
|
// Package mock is a generated GoMock package.
|
||||||
package mock
|
package mock
|
||||||
|
|
||||||
import (
|
import (
|
||||||
entity "github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
entity "github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
gomock "github.com/golang/mock/gomock"
|
gomock "github.com/golang/mock/gomock"
|
||||||
reflect "reflect"
|
reflect "reflect"
|
||||||
)
|
)
|
||||||
@@ -4,9 +4,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
//Service interface
|
//Service interface
|
||||||
@@ -49,13 +47,13 @@ func (s *Service) ListUsers() ([]*entity.User, error) {
|
|||||||
func (s *Service) DeleteUser(id entity.ID) error {
|
func (s *Service) DeleteUser(id entity.ID) error {
|
||||||
u, err := s.GetUser(id)
|
u, err := s.GetUser(id)
|
||||||
if u == nil {
|
if u == nil {
|
||||||
return domain.ErrNotFound
|
return entity.ErrNotFound
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(u.Books) > 0 {
|
if len(u.Books) > 0 {
|
||||||
return domain.ErrCannotBeDeleted
|
return entity.ErrCannotBeDeleted
|
||||||
}
|
}
|
||||||
return s.repo.Delete(id)
|
return s.repo.Delete(id)
|
||||||
}
|
}
|
||||||
@@ -64,7 +62,7 @@ func (s *Service) DeleteUser(id entity.ID) error {
|
|||||||
func (s *Service) UpdateUser(e *entity.User) error {
|
func (s *Service) UpdateUser(e *entity.User) error {
|
||||||
err := e.Validate()
|
err := e.Validate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return domain.ErrInvalidEntity
|
return entity.ErrInvalidEntity
|
||||||
}
|
}
|
||||||
e.UpdatedAt = time.Now()
|
e.UpdatedAt = time.Now()
|
||||||
return s.repo.Update(e)
|
return s.repo.Update(e)
|
||||||
@@ -6,8 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
"github.com/eminetto/clean-architecture-go-v2/infra/repository"
|
||||||
|
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain"
|
"github.com/eminetto/clean-architecture-go-v2/entity"
|
||||||
"github.com/eminetto/clean-architecture-go-v2/domain/entity"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
@@ -50,7 +49,7 @@ func Test_SearchAndFind(t *testing.T) {
|
|||||||
assert.Equal(t, "Osbourne", c[0].LastName)
|
assert.Equal(t, "Osbourne", c[0].LastName)
|
||||||
|
|
||||||
c, err = m.SearchUsers("dio")
|
c, err = m.SearchUsers("dio")
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
assert.Nil(t, c)
|
assert.Nil(t, c)
|
||||||
})
|
})
|
||||||
t.Run("list all", func(t *testing.T) {
|
t.Run("list all", func(t *testing.T) {
|
||||||
@@ -91,12 +90,12 @@ func TestDelete(t *testing.T) {
|
|||||||
u2ID, _ := m.CreateUser(u2.Email, u2.Password, u2.FirstName, u2.LastName)
|
u2ID, _ := m.CreateUser(u2.Email, u2.Password, u2.FirstName, u2.LastName)
|
||||||
|
|
||||||
err := m.DeleteUser(u1.ID)
|
err := m.DeleteUser(u1.ID)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
|
|
||||||
err = m.DeleteUser(u2ID)
|
err = m.DeleteUser(u2ID)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
_, err = m.GetUser(u2ID)
|
_, err = m.GetUser(u2ID)
|
||||||
assert.Equal(t, domain.ErrNotFound, err)
|
assert.Equal(t, entity.ErrNotFound, err)
|
||||||
|
|
||||||
u3 := newFixtureUser()
|
u3 := newFixtureUser()
|
||||||
id, _ := m.CreateUser(u3.Email, u3.Password, u3.FirstName, u3.LastName)
|
id, _ := m.CreateUser(u3.Email, u3.Password, u3.FirstName, u3.LastName)
|
||||||
@@ -104,5 +103,5 @@ func TestDelete(t *testing.T) {
|
|||||||
saved.Books = []entity.ID{entity.NewID()}
|
saved.Books = []entity.ID{entity.NewID()}
|
||||||
_ = m.UpdateUser(saved)
|
_ = m.UpdateUser(saved)
|
||||||
err = m.DeleteUser(id)
|
err = m.DeleteUser(id)
|
||||||
assert.Equal(t, domain.ErrCannotBeDeleted, err)
|
assert.Equal(t, entity.ErrCannotBeDeleted, err)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user