Skip to content

Commit

Permalink
✨ Refactor Repository to one package (bxcodec#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bxcodec authored Dec 13, 2017
1 parent bfea8c0 commit 7ea48da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package mysql
package repository

import (
"database/sql"
"time"

"github.com/Sirupsen/logrus"

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

type mysqlArticleRepository struct {
Conn *sql.DB
}

func NewMysqlArticleRepository(Conn *sql.DB) ArticleRepository {

return &mysqlArticleRepository{Conn}
}

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

rows, err := m.Conn.Query(query, args...)
Expand Down Expand Up @@ -97,11 +100,9 @@ func (m *mysqlArticleRepository) Store(a *models.Article) (int64, error) {

return 0, err
}
now := time.Now()
a.CreatedAt = now
a.UpdatedAt = now
logrus.Debug("Created At: ", now)
res, err := stmt.Exec(a.Title, a.Content, now, now)

logrus.Debug("Created At: ", a.CreatedAt)
res, err := stmt.Exec(a.Title, a.Content, a.UpdatedAt, a.CreatedAt)
if err != nil {

return 0, err
Expand Down Expand Up @@ -139,8 +140,8 @@ func (m *mysqlArticleRepository) Update(ar *models.Article) (*models.Article, er
if err != nil {
return nil, nil
}
now := time.Now()
res, err := stmt.Exec(ar.Title, ar.Content, now, ar.ID)

res, err := stmt.Exec(ar.Title, ar.Content, ar.UpdatedAt, ar.ID)
if err != nil {
return nil, err
}
Expand All @@ -155,8 +156,3 @@ func (m *mysqlArticleRepository) Update(ar *models.Article) (*models.Article, er

return ar, nil
}

func NewMysqlArticleRepository(Conn *sql.DB) repository.ArticleRepository {

return &mysqlArticleRepository{Conn}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package mysql_test
package repository_test

import (
"testing"
"time"

models "github.com/bxcodec/go-clean-arch/article"
articleRepo "github.com/bxcodec/go-clean-arch/article/repository/mysql"
articleRepo "github.com/bxcodec/go-clean-arch/article/repository"
"github.com/stretchr/testify/assert"
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
)
Expand Down Expand Up @@ -52,12 +52,12 @@ func TestGetByID(t *testing.T) {
}

func TestStore(t *testing.T) {

now := time.Now()
ar := &models.Article{
Title: "Judul",
Content: "Content",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedAt: now,
UpdatedAt: now,
}
db, mock, err := sqlmock.New()
if err != nil {
Expand Down Expand Up @@ -117,13 +117,13 @@ func TestDelete(t *testing.T) {
}

func TestUpdate(t *testing.T) {

now := time.Now()
ar := &models.Article{
ID: 12,
Title: "Judul",
Content: "Content",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
CreatedAt: now,
UpdatedAt: now,
}

db, mock, err := sqlmock.New()
Expand Down

0 comments on commit 7ea48da

Please sign in to comment.