Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Introduce domain package #21

Merged
merged 17 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ EXPOSE 9090

COPY --from=builder /go/src/github.com/bxcodec/go-clean-arch/engine /app

CMD /app/engine
CMD /app/engine
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ vendor:
@dep ensure -v

engine: vendor
go build -o ${BINARY}
go build -o ${BINARY} app/*.go

install:
go build -o ${BINARY}
go build -o ${BINARY} app/*.go

unittest:
go test -short $$(go list ./... | grep -v /vendor/)
Expand Down
File renamed without changes.
18 changes: 8 additions & 10 deletions article/delivery/http/article_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/bxcodec/go-clean-arch/models"

"github.com/bxcodec/go-clean-arch/article"
"github.com/bxcodec/go-clean-arch/domain"
"github.com/labstack/echo"

validator "gopkg.in/go-playground/validator.v9"
Expand All @@ -22,10 +20,10 @@ type ResponseError struct {

// HttpArticleHandler represent the httphandler for article
type HttpArticleHandler struct {
AUsecase article.Usecase
AUsecase domain.ArticleUsecase
}

func NewArticleHttpHandler(e *echo.Echo, us article.Usecase) {
func NewArticleHttpHandler(e *echo.Echo, us domain.ArticleUsecase) {
handler := &HttpArticleHandler{
AUsecase: us,
}
Expand Down Expand Up @@ -72,7 +70,7 @@ func (a *HttpArticleHandler) GetByID(c echo.Context) error {
return c.JSON(http.StatusOK, art)
}

func isRequestValid(m *models.Article) (bool, error) {
func isRequestValid(m *domain.Article) (bool, error) {

validate := validator.New()

Expand All @@ -84,7 +82,7 @@ func isRequestValid(m *models.Article) (bool, error) {
}

func (a *HttpArticleHandler) Store(c echo.Context) error {
var article models.Article
var article domain.Article
err := c.Bind(&article)
if err != nil {
return c.JSON(http.StatusUnprocessableEntity, err.Error())
Expand Down Expand Up @@ -129,11 +127,11 @@ func getStatusCode(err error) int {
}
logrus.Error(err)
switch err {
case models.ErrInternalServerError:
case domain.ErrInternalServerError:
return http.StatusInternalServerError
case models.ErrNotFound:
case domain.ErrNotFound:
return http.StatusNotFound
case models.ErrConflict:
case domain.ErrConflict:
return http.StatusConflict
default:
return http.StatusInternalServerError
Expand Down
32 changes: 16 additions & 16 deletions article/delivery/http/article_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"time"

articleHttp "github.com/bxcodec/go-clean-arch/article/delivery/http"
"github.com/bxcodec/go-clean-arch/article/mocks"
"github.com/bxcodec/go-clean-arch/models"
"github.com/bxcodec/go-clean-arch/domain"
"github.com/bxcodec/go-clean-arch/domain/mocks"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -20,12 +20,12 @@ import (
)

func TestFetch(t *testing.T) {
var mockArticle models.Article
var mockArticle domain.Article
err := faker.FakeData(&mockArticle)
assert.NoError(t, err)
mockUCase := new(mocks.Usecase)
mockListArticle := make([]*models.Article, 0)
mockListArticle = append(mockListArticle, &mockArticle)
mockUCase := new(mocks.ArticleUsecase)
mockListArticle := make([]domain.Article, 0)
mockListArticle = append(mockListArticle, mockArticle)
num := 1
cursor := "2"
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(mockListArticle, "10", nil)
Expand All @@ -49,10 +49,10 @@ func TestFetch(t *testing.T) {
}

func TestFetchError(t *testing.T) {
mockUCase := new(mocks.Usecase)
mockUCase := new(mocks.ArticleUsecase)
num := 1
cursor := "2"
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(nil, "", models.ErrInternalServerError)
mockUCase.On("Fetch", mock.Anything, cursor, int64(num)).Return(nil, "", domain.ErrInternalServerError)

e := echo.New()
req, err := http.NewRequest(echo.GET, "/article?num=1&cursor="+cursor, strings.NewReader(""))
Expand All @@ -73,15 +73,15 @@ func TestFetchError(t *testing.T) {
}

func TestGetByID(t *testing.T) {
var mockArticle models.Article
var mockArticle domain.Article
err := faker.FakeData(&mockArticle)
assert.NoError(t, err)

mockUCase := new(mocks.Usecase)
mockUCase := new(mocks.ArticleUsecase)

num := int(mockArticle.ID)

mockUCase.On("GetByID", mock.Anything, int64(num)).Return(&mockArticle, nil)
mockUCase.On("GetByID", mock.Anything, int64(num)).Return(mockArticle, nil)

e := echo.New()
req, err := http.NewRequest(echo.GET, "/article/"+strconv.Itoa(int(num)), strings.NewReader(""))
Expand All @@ -102,7 +102,7 @@ func TestGetByID(t *testing.T) {
}

func TestStore(t *testing.T) {
mockArticle := models.Article{
mockArticle := domain.Article{
Title: "Title",
Content: "Content",
CreatedAt: time.Now(),
Expand All @@ -111,12 +111,12 @@ func TestStore(t *testing.T) {

tempMockArticle := mockArticle
tempMockArticle.ID = 0
mockUCase := new(mocks.Usecase)
mockUCase := new(mocks.ArticleUsecase)

j, err := json.Marshal(tempMockArticle)
assert.NoError(t, err)

mockUCase.On("Store", mock.Anything, mock.AnythingOfType("*models.Article")).Return(nil)
mockUCase.On("Store", mock.Anything, mock.AnythingOfType("*domain.Article")).Return(nil)

e := echo.New()
req, err := http.NewRequest(echo.POST, "/article", strings.NewReader(string(j)))
Expand All @@ -137,11 +137,11 @@ func TestStore(t *testing.T) {
}

func TestDelete(t *testing.T) {
var mockArticle models.Article
var mockArticle domain.Article
err := faker.FakeData(&mockArticle)
assert.NoError(t, err)

mockUCase := new(mocks.Usecase)
mockUCase := new(mocks.ArticleUsecase)

num := int(mockArticle.ID)

Expand Down
17 changes: 0 additions & 17 deletions article/repository.go

This file was deleted.

52 changes: 25 additions & 27 deletions article/repository/mysql_article.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/bxcodec/go-clean-arch/article"
"github.com/bxcodec/go-clean-arch/models"
"github.com/bxcodec/go-clean-arch/domain"
)

const (
Expand All @@ -21,13 +20,13 @@ type mysqlArticleRepository struct {
Conn *sql.DB
}

// NewMysqlArticleRepository will create an object that represent the article.Repository interface
func NewMysqlArticleRepository(Conn *sql.DB) article.Repository {
// NewMysqlArticleRepository will create an object that represent the domain.ArticleRepository interface
func NewMysqlArticleRepository(Conn *sql.DB) domain.ArticleRepository {

return &mysqlArticleRepository{Conn}
}

func (m *mysqlArticleRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]*models.Article, error) {
func (m *mysqlArticleRepository) fetch(ctx context.Context, query string, args ...interface{}) ([]domain.Article, error) {

rows, err := m.Conn.QueryContext(ctx, query, args...)

Expand All @@ -36,9 +35,9 @@ func (m *mysqlArticleRepository) fetch(ctx context.Context, query string, args .
return nil, err
}
defer rows.Close()
result := make([]*models.Article, 0)
result := make([]domain.Article, 0)
for rows.Next() {
t := new(models.Article)
t := domain.Article{}

This comment was marked as off-topic.

authorID := int64(0)
err = rows.Scan(
&t.ID,
Expand All @@ -53,7 +52,7 @@ func (m *mysqlArticleRepository) fetch(ctx context.Context, query string, args .
logrus.Error(err)
return nil, err
}
t.Author = models.Author{
t.Author = domain.Author{
ID: authorID,
}
result = append(result, t)
Expand All @@ -62,14 +61,14 @@ func (m *mysqlArticleRepository) fetch(ctx context.Context, query string, args .
return result, nil
}

func (m *mysqlArticleRepository) Fetch(ctx context.Context, cursor string, num int64) ([]*models.Article, string, error) {
func (m *mysqlArticleRepository) Fetch(ctx context.Context, cursor string, num int64) ([]domain.Article, string, error) {

This comment was marked as resolved.


query := `SELECT id,title,content, author_id, updated_at, created_at
FROM article WHERE created_at > ? ORDER BY created_at LIMIT ? `

decodedCursor, err := DecodeCursor(cursor)
if err != nil && cursor != "" {
return nil, "", models.ErrBadParamInput
return nil, "", domain.ErrBadParamInput
}
res, err := m.fetch(ctx, query, decodedCursor, num)
if err != nil {
Expand All @@ -82,44 +81,43 @@ func (m *mysqlArticleRepository) Fetch(ctx context.Context, cursor string, num i
return res, nextCursor, err

}
func (m *mysqlArticleRepository) GetByID(ctx context.Context, id int64) (*models.Article, error) {
func (m *mysqlArticleRepository) GetByID(ctx context.Context, id int64) (res domain.Article, err error) {

This comment was marked as resolved.

query := `SELECT id,title,content, author_id, updated_at, created_at
FROM article WHERE ID = ?`

list, err := m.fetch(ctx, query, id)
if err != nil {
return nil, err
return domain.Article{}, err
}

a := &models.Article{}
res = domain.Article{}
if len(list) > 0 {
a = list[0]
res = list[0]
} else {
return nil, models.ErrNotFound
return res, domain.ErrNotFound
}

return a, nil
return

This comment was marked as resolved.

}

func (m *mysqlArticleRepository) GetByTitle(ctx context.Context, title string) (*models.Article, error) {
func (m *mysqlArticleRepository) GetByTitle(ctx context.Context, title string) (res domain.Article, err error) {
query := `SELECT id,title,content, author_id, updated_at, created_at
FROM article WHERE title = ?`

list, err := m.fetch(ctx, query, title)
if err != nil {
return nil, err
return res, err
}

a := &models.Article{}
res = domain.Article{}
if len(list) > 0 {
a = list[0]
res = list[0]
} else {
return nil, models.ErrNotFound
return res, domain.ErrNotFound
}
return a, nil
return

This comment was marked as resolved.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jojoarianto actually @bxcodec using named return, so at the end of function, you need to put return
and for pointer afaik is not good using to many pointer, because you will using heap memory instead of stack and will allocated more

}

func (m *mysqlArticleRepository) Store(ctx context.Context, a *models.Article) error {
func (m *mysqlArticleRepository) Store(ctx context.Context, a *domain.Article) error {

query := `INSERT article SET title=? , content=? , author_id=?, updated_at=? , created_at=?`
stmt, err := m.Conn.PrepareContext(ctx, query)
Expand All @@ -134,11 +132,11 @@ func (m *mysqlArticleRepository) Store(ctx context.Context, a *models.Article) e

return err
}
lastId, err := res.LastInsertId()
lastID, err := res.LastInsertId()
if err != nil {
return err
}
a.ID = lastId
a.ID = lastID
return nil
}

Expand All @@ -165,7 +163,7 @@ func (m *mysqlArticleRepository) Delete(ctx context.Context, id int64) error {

return nil
}
func (m *mysqlArticleRepository) Update(ctx context.Context, ar *models.Article) error {
func (m *mysqlArticleRepository) Update(ctx context.Context, ar *domain.Article) error {
query := `UPDATE article set title=?, content=?, author_id=?, updated_at=? WHERE ID = ?`

stmt, err := m.Conn.PrepareContext(ctx, query)
Expand Down
20 changes: 10 additions & 10 deletions article/repository/mysqlarticle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

articleRepo "github.com/bxcodec/go-clean-arch/article/repository"
"github.com/bxcodec/go-clean-arch/models"
"github.com/bxcodec/go-clean-arch/domain"
"github.com/stretchr/testify/assert"
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
)
Expand All @@ -18,14 +18,14 @@ func TestFetch(t *testing.T) {
}
defer db.Close()

mockArticles := []models.Article{
models.Article{
mockArticles := []domain.Article{
domain.Article{
ID: 1, Title: "title 1", Content: "content 1",
Author: models.Author{ID: 1}, UpdatedAt: time.Now(), CreatedAt: time.Now(),
Author: domain.Author{ID: 1}, UpdatedAt: time.Now(), CreatedAt: time.Now(),
},
models.Article{
domain.Article{
ID: 2, Title: "title 2", Content: "content 2",
Author: models.Author{ID: 1}, UpdatedAt: time.Now(), CreatedAt: time.Now(),
Author: domain.Author{ID: 1}, UpdatedAt: time.Now(), CreatedAt: time.Now(),
},
}

Expand Down Expand Up @@ -69,12 +69,12 @@ func TestGetByID(t *testing.T) {

func TestStore(t *testing.T) {
now := time.Now()
ar := &models.Article{
ar := &domain.Article{
Title: "Judul",
Content: "Content",
CreatedAt: now,
UpdatedAt: now,
Author: models.Author{
Author: domain.Author{
ID: 1,
Name: "Iman Tumorang",
},
Expand Down Expand Up @@ -137,13 +137,13 @@ func TestDelete(t *testing.T) {

func TestUpdate(t *testing.T) {
now := time.Now()
ar := &models.Article{
ar := &domain.Article{
ID: 12,
Title: "Judul",
Content: "Content",
CreatedAt: now,
UpdatedAt: now,
Author: models.Author{
Author: domain.Author{
ID: 1,
Name: "Iman Tumorang",
},
Expand Down
Loading